using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; namespace TweenAnimation { [CustomEditor(typeof(TweenController), false)] public class TweenControllerInspector : Editor { private TweenController controller; public override void OnInspectorGUI() { controller = target as TweenController; if (controller == null) return; GUI.changed = false; // automatically play on enable Rect autoRect = GUILayoutUtility.GetRect(0f, 20f); autoRect.width = 150; GUI.Label(autoRect, "Auto Play When Enabled"); autoRect.x = 190; controller.autoPlayOnEnable = GUI.Toggle(autoRect, controller.autoPlayOnEnable, ""); // default animation if(controller.autoPlayOnEnable) { Rect defaultRect = GUILayoutUtility.GetRect(0f, 20f); float width = defaultRect.width; defaultRect.width = 150; GUI.Label(defaultRect, "Default Animation"); defaultRect.x = 190; defaultRect.width = width - 172; int index = -1; int count = 0; for(int i = 0; i < controller.animations.Count;i++) { if (controller.animations[i] != null) ++count; } string[] displayName = new string[count]; for(int i = 0, j = 0; i < controller.animations.Count; ++i) { if (controller.animations[i] == null) continue; if (controller.animations[i].name == controller.defaultAnimation) index = j; displayName[j++] = controller.animations[i].name; } index = EditorGUI.Popup(defaultRect, index, displayName); if(index >= 0 && index < displayName.Length) controller.defaultAnimation = displayName[index]; } Rect hintRect = GUILayoutUtility.GetRect(0f, 20f); GUI.Label(hintRect, "Animations"); // animation list if(controller.animations.Count > 0) { List deleted = new List(); for (int i = 0; i < controller.animations.Count; ++i) { var animation = controller.animations[i]; Rect rect = GUILayoutUtility.GetRect(0f, 20f); Rect buttonRect = rect; buttonRect.width = 20; if (GUI.Button(buttonRect, "-")) { deleted.Add(i); break; } Rect nameRect = rect; nameRect.x = 60; nameRect.width = 100; if (animation) GUI.Label(nameRect, animation.name); else GUI.Label(nameRect, ""); Rect animationRect = rect; animationRect.width = rect.width - 178; animationRect.x = 190; var newAnimation = EditorGUI.ObjectField(animationRect, animation, typeof(TweenAnimation), true) as TweenAnimation; if (newAnimation != animation && controller.animations.Contains(newAnimation)) { Debug.LogError("Tween Animation'" + newAnimation + "' already added."); } else controller.animations[i] = newAnimation; } if (deleted.Count > 0) { List temp = controller.animations; controller.animations = new List(); for (int i = 0; i < temp.Count; ++i) { if (!deleted.Contains(i)) controller.animations.Add(temp[i]); } } GUILayout.Space(3); } // add new Rect addRect = GUILayoutUtility.GetRect(0f, 20f); if (GUI.Button(addRect, "+New Animation")) { controller.animations.Add(null); } if(GUI.changed) { EditorUtility.SetDirty(controller); } } } }