@@ -139,6 +139,10 @@ public override void OnGui(Rect rect)
139139 var layerMask = target . layer ;
140140 var layerName = LayerMask . LayerToName ( layerMask ) ;
141141
142+ var contentText = GetContentText ( ) ;
143+ var content = new GUIContent ( contentText , $ "{ layerName } layer") ;
144+ EditorGUI . LabelField ( rect , content , Style . centreAlignTextStyle ) ;
145+
142146 string GetContentText ( )
143147 {
144148 switch ( layerMask )
@@ -148,9 +152,6 @@ string GetContentText()
148152 default : return layerMask . ToString ( ) ;
149153 }
150154 }
151-
152- var content = new GUIContent ( GetContentText ( ) , layerName + " layer" ) ;
153- EditorGUI . LabelField ( rect , content , Style . centreAlignTextStyle ) ;
154155 }
155156 }
156157
@@ -159,43 +160,31 @@ private class HierarchyScriptLabel : HierarchyPropertyLabel
159160 private static Texture componentIcon ;
160161 private static Texture transformIcon ;
161162
162- private float baseWidth ;
163- private float summWidth ;
164-
165- private bool isHighlighted ;
166-
167163 /// <summary>
168164 /// Cached components of the last prepared <see cref="target"/>.
169165 /// </summary>
170- private List < Component > cachedComponents ;
171-
172- private void CacheComponents ( GameObject target )
173- {
174- var components = target . GetComponents < Component > ( ) ;
175- cachedComponents = new List < Component > ( components . Length ) ;
176- //cache only valid (non-null) components
177- foreach ( var component in components )
178- {
179- if ( component == null )
180- {
181- continue ;
182- }
183-
184- cachedComponents . Add ( component ) ;
185- }
186- }
166+ private Component [ ] components ;
167+ private float baseWidth ;
168+ private float summWidth ;
169+ private bool isHighlighted ;
187170
188- private GUIContent GetTooltip ( Rect rect )
171+ private GUIContent GetTooltipContent ( )
189172 {
190- var componentsCount = cachedComponents . Count ;
173+ var componentsCount = components . Length ;
191174 var tooltipBuilder = new StringBuilder ( ) ;
192175 var tooltipContent = new GUIContent ( ) ;
193176
194177 tooltipBuilder . Append ( "Components:\n " ) ;
195178 for ( var i = 1 ; i < componentsCount ; i ++ )
196179 {
180+ var component = components [ i ] ;
181+ if ( component == null )
182+ {
183+ continue ;
184+ }
185+
197186 tooltipBuilder . Append ( "- " ) ;
198- tooltipBuilder . Append ( cachedComponents [ i ] . GetType ( ) . Name ) ;
187+ tooltipBuilder . Append ( component . GetType ( ) . Name ) ;
199188 if ( componentsCount - 1 != i )
200189 {
201190 tooltipBuilder . Append ( "\n " ) ;
@@ -209,6 +198,7 @@ private GUIContent GetTooltip(Rect rect)
209198 private GUIContent GetContent ( Component component )
210199 {
211200 var content = EditorGUIUtility . ObjectContent ( component , component . GetType ( ) ) ;
201+ content . text = string . Empty ;
212202 if ( content . image == null )
213203 {
214204 content . image = componentIcon ;
@@ -220,31 +210,23 @@ private GUIContent GetContent(Component component)
220210 public override bool Prepare ( GameObject target , Rect availableRect )
221211 {
222212 var isValid = base . Prepare ( target , availableRect ) ;
223- if ( isValid )
213+ if ( ! isValid )
224214 {
225- baseWidth = Style . minWidth ;
226- var rect = availableRect ;
227- rect . xMin = rect . xMax - baseWidth ;
228- if ( rect . Contains ( Event . current . mousePosition ) )
229- {
230- isHighlighted = true ;
231- CacheComponents ( target ) ;
232- summWidth = cachedComponents . Count > 1
233- ? ( cachedComponents . Count - 1 ) * baseWidth
234- : baseWidth ;
235- }
236- else
237- {
238- isHighlighted = false ;
239- summWidth = baseWidth ;
240- }
241-
242- componentIcon = componentIcon ?? EditorGUIUtility . IconContent ( "cs Script Icon" ) . image ;
243- transformIcon = transformIcon ?? EditorGUIUtility . IconContent ( "Transform Icon" ) . image ;
244- return true ;
215+ return false ;
245216 }
246217
247- return false ;
218+ baseWidth = Style . minWidth ;
219+ components = target . GetComponents < Component > ( ) ;
220+ var componentsCount = components . Length ;
221+ summWidth = componentsCount > 1
222+ ? ( componentsCount - 1 ) * baseWidth
223+ : baseWidth ;
224+
225+ isHighlighted = availableRect . Contains ( Event . current . mousePosition ) ;
226+
227+ componentIcon = componentIcon != null ? componentIcon : EditorGUIUtility . IconContent ( "cs Script Icon" ) . image ;
228+ transformIcon = transformIcon != null ? transformIcon : EditorGUIUtility . IconContent ( "Transform Icon" ) . image ;
229+ return true ;
248230 }
249231
250232 public override float GetWidth ( )
@@ -254,47 +236,43 @@ public override float GetWidth()
254236
255237 public override void OnGui ( Rect rect )
256238 {
257- var tooltip = string . Empty ;
258- var texture = componentIcon ;
259-
239+ var fullRect = rect ;
260240 rect . xMin = rect . xMax - baseWidth ;
261241
262- if ( isHighlighted )
242+ var componentsCount = components . Length ;
243+ if ( componentsCount <= 1 )
263244 {
264- var componentsCount = cachedComponents . Count ;
265- if ( componentsCount > 1 )
266- {
267- //draw tooltip based on all available components
268- GUI . Label ( rect , GetTooltip ( rect ) ) ;
269-
270- rect . xMin -= baseWidth * ( componentsCount - 2 ) ;
271-
272- var iconRect = rect ;
273- iconRect . xMin = rect . xMin ;
274- iconRect . xMax = rect . xMin + baseWidth ;
245+ GUI . Label ( fullRect , new GUIContent ( transformIcon , "There is no additional component" ) ) ;
246+ return ;
247+ }
275248
276- //draw all icons associated to cached components (except transform)
277- for ( var i = 1 ; i < cachedComponents . Count ; i ++ )
278- {
279- var component = cachedComponents [ i ] ;
280- var content = GetContent ( component ) ;
249+ rect . xMin -= baseWidth * ( componentsCount - 2 ) ;
281250
282- //draw icon for the current component
283- GUI . Label ( iconRect , new GUIContent ( content . image ) ) ;
284- //adjust rect for the next script icon
285- iconRect . x += baseWidth ;
286- }
251+ var iconRect = rect ;
252+ iconRect . xMin = rect . xMin ;
253+ iconRect . xMax = rect . xMin + baseWidth ;
287254
288- return ;
289- }
290- else
255+ //draw all icons associated to cached components (except transform)
256+ for ( var i = 1 ; i < components . Length ; i ++ )
257+ {
258+ var component = components [ i ] ;
259+ if ( component == null )
291260 {
292- texture = transformIcon ;
293- tooltip = "There is no additional component" ;
261+ continue ;
294262 }
263+
264+ var content = GetContent ( component ) ;
265+ //draw icon for the current component
266+ GUI . Label ( iconRect , content ) ;
267+ //adjust rect for the next script icon
268+ iconRect . x += baseWidth ;
295269 }
296270
297- GUI . Label ( rect , new GUIContent ( texture , tooltip ) ) ;
271+ if ( isHighlighted )
272+ {
273+ var tooltipContent = GetTooltipContent ( ) ;
274+ GUI . Label ( fullRect , tooltipContent ) ;
275+ }
298276 }
299277 }
300278
@@ -409,19 +387,19 @@ public void OnGUI(Rect rect, GameObject target, int siblingIndex, bool isCurrent
409387 if ( GetParentChildCount ( target ) == ( siblingIndex + 1 ) )
410388 {
411389 renderedLastLevelGameobject = true ;
412- EditorGUI . LabelField ( rect , Style . elementLast , Style . centreAlignTreeLineStyle ) ;
390+ EditorGUI . LabelField ( rect , Style . treeElementLast , Style . treeElementStyle ) ;
413391 }
414392 else
415393 {
416394 renderedLastLevelGameobject = false ;
417- EditorGUI . LabelField ( rect , Style . elementCross , Style . centreAlignTreeLineStyle ) ;
395+ EditorGUI . LabelField ( rect , Style . treeElementCross , Style . treeElementStyle ) ;
418396 }
419397 }
420398 else
421399 {
422400 if ( ! renderedLastLevelGameobject )
423401 {
424- EditorGUI . LabelField ( rect , Style . elementPass , Style . centreAlignTreeLineStyle ) ;
402+ EditorGUI . LabelField ( rect , Style . treeElementPass , Style . treeElementStyle ) ;
425403 }
426404 }
427405 }
@@ -461,19 +439,19 @@ protected static class Style
461439 internal static readonly GUIStyle defaultAlignTextStyle ;
462440 internal static readonly GUIStyle centreAlignTextStyle ;
463441 internal static readonly GUIStyle rightAlignTextStyle ;
464- internal static readonly GUIStyle centreAlignTreeLineStyle ;
442+ internal static readonly GUIStyle treeElementStyle ;
465443
466- internal static readonly GUIContent elementLast ;
467- internal static readonly GUIContent elementCross ;
468- internal static readonly GUIContent elementPass ;
444+ internal static readonly GUIContent treeElementLast ;
445+ internal static readonly GUIContent treeElementCross ;
446+ internal static readonly GUIContent treeElementPass ;
469447
470448 internal static readonly Color characterColor ;
471449
472450 static Style ( )
473451 {
474- elementLast = new GUIContent ( "└" ) ;
475- elementCross = new GUIContent ( "├" ) ;
476- elementPass = new GUIContent ( "│" ) ;
452+ treeElementLast = new GUIContent ( "└" ) ;
453+ treeElementCross = new GUIContent ( "├" ) ;
454+ treeElementPass = new GUIContent ( "│" ) ;
477455
478456 defaultAlignTextStyle = new GUIStyle ( EditorStyles . miniLabel )
479457 {
@@ -503,14 +481,14 @@ static Style()
503481 alignment = TextAnchor . UpperRight
504482#endif
505483 } ;
506- centreAlignTreeLineStyle = new GUIStyle ( EditorStyles . miniLabel )
484+ treeElementStyle = new GUIStyle ( EditorStyles . miniLabel )
507485 {
508- fontSize = 18 ,
486+ fontSize = 16 ,
509487 } ;
510488
511489 if ( ! EditorGUIUtility . isProSkin )
512490 {
513- centreAlignTreeLineStyle . normal . textColor = Color . white ;
491+ treeElementStyle . normal . textColor = Color . white ;
514492 }
515493 }
516494 }
0 commit comments