modular-avatar/Packages/net.fushizen.modular-avatar/Editor/Inspector/AvatarObjectReferenceDrawer.cs

133 lines
5.6 KiB
C#
Raw Normal View History

using UnityEditor;
using UnityEngine;
namespace net.fushizen.modular_avatar.core.editor
{
[CustomPropertyDrawer(typeof(AvatarObjectReference))]
public class AvatarObjectReferenceDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
if (!CustomGUI(position, property, label))
{
var xButtonSize = EditorStyles.miniButtonRight.CalcSize(new GUIContent("x"));
var xButtonRect = new Rect(position.xMax - xButtonSize.x, position.y, xButtonSize.x, position.height);
position = new Rect(position.x, position.y, position.width - xButtonSize.x, position.height);
property = property.FindPropertyRelative(nameof(AvatarObjectReference.referencePath));
position = EditorGUI.PrefixLabel(position, label);
2022-10-20 10:42:33 +08:00
using (var scope = new ZeroIndentScope())
{
EditorGUI.LabelField(position,
string.IsNullOrEmpty(property.stringValue) ? "(null)" : property.stringValue);
}
}
}
private bool CustomGUI(Rect position, SerializedProperty property, GUIContent label)
{
var color = GUI.contentColor;
property = property.FindPropertyRelative(nameof(AvatarObjectReference.referencePath));
try
{
// Find containing object, and from that the avatar
if (property.serializedObject == null || property.serializedObject.targetObjects.Length != 1)
return false;
var obj = property.serializedObject.targetObject as Component;
if (obj == null) return false;
var transform = obj.transform;
var avatar = RuntimeUtil.FindAvatarInParents(transform);
if (avatar == null) return false;
bool isRoot = property.stringValue == AvatarObjectReference.AVATAR_ROOT;
bool isNull = string.IsNullOrEmpty(property.stringValue);
Transform target;
if (isNull) target = null;
else if (isRoot) target = avatar.transform;
else target = avatar.transform.Find(property.stringValue);
var labelRect = position;
position = EditorGUI.PrefixLabel(position, label);
labelRect.width = position.x - labelRect.x;
var nullContent = GUIContent.none;
2022-10-20 10:42:33 +08:00
using (var scope = new ZeroIndentScope())
{
2022-10-20 10:42:33 +08:00
if (target != null || isNull)
{
2022-10-20 10:42:33 +08:00
EditorGUI.BeginChangeCheck();
var newTarget = EditorGUI.ObjectField(position, nullContent, target, typeof(Transform), true);
if (EditorGUI.EndChangeCheck())
{
2022-10-20 10:42:33 +08:00
if (newTarget == null)
{
property.stringValue = "";
}
else if (newTarget == avatar.transform)
{
property.stringValue = AvatarObjectReference.AVATAR_ROOT;
}
else
{
var relPath =
RuntimeUtil.RelativePath(avatar.gameObject, ((Transform) newTarget).gameObject);
if (relPath == null) return true;
property.stringValue = relPath;
}
}
}
2022-10-20 10:42:33 +08:00
else
{
// For some reason, this color change retroactively affects the prefix label above, so draw our own
// label as well (we still want the prefix label for highlights, etc).
EditorGUI.LabelField(labelRect, label);
2022-10-20 10:42:33 +08:00
GUI.contentColor = new Color(0, 0, 0, 0);
EditorGUI.BeginChangeCheck();
var newTarget = EditorGUI.ObjectField(position, nullContent, target, typeof(Transform), true);
GUI.contentColor = color;
2022-10-20 10:42:33 +08:00
if (EditorGUI.EndChangeCheck())
{
2022-10-20 10:42:33 +08:00
if (newTarget == null)
{
property.stringValue = "";
}
else if (newTarget == avatar.transform)
{
property.stringValue = AvatarObjectReference.AVATAR_ROOT;
}
else
{
var relPath =
RuntimeUtil.RelativePath(avatar.gameObject, ((Transform) newTarget).gameObject);
if (relPath == null) return true;
property.stringValue = relPath;
}
}
else
{
2022-10-20 10:42:33 +08:00
GUI.contentColor = Color.red;
EditorGUI.LabelField(position, property.stringValue);
}
}
2022-10-20 10:42:33 +08:00
return true;
}
}
finally
{
GUI.contentColor = color;
}
}
}
}