public override void OnInspectorGUI()
        {
            if (needToReset)
            {
                follower.HandleReset(null);
                needToReset = false;
            }
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(skeletonRenderer);
            EditorGUILayout.PropertyField(slotName, new GUIContent("Slot"));

            if (EditorGUI.EndChangeCheck())
            {
                serializedObject.ApplyModifiedProperties();
                needToReset = true;
            }

            bool hasBone = follower.GetComponent <BoneFollower>() != null;

            EditorGUI.BeginDisabledGroup(hasBone || follower.Slot == null);
            {
                if (GUILayout.Button(new GUIContent("Add Bone Follower", SpineEditorUtilities.Icons.bone)))
                {
                    var boneFollower = follower.gameObject.AddComponent <BoneFollower>();
                    boneFollower.boneName = follower.Slot.Data.BoneData.Name;
                }
            }
            EditorGUI.EndDisabledGroup();



            //GUILayout.Space(20);
            GUILayout.Label("Attachment Names", EditorStyles.boldLabel);
            foreach (var kp in follower.attachmentNameTable)
            {
                string name          = kp.Value;
                var    collider      = follower.colliderTable[kp.Key];
                bool   isPlaceholder = name != kp.Key.Name;
                collider.enabled = EditorGUILayout.ToggleLeft(new GUIContent(!isPlaceholder ? name : name + " [" + kp.Key.Name + "]", isPlaceholder ? SpineEditorUtilities.Icons.skinPlaceholder : SpineEditorUtilities.Icons.boundingBox), collider.enabled);
            }
        }
        public override void OnInspectorGUI()
        {
                        #if !NEW_PREFAB_SYSTEM
            bool isInspectingPrefab = (PrefabUtility.GetPrefabType(target) == PrefabType.Prefab);
                        #else
            bool isInspectingPrefab = false;
                        #endif

            // Try to auto-assign SkeletonRenderer field.
            if (skeletonRenderer.objectReferenceValue == null)
            {
                var foundSkeletonRenderer = follower.GetComponentInParent <SkeletonRenderer>();
                if (foundSkeletonRenderer != null)
                {
                    Debug.Log("BoundingBoxFollower automatically assigned: " + foundSkeletonRenderer.gameObject.name);
                }
                else if (Event.current.type == EventType.Repaint)
                {
                    Debug.Log("No Spine GameObject detected. Make sure to set this GameObject as a child of the Spine GameObject; or set BoundingBoxFollower's 'Skeleton Renderer' field in the inspector.");
                }

                skeletonRenderer.objectReferenceValue = foundSkeletonRenderer;
                serializedObject.ApplyModifiedProperties();
            }

            var skeletonRendererValue = skeletonRenderer.objectReferenceValue as SkeletonRenderer;
            if (skeletonRendererValue != null && skeletonRendererValue.gameObject == follower.gameObject)
            {
                using (new EditorGUILayout.VerticalScope(EditorStyles.helpBox)) {
                    EditorGUILayout.HelpBox("It's ideal to add BoundingBoxFollower to a separate child GameObject of the Spine GameObject.", MessageType.Warning);

                    if (GUILayout.Button(new GUIContent("Move BoundingBoxFollower to new GameObject", Icons.boundingBox), GUILayout.Height(50f)))
                    {
                        AddBoundingBoxFollowerChild(skeletonRendererValue, follower);
                        DestroyImmediate(follower);
                        return;
                    }
                }
                EditorGUILayout.Space();
            }

            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(skeletonRenderer);
            EditorGUILayout.PropertyField(slotName, new GUIContent("Slot"));
            if (EditorGUI.EndChangeCheck())
            {
                serializedObject.ApplyModifiedProperties();
                                #if !NEW_PREFAB_SYSTEM
                if (!isInspectingPrefab)
                {
                    rebuildRequired = true;
                }
                                #endif
            }

            using (new SpineInspectorUtility.LabelWidthScope(150f)) {
                EditorGUI.BeginChangeCheck();
                EditorGUILayout.PropertyField(isTrigger);
                bool triggerChanged = EditorGUI.EndChangeCheck();

                EditorGUI.BeginChangeCheck();
                EditorGUILayout.PropertyField(clearStateOnDisable, new GUIContent(clearStateOnDisable.displayName, "Enable this if you are pooling your Spine GameObject"));
                bool clearStateChanged = EditorGUI.EndChangeCheck();

                if (clearStateChanged || triggerChanged)
                {
                    serializedObject.ApplyModifiedProperties();
                    if (triggerChanged)
                    {
                        foreach (var col in follower.colliderTable.Values)
                        {
                            col.isTrigger = isTrigger.boolValue;
                        }
                    }
                }
            }

            if (isInspectingPrefab)
            {
                follower.colliderTable.Clear();
                follower.nameTable.Clear();
                EditorGUILayout.HelpBox("BoundingBoxAttachments cannot be previewed in prefabs.", MessageType.Info);

                // How do you prevent components from being saved into the prefab? No such HideFlag. DontSaveInEditor | DontSaveInBuild does not work. DestroyImmediate does not work.
                var collider = follower.GetComponent <PolygonCollider2D>();
                if (collider != null)
                {
                    Debug.LogWarning("Found BoundingBoxFollower collider components in prefab. These are disposed and regenerated at runtime.");
                }
            }
            else
            {
                using (new SpineInspectorUtility.BoxScope()) {
                    if (debugIsExpanded = EditorGUILayout.Foldout(debugIsExpanded, "Debug Colliders"))
                    {
                        EditorGUI.indentLevel++;
                        EditorGUILayout.LabelField(string.Format("Attachment Names ({0} PolygonCollider2D)", follower.colliderTable.Count));
                        EditorGUI.BeginChangeCheck();
                        foreach (var kp in follower.nameTable)
                        {
                            string attachmentName = kp.Value;
                            var    collider       = follower.colliderTable[kp.Key];
                            bool   isPlaceholder  = attachmentName != kp.Key.Name;
                            collider.enabled = EditorGUILayout.ToggleLeft(new GUIContent(!isPlaceholder ? attachmentName : string.Format("{0} [{1}]", attachmentName, kp.Key.Name), isPlaceholder ? Icons.skinPlaceholder : Icons.boundingBox), collider.enabled);
                        }
                        sceneRepaintRequired |= EditorGUI.EndChangeCheck();
                        EditorGUI.indentLevel--;
                    }
                }
            }

            bool hasBoneFollower = follower.GetComponent <BoneFollower>() != null;
            if (!hasBoneFollower)
            {
                bool buttonDisabled = follower.Slot == null;
                using (new EditorGUI.DisabledGroupScope(buttonDisabled)) {
                    addBoneFollower |= SpineInspectorUtility.LargeCenteredButton(AddBoneFollowerLabel, true);
                    EditorGUILayout.Space();
                }
            }


            if (Event.current.type == EventType.Repaint)
            {
                if (addBoneFollower)
                {
                    var boneFollower = follower.gameObject.AddComponent <BoneFollower>();
                    boneFollower.skeletonRenderer = skeletonRendererValue;
                    boneFollower.SetBone(follower.Slot.Data.BoneData.Name);
                    addBoneFollower = false;
                }

                if (sceneRepaintRequired)
                {
                    SceneView.RepaintAll();
                    sceneRepaintRequired = false;
                }

                if (rebuildRequired)
                {
                    follower.Initialize();
                    rebuildRequired = false;
                }
            }
        }
