Skip to content

Commit fd43673

Browse files
committed
Made custom types faster and more stable
1 parent 1ea10a3 commit fd43673

File tree

3 files changed

+55
-52
lines changed

3 files changed

+55
-52
lines changed

Editor/Utilities/Extensions.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ public static class SerializedPropertyExtensions
1616
/// <returns></returns>
1717
public static System.Type GetSerializedType(this SerializedProperty property)
1818
{
19-
var value = property.GetSerializedValue();
20-
if (value != null) return value.GetType();
2119
return property.GetSerializedFieldInfo()?.FieldType;
2220
}
2321

@@ -48,7 +46,7 @@ public static object GetSerializedValue(this SerializedProperty property)
4846

4947
// Manually get a bunch because its more efficient than looking up serialized values
5048
case SerializedPropertyType.ObjectReference:
51-
return property.objectReferenceValue ? property.objectReferenceValue.name : "[ NULL ]";
49+
return property.objectReferenceValue ? property.objectReferenceValue : null;
5250
case SerializedPropertyType.Boolean:
5351
return property.boolValue;
5452
case SerializedPropertyType.Integer:

Editor/Utilities/RandomListDrawer.cs

Lines changed: 53 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,17 @@
1111
namespace Lachee.Utilities.Editor
1212
{
1313
/// <summary>Default styling used for the random list</summary>
14-
public sealed class RandomListEditorStyle
14+
public class RandomListDrawerStyle
1515
{
16-
public static readonly Color[] Colors =
16+
/// <summary>Dictionary lookup for custom drawers for the slider labels</summary>
17+
public Dictionary<Type, IRistBoxLabelDrawer> Drawers = new Dictionary<Type, IRistBoxLabelDrawer>()
18+
{
19+
{ typeof(void), new BasicRistDrawer() },
20+
{ typeof(Color), new ColorRistDrawer() },
21+
{ typeof(AnimationCurve), new AnimationCurveRistDrawer() }
22+
};
23+
24+
public readonly Color[] Colors =
1725
{
1826
new Color(0.4831376f, 0.6211768f, 0.0219608f, 1.0f),
1927
new Color(0.3827448f, 0.2886272f, 0.5239216f, 1.0f),
@@ -39,25 +47,22 @@ public sealed class RandomListEditorStyle
3947
/// <summary>Interface used to draw labels on weight sliders</summary>
4048
public interface IRistBoxLabelDrawer
4149
{
42-
public void DrawLabel(Rect position, RandomListEditorStyle style, SerializedProperty property, float weight, bool isMini);
50+
public void DrawLabel(Rect position, RandomListDrawerStyle style, SerializedProperty property, float weight, bool isMini);
4351
}
4452

45-
4653
/// <summary>
4754
/// Handles drawing the Random List and its sliders sliders
4855
/// </summary>
4956
#pragma warning disable CS0618 // Type or member is obsolete
5057
[CustomPropertyDrawer(typeof(RandomList<>))]
5158
[CustomPropertyDrawer(typeof(Rist<>))]
5259
#pragma warning restore CS0618 // Type or member is obsolete
53-
public sealed class RandomListDrawer : UnityEditor.PropertyDrawer, IRistBoxLabelDrawer
60+
public sealed class RandomListDrawer : UnityEditor.PropertyDrawer
5461
{
55-
/// <summary>Dictionary lookup for custom drawers for the slider labels</summary>
56-
public static Dictionary<System.Type, IRistBoxLabelDrawer> SliderLabelDrawers = new Dictionary<Type, IRistBoxLabelDrawer>()
57-
{
58-
{ typeof(Color), new ColorRistDrawer() },
59-
{ typeof(AnimationCurve), new AnimationCurveRistDrawer() }
60-
};
62+
63+
private static RandomListDrawerStyle _style;
64+
public static RandomListDrawerStyle style => _style;
65+
6166

6267
private const int WEIGHT_HEIGHT = 32;
6368
private const int WEIGHT_RANGE_PADDING = 2;
@@ -67,16 +72,6 @@ public sealed class RandomListDrawer : UnityEditor.PropertyDrawer, IRistBoxLabel
6772
private int _selectedWeight = -1;
6873
private ReorderableList _list;
6974

70-
private static RandomListEditorStyle _style;
71-
private static RandomListEditorStyle Style
72-
{
73-
get
74-
{
75-
if (_style == null)
76-
_style = new RandomListEditorStyle();
77-
return _style;
78-
}
79-
}
8075

8176
private bool? _isFoldOut;
8277
private bool isListFoldout
@@ -124,12 +119,12 @@ private static ReorderableList CreateReoderableList(SerializedProperty property)
124119
var previous = GUI.backgroundColor;
125120

126121
if (index >= 0)
127-
GUI.backgroundColor = RandomListEditorStyle.Colors[index % RandomListEditorStyle.Colors.Length];
122+
GUI.backgroundColor = style.Colors[index % style.Colors.Length];
128123

129124
if (!isActive)
130125
GUI.backgroundColor *= 0.6f;
131126

132-
Style.sliderRange.Draw(rect, GUIContent.none, false, false, false, false);
127+
style.sliderRange.Draw(rect, GUIContent.none, false, false, false, false);
133128
GUI.backgroundColor = previous;
134129
};
135130

@@ -172,8 +167,11 @@ private static ReorderableList CreateReoderableList(SerializedProperty property)
172167

173168
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
174169
{
175-
isListFoldout = EditorGUI.Foldout(new Rect(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight), isListFoldout, label);
170+
// Initialize the style the first chance we get
171+
if (_style == null)
172+
_style = new RandomListDrawerStyle();
176173

174+
isListFoldout = EditorGUI.Foldout(new Rect(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight), isListFoldout, label);
177175
EditorGUI.indentLevel++;
178176
if (isListFoldout)
179177
{
@@ -298,16 +296,14 @@ private void OnSliderBoxGUI(Rect groupRect, SerializedProperty property, List<We
298296
}
299297
private void DrawSliderBox(Rect area, List<WeightSlider> weights, int activeWeight, bool isCompact)
300298
{
301-
Style.sliderBG.Draw(area, GUIContent.none, false, false, false, false);
299+
style.sliderBG.Draw(area, GUIContent.none, false, false, false, false);
302300
var tempColor = GUI.backgroundColor;
303301
for (int i = 0; i < weights.Count; i++)
304302
{
305303
// Get the weight and update any missing drawers
306304
WeightSlider weight = weights[i];
307-
if (weight.labelDrawer == null)
308-
weight.labelDrawer = this;
305+
Color color = style.Colors[i % style.Colors.Length];
309306

310-
Color color = RandomListEditorStyle.Colors[i % RandomListEditorStyle.Colors.Length];
311307
// The can no longer be activated
312308
// if (activeWeight == i)
313309
// {
@@ -326,8 +322,9 @@ private void DrawSliderBox(Rect area, List<WeightSlider> weights, int activeWeig
326322
{
327323
//DrawLODRAnge
328324
GUI.backgroundColor = color * 0.6f;
329-
Style.sliderRange.Draw(weight.rangePosition, GUIContent.none, false, false, false, false);
330-
weight.labelDrawer.DrawLabel(weight.rangePosition, Style, weight.itemProperty, weight.weightPercentage, isCompact);
325+
style.sliderRange.Draw(weight.rangePosition, GUIContent.none, false, false, false, false);
326+
if (weight.labelDrawer != null)
327+
weight.labelDrawer.DrawLabel(weight.rangePosition, style, weight.itemProperty, weight.weightPercentage, isCompact);
331328
}
332329

333330
//DrawLODButton
@@ -341,16 +338,6 @@ private void DrawSliderBox(Rect area, List<WeightSlider> weights, int activeWeig
341338
GUI.backgroundColor = tempColor;
342339
}
343340

344-
void IRistBoxLabelDrawer.DrawLabel(Rect position, RandomListEditorStyle style, SerializedProperty property, float weight, bool isMini)
345-
{
346-
var name = property.hasVisibleChildren ? property.displayName : property.GetValueName();
347-
var label = isMini
348-
? name
349-
: string.Format("{0}\n{1:0}%", name, weight * 100);
350-
351-
style.sliderText.Draw(position, label, false, false, false, false);
352-
}
353-
354341
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
355342
{
356343
if (!isListFoldout)
@@ -411,6 +398,12 @@ private static List<WeightSlider> CreateSliders(SerializedProperty property, Rec
411398
SerializedProperty itemListProperty = property.FindPropertyRelative("_list");
412399
SerializedProperty weightListProperty = property.FindPropertyRelative("_weights");
413400

401+
var baseType = property.GetSerializedType();
402+
var elmType = baseType.GetGenericArguments()[0];
403+
IRistBoxLabelDrawer drawer;
404+
if (style.Drawers == null || elmType == null || !style.Drawers.TryGetValue(elmType, out drawer))
405+
drawer = style.Drawers[typeof(void)];
406+
414407
float weightOffset = 0;
415408
for (int i = 0; i < itemListProperty.arraySize; i++)
416409
{
@@ -426,27 +419,39 @@ private static List<WeightSlider> CreateSliders(SerializedProperty property, Rec
426419
startWeight = weightOffset,
427420
endWeight = weightOffset + weightProperty.floatValue,
428421
sumWeight = totalWeightProperty.floatValue,
422+
labelDrawer = drawer,
429423
};
430424

431425
// Calculate the rect
432426
float buttonSize = 10;
433427
info.rangePosition = new Rect(sliderRect.x + info.startPercentage * sliderRect.width, sliderRect.y, info.weightPercentage * sliderRect.width, sliderRect.height);
434428
info.buttonPosition = new Rect(info.rangePosition.x - (buttonSize / 2), info.rangePosition.y, buttonSize, info.rangePosition.height);
429+
435430
infos.Add(info);
436-
437-
var type = itemProperty.GetSerializedType();
438-
SliderLabelDrawers.TryGetValue(type, out info.labelDrawer);
439-
440431
weightOffset += info.weight;
441432
}
442433

434+
443435
return infos;
444436
}
445437
}
446438

447-
internal class ColorRistDrawer : IRistBoxLabelDrawer
439+
internal sealed class BasicRistDrawer : IRistBoxLabelDrawer
440+
{
441+
public void DrawLabel(Rect position, RandomListDrawerStyle style, SerializedProperty property, float weight, bool isMini)
442+
{
443+
var name = property.hasVisibleChildren ? property.displayName : property.GetValueName();
444+
var label = isMini
445+
? name
446+
: string.Format("{0}\n{1:0}%", name, weight * 100);
447+
448+
style.sliderText.Draw(position, label, false, false, false, false);
449+
}
450+
}
451+
452+
internal sealed class ColorRistDrawer : IRistBoxLabelDrawer
448453
{
449-
public void DrawLabel(Rect position, RandomListEditorStyle style, SerializedProperty property, float weight, bool isMini)
454+
public void DrawLabel(Rect position, RandomListDrawerStyle style, SerializedProperty property, float weight, bool isMini)
450455
{
451456
var label = isMini
452457
? property.GetValueName()
@@ -463,11 +468,11 @@ public void DrawLabel(Rect position, RandomListEditorStyle style, SerializedProp
463468
}
464469
}
465470

466-
internal class AnimationCurveRistDrawer : IRistBoxLabelDrawer
471+
internal sealed class AnimationCurveRistDrawer : IRistBoxLabelDrawer
467472
{
468473
const float PADDING = 2;
469474

470-
public void DrawLabel(Rect position, RandomListEditorStyle style, SerializedProperty property, float weight, bool isMini)
475+
public void DrawLabel(Rect position, RandomListDrawerStyle style, SerializedProperty property, float weight, bool isMini)
471476
{
472477
var label = isMini
473478
? ""

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.lachee.utilities",
3-
"version": "1.3.3",
3+
"version": "1.3.4",
44
"displayName": "Lachee's Utilities",
55
"description": "Bunch of utility functionality",
66
"unity": "2019.1",

0 commit comments

Comments
 (0)