Add drawers for unity built-in types

This commit is contained in:
VladV 2022-01-07 20:21:36 +03:00
parent c65805ae24
commit a298305569
9 changed files with 265 additions and 86 deletions

View File

@ -1,41 +0,0 @@
using TriInspector;
using TriInspector.Drawers;
using UnityEditor;
using UnityEngine;
[assembly: RegisterTriValueDrawer(typeof(BoolDrawer), TriDrawerOrder.Fallback)]
namespace TriInspector.Drawers
{
public class BoolDrawer : TriValueDrawer<bool>
{
public override TriElement CreateElement(TriValue<bool> propertyValue, TriElement next)
{
if (propertyValue.Property.TryGetSerializedProperty(out _))
{
return next;
}
return base.CreateElement(propertyValue, next);
}
public override float GetHeight(float width, TriValue<bool> propertyValue, TriElement next)
{
return EditorGUIUtility.singleLineHeight;
}
public override void OnGUI(Rect position, TriValue<bool> propertyValue, TriElement next)
{
var value = propertyValue.Value;
EditorGUI.BeginChangeCheck();
value = EditorGUI.Toggle(position, propertyValue.Property.DisplayNameContent, value);
if (EditorGUI.EndChangeCheck())
{
propertyValue.Value = value;
}
}
}
}

View File

