@@ -139,6 +139,10 @@ public override void OnGui(Rect rect)
139
139
var layerMask = target . layer ;
140
140
var layerName = LayerMask . LayerToName ( layerMask ) ;
141
141
142
+ var contentText = GetContentText ( ) ;
143
+ var content = new GUIContent ( contentText , $ "{ layerName } layer") ;
144
+ EditorGUI . LabelField ( rect , content , Style . centreAlignTextStyle ) ;
145
+
142
146
string GetContentText ( )
143
147
{
144
148
switch ( layerMask )
@@ -148,9 +152,6 @@ string GetContentText()
148
152
default : return layerMask . ToString ( ) ;
149
153
}
150
154
}
151
-
152
- var content = new GUIContent ( GetContentText ( ) , layerName + " layer" ) ;
153
- EditorGUI . LabelField ( rect , content , Style . centreAlignTextStyle ) ;
154
155
}
155
156
}
156
157
@@ -159,43 +160,31 @@ private class HierarchyScriptLabel : HierarchyPropertyLabel
159
160
private static Texture componentIcon ;
160
161
private static Texture transformIcon ;
161
162
162
- private float baseWidth ;
163
- private float summWidth ;
164
-
165
- private bool isHighlighted ;
166
-
167
163
/// <summary>
168
164
/// Cached components of the last prepared <see cref="target"/>.
169
165
/// </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 ;
187
170
188
- private GUIContent GetTooltip ( Rect rect )
171
+ private GUIContent GetTooltipContent ( )
189
172
{
190
- var componentsCount = cachedComponents . Count ;
173
+ var componentsCount = components . Length ;
191
174
var tooltipBuilder = new StringBuilder ( ) ;
192
175
var tooltipContent = new GUIContent ( ) ;
193
176
194
177
tooltipBuilder . Append ( "Components:\n " ) ;
195
178
for ( var i = 1 ; i < componentsCount ; i ++ )
196
179
{
180
+ var component = components [ i ] ;
181
+ if ( component == null )
182
+ {
183
+ continue ;
184
+ }
185
+
197
186
tooltipBuilder . Append ( "- " ) ;
198
- tooltipBuilder . Append ( cachedComponents [ i ] . GetType ( ) . Name ) ;
187
+ tooltipBuilder . Append ( component . GetType ( ) . Name ) ;
199
188
if ( componentsCount - 1 != i )
200
189
{
201
190
tooltipBuilder . Append ( "\n " ) ;
@@ -209,6 +198,7 @@ private GUIContent GetTooltip(Rect rect)
209
198
private GUIContent GetContent ( Component component )
210
199
{
211
200
var content = EditorGUIUtility . ObjectContent ( component , component . GetType ( ) ) ;
201
+ content . text = string . Empty ;
212
202
if ( content . image == null )
213
203
{
214
204
content . image = componentIcon ;
@@ -220,31 +210,23 @@ private GUIContent GetContent(Component component)
220
210
public override bool Prepare ( GameObject target , Rect availableRect )
221
211
{
222
212
var isValid = base . Prepare ( target , availableRect ) ;
223
- if ( isValid )
213
+ if ( ! isValid )
224
214
{
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 ;
245
216
}
246
217
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 ;
248
230
}
249
231
250
232
public override float GetWidth ( )
@@ -254,47 +236,43 @@ public override float GetWidth()
254
236
255
237
public override void OnGui ( Rect rect )
256
238
{
257
- var tooltip = string . Empty ;
258
- var texture = componentIcon ;
259
-
239
+ var fullRect = rect ;
260
240
rect . xMin = rect . xMax - baseWidth ;
261
241
262
- if ( isHighlighted )
242
+ var componentsCount = components . Length ;
243
+ if ( componentsCount <= 1 )
263
244
{
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
+ }
275
248
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 ) ;
281
250
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 ;
287
254
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 )
291
260
{
292
- texture = transformIcon ;
293
- tooltip = "There is no additional component" ;
261
+ continue ;
294
262
}
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 ;
295
269
}
296
270
297
- GUI . Label ( rect , new GUIContent ( texture , tooltip ) ) ;
271
+ if ( isHighlighted )
272
+ {
273
+ var tooltipContent = GetTooltipContent ( ) ;
274
+ GUI . Label ( fullRect , tooltipContent ) ;
275
+ }
298
276
}
299
277
}
300
278
@@ -409,19 +387,19 @@ public void OnGUI(Rect rect, GameObject target, int siblingIndex, bool isCurrent
409
387
if ( GetParentChildCount ( target ) == ( siblingIndex + 1 ) )
410
388
{
411
389
renderedLastLevelGameobject = true ;
412
- EditorGUI . LabelField ( rect , Style . elementLast , Style . centreAlignTreeLineStyle ) ;
390
+ EditorGUI . LabelField ( rect , Style . treeElementLast , Style . treeElementStyle ) ;
413
391
}
414
392
else
415
393
{
416
394
renderedLastLevelGameobject = false ;
417
- EditorGUI . LabelField ( rect , Style . elementCross , Style . centreAlignTreeLineStyle ) ;
395
+ EditorGUI . LabelField ( rect , Style . treeElementCross , Style . treeElementStyle ) ;
418
396
}
419
397
}
420
398
else
421
399
{
422
400
if ( ! renderedLastLevelGameobject )
423
401
{
424
- EditorGUI . LabelField ( rect , Style . elementPass , Style . centreAlignTreeLineStyle ) ;
402
+ EditorGUI . LabelField ( rect , Style . treeElementPass , Style . treeElementStyle ) ;
425
403
}
426
404
}
427
405
}
@@ -461,19 +439,19 @@ protected static class Style
461
439
internal static readonly GUIStyle defaultAlignTextStyle ;
462
440
internal static readonly GUIStyle centreAlignTextStyle ;
463
441
internal static readonly GUIStyle rightAlignTextStyle ;
464
- internal static readonly GUIStyle centreAlignTreeLineStyle ;
442
+ internal static readonly GUIStyle treeElementStyle ;
465
443
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 ;
469
447
470
448
internal static readonly Color characterColor ;
471
449
472
450
static Style ( )
473
451
{
474
- elementLast = new GUIContent ( "└" ) ;
475
- elementCross = new GUIContent ( "├" ) ;
476
- elementPass = new GUIContent ( "│" ) ;
452
+ treeElementLast = new GUIContent ( "└" ) ;
453
+ treeElementCross = new GUIContent ( "├" ) ;
454
+ treeElementPass = new GUIContent ( "│" ) ;
477
455
478
456
defaultAlignTextStyle = new GUIStyle ( EditorStyles . miniLabel )
479
457
{
@@ -503,14 +481,14 @@ static Style()
503
481
alignment = TextAnchor . UpperRight
504
482
#endif
505
483
} ;
506
- centreAlignTreeLineStyle = new GUIStyle ( EditorStyles . miniLabel )
484
+ treeElementStyle = new GUIStyle ( EditorStyles . miniLabel )
507
485
{
508
- fontSize = 18 ,
486
+ fontSize = 16 ,
509
487
} ;
510
488
511
489
if ( ! EditorGUIUtility . isProSkin )
512
490
{
513
- centreAlignTreeLineStyle . normal . textColor = Color . white ;
491
+ treeElementStyle . normal . textColor = Color . white ;
514
492
}
515
493
}
516
494
}
0 commit comments