public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { EditorGUI.BeginProperty(position, label, property); if (animator == null) { MonoBehaviour go = (MonoBehaviour)GetParent(property); animator = go.GetComponentInChildren <BaseAnimator>(); } if (animator != null) { if (animations == null || animations.Length != animator.animations.Count) { animations = new string[animator.animations.Count]; for (int i = 0; i < animations.Length; i++) { animations[i] = animator.animations[i].Name; if (animations[i] == property.stringValue) { selectedAnim = i; } } } selectedAnim = EditorGUI.Popup(position, label.text, selectedAnim, animations); property.stringValue = animations[selectedAnim]; } else { EditorGUI.LabelField(position, "No animator detected in this object."); } EditorGUI.EndProperty(); }
/// <summary> /// Draws a common inspector /// </summary> private void Init(BaseAnimator targetAnimator) { init = true; playOnAwake = serializedObject.FindProperty("playOnAwake"); ignoreTimeScale = serializedObject.FindProperty("ignoreTimeScale"); delayBetweenLoops = serializedObject.FindProperty("delayBetweenLoops"); minDelayBetweenLoops = serializedObject.FindProperty("minDelayBetweenLoops"); maxDelayBetweenLoops = serializedObject.FindProperty("maxDelayBetweenLoops"); oneShot = serializedObject.FindProperty("oneShot"); backwards = serializedObject.FindProperty("backwards"); randomAnimation = serializedObject.FindProperty("randomAnimation"); disableRendererOnFinish = serializedObject.FindProperty("disableRendererOnFinish"); startAtRandomFrame = serializedObject.FindProperty("startAtRandomFrame"); loopType = serializedObject.FindProperty("loopType"); fallbackLoopType = serializedObject.FindProperty("fallbackLoopType"); if (targetAnimator.animations == null) { targetAnimator.animations = new List <SpriteAnimation>(); } dragAndDropStyle = new GUIStyle(EditorStyles.helpBox); dragAndDropStyle.richText = true; dragAndDropStyle.alignment = TextAnchor.MiddleCenter; animationBoxStyle = new GUIStyle(EditorStyles.helpBox); animationBoxStyle.margin.left += 10; emptyContent = new GUIContent(""); arrowContent = new GUIContent(EditorGUIUtility.IconContent("UpArrow")); draggedAnimations = new List <SpriteAnimation>(); if (!targetAnimator.StartAnimation.Equals("")) { int startAnimationIndex = -1; for (int i = 0; i < targetAnimator.animations.Count; i++) { if (targetAnimator.animations[i] != null && targetAnimator.animations[i].Name.Equals(targetAnimator.StartAnimation)) { startAnimationIndex = i; } } if (startAnimationIndex != -1) { MoveStartingAnimationToTop(targetAnimator, startAnimationIndex); } else { targetAnimator.StartAnimation = ""; } } showAnimationList = EditorPrefs.GetBool(SHOW_ANIMATION_LIST_KEY, true); }
private void MoveStartingAnimationToTop(BaseAnimator targetAnimator, int startAnimationIndex) { List <SpriteAnimation> auxList = new List <SpriteAnimation>(targetAnimator.animations); targetAnimator.animations.Clear(); targetAnimator.animations.Add(auxList[startAnimationIndex]); for (int i = 0; i < auxList.Count; i++) { if (i != startAnimationIndex) { targetAnimator.animations.Add(auxList[i]); } } }
protected void DrawInspector(BaseAnimator targetAnimator) { if (animationNames == null) { GetAnimationNames(targetAnimator); } SerializedProperty animations = serializedObject.FindProperty("animations"); targetAnimator.playOnAwake = EditorGUILayout.Toggle("Play on Awake", targetAnimator.playOnAwake); targetAnimator.ignoreTimeScale = EditorGUILayout.Toggle("Ignore TimeScale", targetAnimator.ignoreTimeScale); if (targetAnimator.playOnAwake) { if (animationNames != null && animationNames.Length > 0) { startAnimationIndex = EditorGUILayout.Popup("Start Animation", startAnimationIndex, animationNames); targetAnimator.startAnimation = animationNames[startAnimationIndex]; } else { EditorGUILayout.LabelField("Start animation", "No animations"); } EditorUtility.SetDirty(targetAnimator); } targetAnimator.framesPerSecond = EditorGUILayout.IntField("FPS", targetAnimator.framesPerSecond); if (targetAnimator.framesPerSecond < 0) { targetAnimator.framesPerSecond = 0; } EditorGUILayout.PropertyField(animations, true); if (GUI.changed) { EditorUtility.SetDirty(targetAnimator); serializedObject.ApplyModifiedProperties(); GetAnimationNames(targetAnimator); } }
/// <summary> /// Fetch animations on the target animator /// </summary> /// <param name="targetAnimator"></param> private void GetAnimationNames(BaseAnimator targetAnimator) { if (targetAnimator.animations != null && targetAnimator.animations.Count > 0) { animationNames = new string[targetAnimator.animations.Count]; for (int i = 0; i < animationNames.Length; i++) { if (targetAnimator.animations[i]) { animationNames[i] = targetAnimator.animations[i].Name; if (targetAnimator.animations[i].Name == targetAnimator.startAnimation) { startAnimationIndex = i; } } else { animationNames[i] = "null" + i; } } } }
private void DragAndDropBox(BaseAnimator targetAnimator) { // Drag and drop box for sprite frames Rect dropArea = GUILayoutUtility.GetRect(0f, 50, GUILayout.ExpandWidth(true)); Event evt = Event.current; GUI.Box(dropArea, "Drop animations <b>HERE</b>.", dragAndDropStyle); switch (evt.type) { case EventType.DragUpdated: case EventType.DragPerform: if (!dropArea.Contains(evt.mousePosition)) { return; } DragAndDrop.visualMode = DragAndDropVisualMode.Copy; if (evt.type == EventType.DragPerform) { if (DragAndDrop.objectReferences.Length > 0) { DragAndDrop.AcceptDrag(); draggedAnimations.Clear(); foreach (Object draggedObject in DragAndDrop.objectReferences) { // Get dragged sprites SpriteAnimation s = draggedObject as SpriteAnimation; if (s != null && !targetAnimator.animations.Contains(s)) { draggedAnimations.Add(s); } } } } break; } }
private void DrawAnimationList(BaseAnimator targetAnimator) { EditorGUILayout.BeginVertical(EditorStyles.helpBox); { // Draw animation list if (targetAnimator.animations != null && targetAnimator.animations.Count > 0) { if (targetAnimator.animations[0] != null) { targetAnimator.StartAnimation = targetAnimator.animations[0].Name; } int toRemove = -1; int toFirst = -1; for (int i = 0; i < targetAnimator.animations.Count; i++) { bool fallback = (targetAnimator.FallbackAnimation != null && targetAnimator.FallbackAnimation.Name.Equals(targetAnimator.animations[i].Name)); EditorGUILayout.BeginVertical(i == 0 ? EditorStyles.helpBox : animationBoxStyle); { if (i == 0) { if (!fallback) { EditorGUILayout.LabelField("Starting Animation", EditorStyles.miniLabel); } else { EditorGUILayout.LabelField("Starting and Fallback Animation", EditorStyles.miniLabel); } } else { if (fallback) { EditorGUILayout.LabelField("Fallback Animation", EditorStyles.miniLabel); } } EditorGUILayout.BeginHorizontal(); { targetAnimator.animations[i] = EditorGUILayout.ObjectField("", targetAnimator.animations[i], typeof(SpriteAnimation), false) as SpriteAnimation; // Make this animation the first one if (i != 0 && GUILayout.Button(arrowContent, GUILayout.Width(30), GUILayout.Height(EditorGUIUtility.singleLineHeight), GUILayout.ExpandWidth(false))) { toFirst = i; targetAnimator.StartAnimation = targetAnimator.animations[i].Name; } // Remove animation if (GUILayout.Button("-", GUILayout.Height(EditorGUIUtility.singleLineHeight), GUILayout.ExpandWidth(false))) { toRemove = i; } } EditorGUILayout.EndHorizontal(); } EditorGUILayout.EndVertical(); } if (toRemove != -1) { targetAnimator.animations.RemoveAt(toRemove); if (targetAnimator.animations.Count == 0) { targetAnimator.StartAnimation = ""; } } if (toFirst != -1) { MoveStartingAnimationToTop(targetAnimator, toFirst); } } else { targetAnimator.StartAnimation = ""; } } EditorGUILayout.EndVertical(); }
/// <summary> /// Draws a common inspector /// </summary> protected void DrawInspector(BaseAnimator targetAnimator) { if (!init) { Init(targetAnimator); } EditorGUILayout.Space(); EditorGUILayout.LabelField("Basic Settings"); EditorGUILayout.BeginVertical(EditorStyles.helpBox); { EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Ignore time scale", GUILayout.ExpandWidth(false)); GUILayout.FlexibleSpace(); EditorGUILayout.PropertyField(ignoreTimeScale, emptyContent, GUILayout.ExpandWidth(false)); EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Start at random frame", GUILayout.ExpandWidth(false)); GUILayout.FlexibleSpace(); EditorGUILayout.PropertyField(startAtRandomFrame, emptyContent, GUILayout.ExpandWidth(false)); EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Play on awake", GUILayout.ExpandWidth(false)); GUILayout.FlexibleSpace(); EditorGUILayout.PropertyField(playOnAwake, emptyContent, GUILayout.ExpandWidth(false)); EditorGUILayout.EndHorizontal(); if (playOnAwake.boolValue) { EditorGUI.indentLevel++; EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("One shot", GUILayout.ExpandWidth(false)); GUILayout.FlexibleSpace(); EditorGUILayout.PropertyField(oneShot, emptyContent, GUILayout.ExpandWidth(false)); EditorGUILayout.EndHorizontal(); if (!oneShot.boolValue) { EditorGUI.indentLevel++; EditorGUILayout.PropertyField(loopType); delayBetweenLoops.boolValue = EditorGUILayout.BeginToggleGroup("Delay", delayBetweenLoops.boolValue); { EditorGUILayout.BeginHorizontal(GUILayout.ExpandWidth(false)); EditorGUIUtility.labelWidth = 65; EditorGUILayout.PropertyField(minDelayBetweenLoops, new GUIContent("Min")); EditorGUILayout.PropertyField(maxDelayBetweenLoops, new GUIContent("Max")); EditorGUIUtility.labelWidth = 0; EditorGUILayout.EndHorizontal(); } EditorGUILayout.EndToggleGroup(); if (minDelayBetweenLoops.floatValue < 0) { minDelayBetweenLoops.floatValue = 0; } if (maxDelayBetweenLoops.floatValue < minDelayBetweenLoops.floatValue) { maxDelayBetweenLoops.floatValue = minDelayBetweenLoops.floatValue; } EditorGUI.indentLevel--; } EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Disable render on finish", GUILayout.ExpandWidth(false)); GUILayout.FlexibleSpace(); EditorGUILayout.PropertyField(disableRendererOnFinish, emptyContent, GUILayout.ExpandWidth(false)); EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Backwards", GUILayout.ExpandWidth(false)); GUILayout.FlexibleSpace(); EditorGUILayout.PropertyField(backwards, emptyContent, GUILayout.ExpandWidth(false)); EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Random animation", GUILayout.ExpandWidth(false)); GUILayout.FlexibleSpace(); EditorGUILayout.PropertyField(randomAnimation, emptyContent, GUILayout.ExpandWidth(false)); EditorGUILayout.EndHorizontal(); EditorGUI.indentLevel--; } EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Fallback animation", GUILayout.ExpandWidth(false)); GUILayout.FlexibleSpace(); targetAnimator.FallbackAnimation = EditorGUILayout.ObjectField(targetAnimator.FallbackAnimation, typeof(SpriteAnimation), false) as SpriteAnimation; EditorGUILayout.EndHorizontal(); if (targetAnimator.FallbackAnimation != null) { EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Fallback animation loop type", GUILayout.ExpandWidth(false)); GUILayout.FlexibleSpace(); EditorGUILayout.PropertyField(fallbackLoopType, emptyContent, GUILayout.ExpandWidth(false)); EditorGUILayout.EndHorizontal(); } } EditorGUILayout.EndVertical(); EditorGUILayout.Space(); showAnimationList = EditorGUI.Foldout(GUILayoutUtility.GetRect(40f, 40f, 16f, 16f), showAnimationList, "Animation List", true); DragAndDropBox(targetAnimator); if (showAnimationList) { DrawAnimationList(targetAnimator); } else { EditorGUILayout.BeginVertical(EditorStyles.helpBox); EditorGUILayout.EndVertical(); } if (draggedAnimations != null && draggedAnimations.Count > 0) { showAnimationList = true; for (int i = 0; i < draggedAnimations.Count; i++) { targetAnimator.animations.Add(draggedAnimations[i]); if (targetAnimator.animations.Count == 1 && targetAnimator.animations[0] != null) { targetAnimator.StartAnimation = targetAnimator.animations[0].Name; } } draggedAnimations.Clear(); } if (GUI.changed) { EditorPrefs.SetBool(SHOW_ANIMATION_LIST_KEY, showAnimationList); if (targetAnimator.FallbackAnimation != null) { if (!targetAnimator.animations.Contains(targetAnimator.FallbackAnimation)) { targetAnimator.animations.Add(targetAnimator.FallbackAnimation); } } serializedObject.ApplyModifiedProperties(); EditorUtility.SetDirty(targetAnimator); } }
/// <summary> /// Draws a common inspector /// </summary> /// <param name="targetAnimator"></param> protected void DrawInspector(BaseAnimator targetAnimator) { if (animationNames == null) { GetAnimationNames(targetAnimator); } SerializedProperty animations = serializedObject.FindProperty("animations"); SerializedProperty oneShot = serializedObject.FindProperty("oneShot"); SerializedProperty backwards = serializedObject.FindProperty("backwards"); SerializedProperty randomAnimation = serializedObject.FindProperty("randomAnimation"); SerializedProperty disableRendererOnFinish = serializedObject.FindProperty("disableRendererOnFinish"); targetAnimator.ignoreTimeScale = EditorGUILayout.Toggle("Ignore TimeScale", targetAnimator.ignoreTimeScale); targetAnimator.playOnAwake = EditorGUILayout.Toggle("Play on Awake", targetAnimator.playOnAwake); if (targetAnimator.playOnAwake) { EditorGUI.indentLevel++; EditorGUILayout.PropertyField(oneShot); if (!oneShot.boolValue) { EditorGUI.indentLevel++; targetAnimator.delayBetweenLoops = EditorGUILayout.BeginToggleGroup("Delay", targetAnimator.delayBetweenLoops); { EditorGUILayout.BeginHorizontal(GUILayout.ExpandWidth(false)); EditorGUIUtility.labelWidth = 65; targetAnimator.minDelayBetweenLoops = EditorGUILayout.FloatField("Min", targetAnimator.minDelayBetweenLoops); targetAnimator.maxDelayBetweenLoops = EditorGUILayout.FloatField("Max", targetAnimator.maxDelayBetweenLoops); EditorGUIUtility.labelWidth = 0; EditorGUILayout.EndHorizontal(); } EditorGUILayout.EndToggleGroup(); if (targetAnimator.minDelayBetweenLoops < 0) { targetAnimator.minDelayBetweenLoops = 0; } if (targetAnimator.maxDelayBetweenLoops < targetAnimator.minDelayBetweenLoops) { targetAnimator.maxDelayBetweenLoops = targetAnimator.minDelayBetweenLoops; } EditorGUI.indentLevel--; } EditorGUILayout.PropertyField(disableRendererOnFinish); EditorGUILayout.PropertyField(backwards); EditorGUILayout.PropertyField(randomAnimation); if (!randomAnimation.boolValue) { if (animationNames != null && animationNames.Length > 0) { if (startAnimationIndex >= animationNames.Length) { startAnimationIndex = animationNames.Length - 1; } startAnimationIndex = EditorGUILayout.Popup("Start Animation", startAnimationIndex, animationNames); targetAnimator.startAnimation = animationNames[startAnimationIndex]; } else { EditorGUILayout.LabelField("Start animation", "No animations"); } } EditorGUI.indentLevel--; } EditorGUILayout.PropertyField(animations, true); if (GUI.changed) { serializedObject.ApplyModifiedProperties(); EditorUtility.SetDirty(targetAnimator); GetAnimationNames(targetAnimator); } }
/// <summary> /// Draws a common inspector /// </summary> /// <param name="targetAnimator"></param> protected void DrawInspector(BaseAnimator targetAnimator) { playOnAwake = serializedObject.FindProperty("playOnAwake"); ignoreTimeScale = serializedObject.FindProperty("ignoreTimeScale"); delayBetweenLoops = serializedObject.FindProperty("delayBetweenLoops"); minDelayBetweenLoops = serializedObject.FindProperty("minDelayBetweenLoops"); maxDelayBetweenLoops = serializedObject.FindProperty("maxDelayBetweenLoops"); startAnimation = serializedObject.FindProperty("startAnimation"); animations = serializedObject.FindProperty("animations"); oneShot = serializedObject.FindProperty("oneShot"); backwards = serializedObject.FindProperty("backwards"); randomAnimation = serializedObject.FindProperty("randomAnimation"); disableRendererOnFinish = serializedObject.FindProperty("disableRendererOnFinish"); startAtRandomFrame = serializedObject.FindProperty("startAtRandomFrame"); if (animationNames == null) { GetAnimationNames(targetAnimator); } EditorGUILayout.PropertyField(ignoreTimeScale); EditorGUILayout.PropertyField(startAtRandomFrame); EditorGUILayout.PropertyField(playOnAwake); if (playOnAwake.boolValue) { EditorGUI.indentLevel++; EditorGUILayout.PropertyField(oneShot); if (!oneShot.boolValue) { EditorGUI.indentLevel++; delayBetweenLoops.boolValue = EditorGUILayout.BeginToggleGroup("Delay", delayBetweenLoops.boolValue); { EditorGUILayout.BeginHorizontal(GUILayout.ExpandWidth(false)); EditorGUIUtility.labelWidth = 65; EditorGUILayout.PropertyField(minDelayBetweenLoops); EditorGUILayout.PropertyField(maxDelayBetweenLoops); EditorGUIUtility.labelWidth = 0; EditorGUILayout.EndHorizontal(); } EditorGUILayout.EndToggleGroup(); if (minDelayBetweenLoops.floatValue < 0) { minDelayBetweenLoops.floatValue = 0; } if (maxDelayBetweenLoops.floatValue < minDelayBetweenLoops.floatValue) { maxDelayBetweenLoops.floatValue = minDelayBetweenLoops.floatValue; } EditorGUI.indentLevel--; } EditorGUILayout.PropertyField(disableRendererOnFinish); EditorGUILayout.PropertyField(backwards); EditorGUILayout.PropertyField(randomAnimation); if (!randomAnimation.boolValue) { if (animationNames != null && animationNames.Length > 0) { if (startAnimationIndex >= animationNames.Length) { startAnimationIndex = animationNames.Length - 1; } startAnimationIndex = EditorGUILayout.Popup("Start Animation", startAnimationIndex, animationNames); startAnimation.stringValue = animationNames[startAnimationIndex]; } else { EditorGUILayout.LabelField("Start animation", "No animations"); } } EditorGUI.indentLevel--; } EditorGUILayout.PropertyField(animations, true); if (GUI.changed) { serializedObject.ApplyModifiedProperties(); EditorUtility.SetDirty(targetAnimator); GetAnimationNames(targetAnimator); } }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { EditorGUI.BeginProperty(position, label, property); if (animator == null) { MonoBehaviour go = (MonoBehaviour)GetParent(property); animator = go.GetComponentInChildren <BaseAnimator>(); if (animator == null) { EditorGUI.LabelField(position, "No animator detected in this object."); } } else { if (animator.animations == null || animator.animations.Count == 0) { EditorGUI.LabelField(position, "Animator without animations"); } else { bool refresh = animations == null || animations.Length != animator.animations.Count; if (!refresh) { if (animations.Length > 0 && animations.Length == animator.animations.Count) { for (int i = 0; i < animator.animations.Count; i++) { if (!animations[i].Equals(animator.animations[i])) { refresh = true; break; } } } } if (refresh) { animations = new string[animator.animations.Count]; for (int i = 0; i < animations.Length; i++) { if (animator.animations[i] != null) { animations[i] = animator.animations[i].Name; if (animations[i] == property.stringValue) { selectedAnim = i; } } else { animations[i] = "{null}"; } } } if (animations.Length > 0) { selectedAnim = EditorGUI.Popup(position, label.text, selectedAnim, animations); property.stringValue = animations[selectedAnim]; } else { selectedAnim = -1; EditorGUI.LabelField(position, "Animator without animations"); } } } EditorGUI.EndProperty(); }