@ -0,0 +1,39 @@
using UnityEditor;
using UnityEngine;
namespace TriInspector.Drawers
{
public abstract class BuiltinDrawerBase<T> : TriValueDrawer<T>
{
public sealed override TriElement CreateElement(TriValue<T> propertyValue, TriElement next)
{
if (propertyValue.Property.TryGetSerializedProperty(out _))
{
return next;
}
return base.CreateElement(propertyValue, next);
}
public sealed override float GetHeight(float width, TriValue<T> propertyValue, TriElement next)
{
return EditorGUIUtility.singleLineHeight;
}
public sealed override void OnGUI(Rect position, TriValue<T> propertyValue, TriElement next)
{
var value = propertyValue.Value;
EditorGUI.BeginChangeCheck();
value = OnValueGUI(position, propertyValue.Property.DisplayNameContent, value);
if (EditorGUI.EndChangeCheck())
{
propertyValue.Value = value;
}
}
protected abstract T OnValueGUI(Rect position, GUIContent label, T value);
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 1099ac0317654a238e210dec14c7ea89
timeCreated: 1641570232

View File

@ -0,0 +1,164 @@
using System;
using TriInspector;
using TriInspector.Drawers;
using UnityEditor;
using UnityEngine;
[assembly: RegisterTriValueDrawer(typeof(IntegerDrawer), TriDrawerOrder.Fallback)]
[assembly: RegisterTriValueDrawer(typeof(BooleanDrawer), TriDrawerOrder.Fallback)]
[assembly: RegisterTriValueDrawer(typeof(FloatDrawer), TriDrawerOrder.Fallback)]
[assembly: RegisterTriValueDrawer(typeof(StringDrawer), TriDrawerOrder.Fallback)]
[assembly: RegisterTriValueDrawer(typeof(ColorDrawer), TriDrawerOrder.Fallback)]
[assembly: RegisterTriValueDrawer(typeof(LayerMaskDrawer), TriDrawerOrder.Fallback)]
[assembly: RegisterTriValueDrawer(typeof(EnumDrawer), TriDrawerOrder.Fallback)]
[assembly: RegisterTriValueDrawer(typeof(Vector2Drawer), TriDrawerOrder.Fallback)]
[assembly: RegisterTriValueDrawer(typeof(Vector3Drawer), TriDrawerOrder.Fallback)]
[assembly: RegisterTriValueDrawer(typeof(Vector4Drawer), TriDrawerOrder.Fallback)]
[assembly: RegisterTriValueDrawer(typeof(RectDrawer), TriDrawerOrder.Fallback)]
[assembly: RegisterTriValueDrawer(typeof(AnimationCurveDrawer), TriDrawerOrder.Fallback)]
[assembly: RegisterTriValueDrawer(typeof(BoundsDrawer), TriDrawerOrder.Fallback)]
[assembly: RegisterTriValueDrawer(typeof(GradientDrawer), TriDrawerOrder.Fallback)]
[assembly: RegisterTriValueDrawer(typeof(Vector2IntDrawer), TriDrawerOrder.Fallback)]
[assembly: RegisterTriValueDrawer(typeof(Vector3IntDrawer), TriDrawerOrder.Fallback)]
[assembly: RegisterTriValueDrawer(typeof(RectIntDrawer), TriDrawerOrder.Fallback)]
[assembly: RegisterTriValueDrawer(typeof(BoundsIntDrawer), TriDrawerOrder.Fallback)]
namespace TriInspector.Drawers
{
public class BooleanDrawer : BuiltinDrawerBase<bool>
{
protected override bool OnValueGUI(Rect position, GUIContent label, bool value)
{
return EditorGUI.Toggle(position, label, value);
}
}
public class IntegerDrawer : BuiltinDrawerBase<int>
{
protected override int OnValueGUI(Rect position, GUIContent label, int value)
{
return EditorGUI.IntField(position, label, value);
}
}
public class FloatDrawer : BuiltinDrawerBase<float>
{
protected override float OnValueGUI(Rect position, GUIContent label, float value)
{
return EditorGUI.FloatField(position, label, value);
}
}
public class ColorDrawer : BuiltinDrawerBase<Color>
{
protected override Color OnValueGUI(Rect position, GUIContent label, Color value)
{
return EditorGUI.ColorField(position, label, value);
}
}
public class LayerMaskDrawer : BuiltinDrawerBase<LayerMask>
{
protected override LayerMask OnValueGUI(Rect position, GUIContent label, LayerMask value)
{
return EditorGUI.LayerField(position, label, value);
}
}
public class EnumDrawer : BuiltinDrawerBase<Enum>
{
protected override Enum OnValueGUI(Rect position, GUIContent label, Enum value)
{
return EditorGUI.EnumPopup(position, label, value);
}
}
public class Vector2Drawer : BuiltinDrawerBase<Vector2>
{
protected override Vector2 OnValueGUI(Rect position, GUIContent label, Vector2 value)
{
return EditorGUI.Vector2Field(position, label, value);
}
}
public class Vector3Drawer : BuiltinDrawerBase<Vector3>
{
protected override Vector3 OnValueGUI(Rect position, GUIContent label, Vector3 value)
{
return EditorGUI.Vector3Field(position, label, value);
}
}
public class Vector4Drawer : BuiltinDrawerBase<Vector4>
{
protected override Vector4 OnValueGUI(Rect position, GUIContent label, Vector4 value)
{
return EditorGUI.Vector4Field(position, label, value);
}
}
public class RectDrawer : BuiltinDrawerBase<Rect>
{
protected override Rect OnValueGUI(Rect position, GUIContent label, Rect value)
{
return EditorGUI.RectField(position, label, value);
}
}
public class AnimationCurveDrawer : BuiltinDrawerBase<AnimationCurve>
{
protected override AnimationCurve OnValueGUI(Rect position, GUIContent label, AnimationCurve value)
{
return EditorGUI.CurveField(position, label, value);
}
}
public class BoundsDrawer : BuiltinDrawerBase<Bounds>
{
protected override Bounds OnValueGUI(Rect position, GUIContent label, Bounds value)
{
return EditorGUI.BoundsField(position, label, value);
}
}
public class GradientDrawer : BuiltinDrawerBase<Gradient>
{
protected override Gradient OnValueGUI(Rect position, GUIContent label, Gradient value)
{
return EditorGUI.GradientField(position, label, value);
}
}
public class Vector2IntDrawer : BuiltinDrawerBase<Vector2Int>
{
protected override Vector2Int OnValueGUI(Rect position, GUIContent label, Vector2Int value)
{
return EditorGUI.Vector2IntField(position, label, value);
}
}
public class Vector3IntDrawer : BuiltinDrawerBase<Vector3Int>
{
protected override Vector3Int OnValueGUI(Rect position, GUIContent label, Vector3Int value)
{
return EditorGUI.Vector3IntField(position, label, value);
}
}
public class RectIntDrawer : BuiltinDrawerBase<RectInt>
{
protected override RectInt OnValueGUI(Rect position, GUIContent label, RectInt value)
{
return EditorGUI.RectIntField(position, label, value);
}
}
public class BoundsIntDrawer : BuiltinDrawerBase<BoundsInt>
{
protected override BoundsInt OnValueGUI(Rect position, GUIContent label, BoundsInt value)
{
return EditorGUI.BoundsIntField(position, label, value);
}
}
}

View File

@ -1,42 +0,0 @@
using TriInspector;
using TriInspector.Drawers;
using TriInspector.Elements;
using UnityEditor;
using UnityEngine;
[assembly: RegisterTriValueDrawer(typeof(FloatDrawer), TriDrawerOrder.Fallback)]
namespace TriInspector.Drawers
{
public class FloatDrawer : TriValueDrawer<float>
{
public override TriElement CreateElement(TriValue<float> propertyValue, TriElement next)
{
if (propertyValue.Property.TryGetSerializedProperty(out _))
{
return next;
}
return base.CreateElement(propertyValue, next);
}
public override float GetHeight(float width, TriValue<float> propertyValue, TriElement next)
{
return EditorGUIUtility.singleLineHeight;
}
public override void OnGUI(Rect position, TriValue<float> propertyValue, TriElement next)
{
var value = propertyValue.Value;
EditorGUI.BeginChangeCheck();
value = EditorGUI.FloatField(position, propertyValue.Property.DisplayNameContent, value);
if (EditorGUI.EndChangeCheck())
{
propertyValue.Value = value;
}
}
}
}

View File

@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: 519fae04e2154ae69d75d5f131a8678c
timeCreated: 1639384497

View File

@ -0,0 +1,56 @@
using TriInspector;
using TriInspector.Drawers;
using UnityEditor;
using UnityEngine;
[assembly: RegisterTriValueDrawer(typeof(ObjectReferenceDrawer<>), TriDrawerOrder.Fallback)]
namespace TriInspector.Drawers
{
public class ObjectReferenceDrawer<T> : TriValueDrawer<T>
where T : Object
{
public override TriElement CreateElement(TriValue<T> value, TriElement next)
{
if (value.Property.TryGetSerializedProperty(out _))
{
return next;
}
return new ObjectReferenceDrawerElement(value);
}
private class ObjectReferenceDrawerElement : TriElement
{
private readonly TriValue<T> _propertyValue;
public ObjectReferenceDrawerElement(TriValue<T> propertyValue)
{
_propertyValue = propertyValue;
}
public override float GetHeight(float width)
{
return EditorGUIUtility.singleLineHeight;
}
public override void OnGUI(Rect position)
{
var value = _propertyValue.Value;
EditorGUI.BeginChangeCheck();
var allowSceneObjects = _propertyValue.Property.PropertyTree.TargetObjects[0] is var targetObject &&
targetObject != null && !EditorUtility.IsPersistent(targetObject);
value = (T) EditorGUI.ObjectField(position, _propertyValue.Property.DisplayNameContent, value,
_propertyValue.Property.FieldType, allowSceneObjects);
if (EditorGUI.EndChangeCheck())
{
_propertyValue.Value = value;
}
}
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 95268b41deb942b8882969bf23911eaf
timeCreated: 1641571162