modular-avatar/Packages/nadena.dev.modular-avatar/Editor/Inspector/BoneProxyEditor.cs

95 lines
3.1 KiB
C#
Raw Normal View History

using UnityEditor;
using UnityEngine;
2022-11-11 12:39:58 +08:00
using static nadena.dev.modular_avatar.core.editor.Localization;
2022-11-11 12:39:58 +08:00
namespace nadena.dev.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));
if (virtProp.objectReferenceValue is Transform targetTransform)
{
var validationResult = BoneProxyProcessor.ValidateTarget(parentAvatar, targetTransform);
if (validationResult != BoneProxyProcessor.ValidationResult.OK)
{
EditorGUILayout.HelpBox(S("boneproxy.err." + validationResult), MessageType.Error);
}
}
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");
var xform = ((TempObjRef) objRefs[i]).target;
if (RuntimeUtil.FindAvatarInParents(xform)?.gameObject != parentAvatar) continue;
t.target = xform;
2022-10-23 04:04:25 +08:00
}
}
}
2022-10-17 05:05:26 +08:00
foldout = EditorGUILayout.Foldout(foldout, G("boneproxy.foldout.advanced"));
if (foldout)
{
EditorGUI.indentLevel++;
DrawDefaultInspector();
EditorGUI.indentLevel--;
}
2022-10-17 05:05:26 +08:00
Localization.ShowLanguageUI();
}
}
}