Exemplo n.º 3
0
        public override void OnInspectorGUI()
        {
            bool isInspectingPrefab = (PrefabUtility.GetPrefabType(target) == PrefabType.Prefab);
            bool repaintEvent       = UnityEngine.Event.current.type == EventType.Repaint;

            if (rebuildRequired)
            {
                follower.HandleRebuild(null);
                rebuildRequired = false;
            }

            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(skeletonRenderer);
            EditorGUILayout.PropertyField(slotName, new GUIContent("Slot"));
            if (EditorGUI.EndChangeCheck())
            {
                serializedObject.ApplyModifiedProperties();
                if (!isInspectingPrefab)
                {
                    rebuildRequired = true;
                }
            }

            bool hasBoneFollower = follower.GetComponent <BoneFollower>() != null;

            using (new EditorGUI.DisabledGroupScope(hasBoneFollower || follower.Slot == null)) {
                if (GUILayout.Button(new GUIContent("Add Bone Follower", SpineEditorUtilities.Icons.bone)))
                {
                    addBoneFollower = true;
                }
            }

            if (isInspectingPrefab)
            {
                follower.colliderTable.Clear();
                follower.attachmentNameTable.Clear();
                EditorGUILayout.HelpBox("BoundingBoxAttachments cannot be previewed in prefabs.", MessageType.Info);

                // How do you prevent components from being saved into the prefab? No such HideFlag. DontSaveInEditor | DontSaveInBuild does not work. DestroyImmediate does not work.
                var collider = follower.GetComponent <PolygonCollider2D>();
                if (collider != null)
                {
                    Debug.LogWarning("Found BoundingBoxFollower collider components in prefab. These are disposed and regenerated at runtime.");
                }
            }
            else
            {
                EditorGUILayout.LabelField(string.Format("Attachment Names ({0} PolygonCollider2D)", follower.colliderTable.Count), EditorStyles.boldLabel);
                EditorGUI.BeginChangeCheck();
                foreach (var kp in follower.attachmentNameTable)
                {
                    string attachmentName = kp.Value;
                    var    collider       = follower.colliderTable[kp.Key];
                    bool   isPlaceholder  = attachmentName != kp.Key.Name;
                    collider.enabled = EditorGUILayout.ToggleLeft(new GUIContent(!isPlaceholder ? attachmentName : attachmentName + " [" + kp.Key.Name + "]", isPlaceholder ? SpineEditorUtilities.Icons.skinPlaceholder : SpineEditorUtilities.Icons.boundingBox), collider.enabled);
                }
                if (EditorGUI.EndChangeCheck())
                {
                    SceneView.RepaintAll();
                }

                if (!Application.isPlaying)
                {
                    EditorGUILayout.HelpBox("\nAt runtime, BoundingBoxFollower enables and disables PolygonCollider2Ds based on the currently active attachment in the slot.\n\nCheckboxes in Edit Mode are only for preview. Checkbox states are not saved.\n", MessageType.Info);
                }
            }

            if (addBoneFollower && repaintEvent)
            {
                var boneFollower = follower.gameObject.AddComponent <BoneFollower>();
                boneFollower.boneName = follower.Slot.Data.BoneData.Name;
                addBoneFollower       = false;
            }
        }