Skip to content

Commit

Permalink
Made custom types faster and more stable
Browse files Browse the repository at this point in the history
  • Loading branch information
Lachee committed Aug 10, 2022
1 parent 1ea10a3 commit fd43673
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 52 deletions.
4 changes: 1 addition & 3 deletions Editor/Utilities/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ public static class SerializedPropertyExtensions
/// <returns></returns>
public static System.Type GetSerializedType(this SerializedProperty property)
{
var value = property.GetSerializedValue();
if (value != null) return value.GetType();
return property.GetSerializedFieldInfo()?.FieldType;
}

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

// Manually get a bunch because its more efficient than looking up serialized values
case SerializedPropertyType.ObjectReference:
return property.objectReferenceValue ? property.objectReferenceValue.name : "[ NULL ]";
return property.objectReferenceValue ? property.objectReferenceValue : null;
case SerializedPropertyType.Boolean:
return property.boolValue;
case SerializedPropertyType.Integer:
Expand Down
101 changes: 53 additions & 48 deletions Editor/Utilities/RandomListDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,17 @@
namespace Lachee.Utilities.Editor
{
/// <summary>Default styling used for the random list</summary>
public sealed class RandomListEditorStyle
public class RandomListDrawerStyle
{
public static readonly Color[] Colors =
/// <summary>Dictionary lookup for custom drawers for the slider labels</summary>
public Dictionary<Type, IRistBoxLabelDrawer> Drawers = new Dictionary<Type, IRistBoxLabelDrawer>()
{
{ typeof(void), new BasicRistDrawer() },
{ typeof(Color), new ColorRistDrawer() },
{ typeof(AnimationCurve), new AnimationCurveRistDrawer() }
};

public readonly Color[] Colors =
{
new Color(0.4831376f, 0.6211768f, 0.0219608f, 1.0f),
new Color(0.3827448f, 0.2886272f, 0.5239216f, 1.0f),
Expand All @@ -39,25 +47,22 @@ public sealed class RandomListEditorStyle
/// <summary>Interface used to draw labels on weight sliders</summary>
public interface IRistBoxLabelDrawer
{
public void DrawLabel(Rect position, RandomListEditorStyle style, SerializedProperty property, float weight, bool isMini);
public void DrawLabel(Rect position, RandomListDrawerStyle style, SerializedProperty property, float weight, bool isMini);
}


/// <summary>
/// Handles drawing the Random List and its sliders sliders
/// </summary>
#pragma warning disable CS0618 // Type or member is obsolete
[CustomPropertyDrawer(typeof(RandomList<>))]
[CustomPropertyDrawer(typeof(Rist<>))]
#pragma warning restore CS0618 // Type or member is obsolete
public sealed class RandomListDrawer : UnityEditor.PropertyDrawer, IRistBoxLabelDrawer
public sealed class RandomListDrawer : UnityEditor.PropertyDrawer
{
/// <summary>Dictionary lookup for custom drawers for the slider labels</summary>
public static Dictionary<System.Type, IRistBoxLabelDrawer> SliderLabelDrawers = new Dictionary<Type, IRistBoxLabelDrawer>()
{
{ typeof(Color), new ColorRistDrawer() },
{ typeof(AnimationCurve), new AnimationCurveRistDrawer() }
};

private static RandomListDrawerStyle _style;
public static RandomListDrawerStyle style => _style;


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

private static RandomListEditorStyle _style;
private static RandomListEditorStyle Style
{
get
{
if (_style == null)
_style = new RandomListEditorStyle();
return _style;
}
}

private bool? _isFoldOut;
private bool isListFoldout
Expand Down Expand Up @@ -124,12 +119,12 @@ private static ReorderableList CreateReoderableList(SerializedProperty property)
var previous = GUI.backgroundColor;

if (index >= 0)
GUI.backgroundColor = RandomListEditorStyle.Colors[index % RandomListEditorStyle.Colors.Length];
GUI.backgroundColor = style.Colors[index % style.Colors.Length];

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

Style.sliderRange.Draw(rect, GUIContent.none, false, false, false, false);
style.sliderRange.Draw(rect, GUIContent.none, false, false, false, false);
GUI.backgroundColor = previous;
};

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

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

isListFoldout = EditorGUI.Foldout(new Rect(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight), isListFoldout, label);
EditorGUI.indentLevel++;
if (isListFoldout)
{
Expand Down Expand Up @@ -298,16 +296,14 @@ private void OnSliderBoxGUI(Rect groupRect, SerializedProperty property, List<We
}
private void DrawSliderBox(Rect area, List<WeightSlider> weights, int activeWeight, bool isCompact)
{
Style.sliderBG.Draw(area, GUIContent.none, false, false, false, false);
style.sliderBG.Draw(area, GUIContent.none, false, false, false, false);
var tempColor = GUI.backgroundColor;
for (int i = 0; i < weights.Count; i++)
{
// Get the weight and update any missing drawers
WeightSlider weight = weights[i];
if (weight.labelDrawer == null)
weight.labelDrawer = this;
Color color = style.Colors[i % style.Colors.Length];

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

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

void IRistBoxLabelDrawer.DrawLabel(Rect position, RandomListEditorStyle style, SerializedProperty property, float weight, bool isMini)
{
var name = property.hasVisibleChildren ? property.displayName : property.GetValueName();
var label = isMini
? name
: string.Format("{0}\n{1:0}%", name, weight * 100);

style.sliderText.Draw(position, label, false, false, false, false);
}

public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
if (!isListFoldout)
Expand Down Expand Up @@ -411,6 +398,12 @@ private static List<WeightSlider> CreateSliders(SerializedProperty property, Rec
SerializedProperty itemListProperty = property.FindPropertyRelative("_list");
SerializedProperty weightListProperty = property.FindPropertyRelative("_weights");

var baseType = property.GetSerializedType();
var elmType = baseType.GetGenericArguments()[0];
IRistBoxLabelDrawer drawer;
if (style.Drawers == null || elmType == null || !style.Drawers.TryGetValue(elmType, out drawer))
drawer = style.Drawers[typeof(void)];

float weightOffset = 0;
for (int i = 0; i < itemListProperty.arraySize; i++)
{
Expand All @@ -426,27 +419,39 @@ private static List<WeightSlider> CreateSliders(SerializedProperty property, Rec
startWeight = weightOffset,
endWeight = weightOffset + weightProperty.floatValue,
sumWeight = totalWeightProperty.floatValue,
labelDrawer = drawer,
};

// Calculate the rect
float buttonSize = 10;
info.rangePosition = new Rect(sliderRect.x + info.startPercentage * sliderRect.width, sliderRect.y, info.weightPercentage * sliderRect.width, sliderRect.height);
info.buttonPosition = new Rect(info.rangePosition.x - (buttonSize / 2), info.rangePosition.y, buttonSize, info.rangePosition.height);

infos.Add(info);

var type = itemProperty.GetSerializedType();
SliderLabelDrawers.TryGetValue(type, out info.labelDrawer);

weightOffset += info.weight;
}


return infos;
}
}

internal class ColorRistDrawer : IRistBoxLabelDrawer
internal sealed class BasicRistDrawer : IRistBoxLabelDrawer
{
public void DrawLabel(Rect position, RandomListDrawerStyle style, SerializedProperty property, float weight, bool isMini)
{
var name = property.hasVisibleChildren ? property.displayName : property.GetValueName();
var label = isMini
? name
: string.Format("{0}\n{1:0}%", name, weight * 100);

style.sliderText.Draw(position, label, false, false, false, false);
}
}

internal sealed class ColorRistDrawer : IRistBoxLabelDrawer
{
public void DrawLabel(Rect position, RandomListEditorStyle style, SerializedProperty property, float weight, bool isMini)
public void DrawLabel(Rect position, RandomListDrawerStyle style, SerializedProperty property, float weight, bool isMini)
{
var label = isMini
? property.GetValueName()
Expand All @@ -463,11 +468,11 @@ public void DrawLabel(Rect position, RandomListEditorStyle style, SerializedProp
}
}

internal class AnimationCurveRistDrawer : IRistBoxLabelDrawer
internal sealed class AnimationCurveRistDrawer : IRistBoxLabelDrawer
{
const float PADDING = 2;

public void DrawLabel(Rect position, RandomListEditorStyle style, SerializedProperty property, float weight, bool isMini)
public void DrawLabel(Rect position, RandomListDrawerStyle style, SerializedProperty property, float weight, bool isMini)
{
var label = isMini
? ""
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "com.lachee.utilities",
"version": "1.3.3",
"version": "1.3.4",
"displayName": "Lachee's Utilities",
"description": "Bunch of utility functionality",
"unity": "2019.1",
Expand Down

0 comments on commit fd43673

Please sign in to comment.