using UnityEngine; using UnityEditor; using System.Collections.Generic; namespace Unity.DemoTeam.DigitalHuman { [CustomEditor(typeof(SkinAttachment)), CanEditMultipleObjects] public class SkinAttachmentEditor : Editor { private Editor attachmentTargetEditor; private HashSet attachmentTargetSet = new HashSet(); public override void OnInspectorGUI() { attachmentTargetSet.Clear(); if (target == null) return; if (targets.Length == 1) { var attachment = target as SkinAttachment; if (attachment == null) return; EditorGUILayout.HelpBox(attachment.attached ? "Currently attached to " + attachment.target : "Currently detached.", MessageType.Info); DrawGUIAttachDetach(attachment, attachmentTargetSet); CommitTargetChanges(attachmentTargetSet); EditorGUILayout.Separator(); base.OnInspectorGUI(); var attachmentTarget = (target as SkinAttachment).target; if (attachmentTarget != null) { EditorGUILayout.Separator(); Editor.CreateCachedEditor(attachmentTarget, null, ref attachmentTargetEditor); attachmentTargetEditor.DrawHeader(); attachmentTargetEditor.OnInspectorGUI(); } } else { EditorGUILayout.HelpBox("Multiple attachments selected.", MessageType.Warning); foreach (var target in targets) { var attachment = target as SkinAttachment; if (attachment == null) continue; EditorGUILayout.BeginHorizontal(); EditorGUILayout.ObjectField(attachment, typeof(SkinAttachment), false); DrawGUIAttachDetach(attachment, attachmentTargetSet); CommitTargetChanges(attachmentTargetSet); EditorGUILayout.EndHorizontal(); } } } public static void CommitTargetChanges(HashSet attachmentTargetSet) { foreach (var attachmentTarget in attachmentTargetSet) { if (attachmentTarget != null) attachmentTarget.CommitSubjectsIfRequired(); } } public static void DrawGUIAttachDetach(SkinAttachment attachment, HashSet attachmentTargetSet) { EditorGUILayout.BeginVertical(); DrawGUIAttach(attachment, attachmentTargetSet); DrawGUIDetach(attachment, attachmentTargetSet); EditorGUILayout.EndVertical(); } public static void DrawGUIAttach(SkinAttachment attachment, HashSet attachmentTargetSet) { EditorGUI.BeginDisabledGroup(attachment.attached); { if (GUILayout.Button("Attach")) { attachmentTargetSet.Add(attachment.targetActive); attachment.Attach(storePositionRotation: true); attachmentTargetSet.Add(attachment.targetActive); } } EditorGUI.EndDisabledGroup(); } public static void DrawGUIDetach(SkinAttachment attachment, HashSet attachmentTargetSet) { EditorGUI.BeginDisabledGroup(!attachment.attached); { EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Detach")) { attachmentTargetSet.Add(attachment.targetActive); attachment.Detach(revertPositionRotation: true); } if (GUILayout.Button("+ Hold", GUILayout.ExpandWidth(false))) { attachmentTargetSet.Add(attachment.targetActive); attachment.Detach(revertPositionRotation: false); } EditorGUILayout.EndHorizontal(); } EditorGUI.EndDisabledGroup(); } } }