UnityEssentials
Small but useful tools and features for Unity
SimpleAnimationsManagerInspector.cs
Go to the documentation of this file.
1 #if UNITY_EDITOR
2 using System;
3 using System.Collections.Generic;
4 using System.Linq;
5 using UnityEditor;
6 
7 namespace UnityEngine
8 {
9  [CanEditMultipleObjects]
10  [CustomEditor(typeof(SimpleAnimationsManager))]
11  public class SimpleAnimationsManagerInspector : UnityEditor.Editor
12  {
13  private Type[] implementations;
14  private int selectedImplementationIndex;
15  private SimpleAnimationsManager simpleAnimationsManager;
16 
17 
18  public override void OnInspectorGUI()
19  {
20 
21  // Update the serializedProperty - always do this in the beginning of OnInspectorGUI.
22  serializedObject.Update ();
23 
24 
25 
26 
27  //specify type
28  simpleAnimationsManager = target as SimpleAnimationsManager;
29  if (simpleAnimationsManager == null) { return; }
30 
31  //find all implementations of ISimpleAnimation using System.Reflection.Module
32  if (implementations == null)
33  implementations = Utils.GetTypeImplementationsNotUnityObject<ISimpleAnimation>();
34 
35  EditorGUILayout.Space();
36 
37  //select an implementation from all found using an editor popup
38  selectedImplementationIndex = EditorGUILayout.Popup(new GUIContent("Animation type"),
39  selectedImplementationIndex, implementations.Select(impl => impl.Name).ToArray());
40 
41  ISimpleAnimation newAnimation = null;
42  if (GUILayout.Button("Create animation"))
43  {
44  //Create a new animation of the selected type
45  newAnimation = (ISimpleAnimation) Activator.CreateInstance(implementations[selectedImplementationIndex]);
46  }
47 
48  //If a new animation has been created...
49  if (newAnimation != null)
50  {
51  //record the gameObject state to enable undo and prevent from exiting the scene without saving
52  Undo.RegisterCompleteObjectUndo(target, "Added new animation");
53  //add the new animation to the animation's list
54  if (simpleAnimationsManager.animations == null)
55  simpleAnimationsManager.animations = new List<ISimpleAnimation>();
56  simpleAnimationsManager.animations.Add(newAnimation);
57  }
58 
59  // Draw horizontal line
60  EditorGUILayout.Space(); EditorGUILayout.LabelField("", GUI.skin.horizontalSlider); EditorGUILayout.Space();
61 
62  if (simpleAnimationsManager.animations != null)
63  {
64  for (int a = 0; a < simpleAnimationsManager.animations.Count; a++)
65  {
66  if (simpleAnimationsManager.animations[a] == null)
67  EditorGUILayout.HelpBox("The animation with index " + a + " is null.\nRecommended to delete the array element by right clicking on it.", MessageType.Warning);
68 
69  if (simpleAnimationsManager.animations.Count() != simpleAnimationsManager.animations.Distinct().Count())
70  {
71  for (int d = a+1; d < simpleAnimationsManager.animations.Count; d++)
72  {
73  if (simpleAnimationsManager.animations[a] != null && (simpleAnimationsManager.animations[a] == simpleAnimationsManager.animations[d]) )
74  EditorGUILayout.HelpBox("The animations with index " + a + " and " + d + " are the same object.", MessageType.Warning);
75  }
76  }
77  }
78  }
79 
80  EditorGUI.indentLevel += 1;
81  EditorGUILayout.Space();
82  GUILayout.Label("Animations Configuration", EditorStyles.boldLabel);
83  ShowAnimationsArray(serializedObject.FindProperty("animations"));
84 
85  EditorGUI.indentLevel -= 1;
86 
87  // Draw horizontal line
88  EditorGUILayout.Space(); EditorGUILayout.Space();
89  EditorGUILayout.LabelField("", GUI.skin.horizontalSlider);
90 
91  // Implementations search
92  EditorGUILayout.BeginHorizontal();
93  if (implementations != null) EditorGUILayout.LabelField($"Found {implementations.Count()} implementations", EditorStyles.helpBox);
94  if (implementations == null || GUILayout.Button("Search implementations"))
95  {
96  //find all implementations of ISimpleAnimation using System.Reflection.Module
97  implementations = Utils.GetTypeImplementationsNotUnityObject<ISimpleAnimation>();
98  }
99  EditorGUILayout.EndHorizontal();
100 
101 
102 
103 
104  // Apply changes to the serializedProperty - always do this in the end of OnInspectorGUI.
105  serializedObject.ApplyModifiedProperties ();
106  }
107 
108 
109 
110 
111 
112  private void ShowAnimationsArray(UnityEditor.SerializedProperty list)
113  {
114  UnityEditor.EditorGUI.indentLevel += 1;
115  for (int i = 0; i < list.arraySize; i++)
116  {
117  EditorGUILayout.Space();
118  using (new GUILayout.VerticalScope(EditorStyles.helpBox))
119  {
120  SerializedProperty transformProp = list.GetArrayElementAtIndex(i);
121 
122  SimpleAnimation animation = ((SimpleAnimation) simpleAnimationsManager.animations[i]);
123 
124  string itemName;
125  if (animation.name.IsNullOrEmpty())
126  itemName = $"Animation [{i}]";
127  else
128  itemName = $"'{animation.name}' animation [{i}]";
129 
130  /*string animType = animation.GetType().Name;
131  int charSize = 57;
132  int reaminingChars = charSize - animType.Length - itemName.Length;
133  for (int j = 0; j < reaminingChars; j++)
134  itemName += " ";
135  itemName += animType;*/
136  EditorGUILayout.PropertyField(transformProp, new GUIContent(itemName), true);
137 
138  DisplayPreview(animation);
139 
140  EditorGUILayout.Space();
141  }
142  EditorGUILayout.Space();
143 
144  }
145  UnityEditor.EditorGUI.indentLevel -= 1;
146  }
147  private void DisplayPreview(SimpleAnimation animation)
148  {
149 
150  UnityEngine.Object animatedObject = animation.GetAnimatedObject(false);
151  if (animatedObject == null)
152  return;
153  int oldIndentLevel = UnityEditor.EditorGUI.indentLevel;
154  UnityEditor.EditorGUI.indentLevel = 1;
155  EditorGUILayout.BeginHorizontal();
156  float oldLabelWidth = EditorGUIUtility.labelWidth;
157  EditorGUIUtility.labelWidth = 75;
158  EditorGUILayout.LabelField("Animation preview");
159  EditorGUIUtility.labelWidth = oldLabelWidth;
160  EditorGUI.BeginChangeCheck();
161  float animProgression = animation.progress;
162  // Glitch to fix: The slider is not set to the proper value by default (after opening the scene, all the sliders are at 0, not at the proper value)
163  //animProgression = EditorGUILayout.Slider(animProgression, animation.mirror?1f:0f, animation.mirror?0f:1f);
164  animProgression = EditorGUILayout.Slider(animProgression, 0f, 1f);
165  if (EditorGUI.EndChangeCheck())
166  {
167  if (animatedObject != null)
168  {
169  Undo.RecordObject(animatedObject, "Changed animation progress (Animated Object)");
170  }
171  Undo.RecordObject(simpleAnimationsManager, "Changed animation progress (Simple Animation Manager)");
172  animation.SetProgress(animProgression);
173  }
174  EditorGUILayout.EndHorizontal();
175  UnityEditor.EditorGUI.indentLevel = oldIndentLevel;
176  }
177  }
178 }
179 
180 #endif