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

84 lines
2.5 KiB
C#
Raw Normal View History

using UnityEditor;
using UnityEngine;
2022-10-17 05:05:26 +08:00
using static net.fushizen.modular_avatar.core.editor.Localization;
namespace net.fushizen.modular_avatar.core.editor
{
internal class TempObjRef : ScriptableObject
{
public Transform target;
}
[CustomEditor(typeof(ModularAvatarBoneProxy))]
[CanEditMultipleObjects]
internal class BoneProxyEditor : MAEditorBase
{
private bool foldout = false;
private Object[] objRefs;
private void OnEnable()
{
objRefs = new Object[targets.Length];
for (int i = 0; i < targets.Length; i++)
{
2022-10-21 11:53:46 +08:00
objRefs[i] = CreateInstance<TempObjRef>();
}
}
protected override void OnInnerInspectorGUI()
{
GameObject parentAvatar = null;
2022-10-21 11:53:46 +08:00
bool suppressTarget = false;
for (int i = 0; i < targets.Length; i++)
{
var t = (ModularAvatarBoneProxy) targets[i];
var av = RuntimeUtil.FindAvatarInParents(t.transform);
if (av != null && parentAvatar == null) parentAvatar = av.gameObject;
if (av == null || parentAvatar != av.gameObject)
{
2022-10-21 11:53:46 +08:00
suppressTarget = true;
break;
}
((TempObjRef) objRefs[i]).target = t.target;
}
2022-10-21 11:53:46 +08:00
if (suppressTarget)
{
foldout = true;
}
else
{
var virtObj = new SerializedObject(objRefs);
var virtProp = virtObj.FindProperty(nameof(TempObjRef.target));
2022-10-23 04:04:25 +08:00
EditorGUI.BeginChangeCheck();
2022-10-23 04:04:25 +08:00
EditorGUILayout.PropertyField(virtProp, G("boneproxy.target"));
if (EditorGUI.EndChangeCheck())
{
2022-10-23 04:04:25 +08:00
virtObj.ApplyModifiedPropertiesWithoutUndo();
for (int i = 0; i < targets.Length; i++)
{
var t = (ModularAvatarBoneProxy) targets[i];
Undo.RecordObjects(targets, "Set targets");
t.target = ((TempObjRef) objRefs[i]).target;
}
}
}
2022-10-17 05:05:26 +08:00
foldout = EditorGUILayout.Foldout(foldout, G("boneproxy.foldout.advanced"));
if (foldout)
{
EditorGUI.indentLevel++;
base.OnInspectorGUI();
EditorGUI.indentLevel--;
}
2022-10-17 05:05:26 +08:00
Localization.ShowLanguageUI();
}
}
}