123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEditor;
- using UnityEditor.AnimatedValues;
- [CustomEditor(typeof(UIAnimation))]
- public class UIAnimationEditor : Editor
- {
- SerializedProperty loop;
- SerializedProperty restartOnVisible;
- SerializedProperty startAnimationAfter;
- SerializedProperty animationFrames;
- AnimBool showMovementProps;
- AnimBool showRotationProps;
- AnimBool showScaleProps;
- AnimBool showOpacityProps;
- AnimBool showSliceProps;
- private static bool movementFoldout = true;
- private static bool rotationFoldout;
- private static bool scaleFoldout;
- private static bool opacityFoldout;
- private static bool sliceFoldout;
- static int chosenFrame = -1;
- int lastAddedFrame = -1;
- private bool recordingPosition;
- private bool lastRecordingState;
- public Vector2 originalPosition;
- void OnEnable()
- {
- loop = serializedObject.FindProperty("Loop");
- restartOnVisible = serializedObject.FindProperty("RestartOnVisible");
- startAnimationAfter = serializedObject.FindProperty("StartAnimationAfter");
- animationFrames = serializedObject.FindProperty("AnimationFrames");
- showMovementProps = new AnimBool();
- showRotationProps = new AnimBool();
- showScaleProps = new AnimBool();
- showOpacityProps = new AnimBool();
- showSliceProps = new AnimBool();
- showMovementProps.valueChanged.AddListener(Repaint);
- showRotationProps.valueChanged.AddListener(Repaint);
- showScaleProps.valueChanged.AddListener(Repaint);
- showOpacityProps.valueChanged.AddListener(Repaint);
- showSliceProps.valueChanged.AddListener(Repaint);
- }
- public override void OnInspectorGUI()
- {
- serializedObject.Update();
- EditorGUILayout.Space();
- EditorGUILayout.LabelField("Settings", EditorStyles.boldLabel);
- EditorGUILayout.PropertyField(startAnimationAfter);
- EditorGUILayout.PropertyField(loop);
- EditorGUILayout.PropertyField(restartOnVisible);
- EditorGUILayout.LabelField("Frames", EditorStyles.boldLabel);
- GUILayout.BeginHorizontal();
- GUILayout.FlexibleSpace();
- GUI.color = Color.green;
- if (GUILayout.Button("Add", GUILayout.Width(60)))
- {
- InsertFrame(0);
- }
- GUI.color = Color.white;
- GUILayout.FlexibleSpace();
- GUILayout.EndHorizontal();
- for (int i = 0; i < animationFrames.arraySize; i++)
- {
- EditorGUILayout.BeginHorizontal();
- if (GUILayout.Button("Frame " + (i + 1) + (lastAddedFrame == i ? " (Newest)" : ""), GUILayout.Height(30)))
- {
- chosenFrame = chosenFrame != i ? i : -1;
- }
- #region Delete Button
- GUI.color = Color.red;
- if (GUILayout.Button("X", GUILayout.Width(20), GUILayout.Height(20)))
- {
- animationFrames.DeleteArrayElementAtIndex(i);
- if (i == lastAddedFrame)
- lastAddedFrame = -1;
- i--;
- continue;
- }
- GUI.color = Color.white;
- #endregion
- EditorGUILayout.EndHorizontal();
- #region Frame Details
- //If this frame is selected
- if (chosenFrame == i)
- {
- SerializedProperty curFrame = animationFrames.GetArrayElementAtIndex(i);
-
- #region Properties
- SerializedProperty startAfter = curFrame.FindPropertyRelative("StartAfter");
- SerializedProperty duration = curFrame.FindPropertyRelative("Duration");
- SerializedProperty movementSection = curFrame.FindPropertyRelative("MovementSection");
- SerializedProperty rotationSection = curFrame.FindPropertyRelative("RotationSection");
- SerializedProperty scaleSection = curFrame.FindPropertyRelative("ScaleSection");
- SerializedProperty opacitySection = curFrame.FindPropertyRelative("OpacitySection");
- SerializedProperty sliceSection = curFrame.FindPropertyRelative("SliceSection");
- SerializedProperty audioClip = curFrame.FindPropertyRelative("AudioClip");
- SerializedProperty onPlay = curFrame.FindPropertyRelative("OnPlay");
- #endregion
- EditorGUILayout.PropertyField(startAfter);
- EditorGUILayout.PropertyField(duration);
- EditorGUILayout.Space();
- //Movement
- DrawAnimationSection(movementSection, ref movementFoldout, ref showMovementProps);
- EditorGUILayout.Space();
- //Rotation
- DrawAnimationSection(rotationSection, ref rotationFoldout, ref showRotationProps);
- EditorGUILayout.Space();
- //Scale
- DrawAnimationSection(scaleSection, ref scaleFoldout, ref showScaleProps);
- EditorGUILayout.Space();
- //Opacity
- DrawAnimationSection(opacitySection, ref opacityFoldout, ref showOpacityProps);
- EditorGUILayout.Space();
- //Slice
- DrawAnimationSection(sliceSection, ref sliceFoldout, ref showSliceProps);
- EditorGUILayout.Space();
- #region Sound
- EditorGUILayout.LabelField("Sound", EditorStyles.boldLabel);
- EditorGUILayout.PropertyField(audioClip, new GUIContent("Clip"));
- #endregion
- #region Event
- EditorGUILayout.LabelField("Events", EditorStyles.boldLabel);
- EditorGUILayout.PropertyField(onPlay, new GUIContent("On Play"));
- #endregion
- EditorGUILayout.Space();
- }
- #endregion
- #region Add Button
- GUILayout.BeginHorizontal();
- GUILayout.FlexibleSpace();
- GUI.color = Color.green;
- if (GUILayout.Button("Add", GUILayout.Width(60)))
- {
- InsertFrame(i + 1);
- }
- GUILayout.FlexibleSpace();
- GUILayout.EndHorizontal();
- #endregion
- GUI.color = Color.white;
- }
- serializedObject.ApplyModifiedProperties();
- }
- void InsertFrame(int index)
- {
- UIAnimation anim = ((UIAnimation)target);
- anim.AnimationFrames.Insert(index, new UIAnimation.AnimationFrame(0.3f, 0.5f, -1, -1, -1, -1, -1));
- lastAddedFrame = index;
- }
- void EndRecording()
- {
- UIAnimation uiAnimation = target as UIAnimation;
- UIElement element = uiAnimation.GetComponent<UIElement>();
- RectTransform elementRT = element.GetComponent<RectTransform>();
- RectTransform parentCanvas = element.GetComponentInParent<Canvas>().GetComponent<RectTransform>();
- Undo.RecordObject(element, "Record Hiding Position");
- float canvasWidth = parentCanvas.lossyScale.x * parentCanvas.rect.width;
- float canvasHeight = parentCanvas.lossyScale.y * parentCanvas.rect.height;
- float canvasStartX = parentCanvas.position.x - canvasWidth / 2;
- float canvasStartY = parentCanvas.position.y - canvasHeight / 2;
- uiAnimation.AnimationFrames[chosenFrame].MovementSection.WantedVectorValue = new Vector3((elementRT.position.x - canvasStartX) / canvasWidth,
- (elementRT.position.y - canvasStartY) / canvasHeight, elementRT.position.z);
- elementRT.position = element.MovementSection.startVectorValue;
- }
- void DiscardRecording()
- {
- UIAnimation uiAnimation = target as UIAnimation;
- UIElement element = uiAnimation.GetComponent<UIElement>();
- RectTransform elementRT = element.GetComponent<RectTransform>();
- elementRT.position = element.MovementSection.startVectorValue;
- lastRecordingState = recordingPosition = false;
- }
- void DrawAnimationSection(SerializedProperty section, ref bool foldout, ref AnimBool animBool)
- {
- #region Properties Initialization
- SerializedProperty useSection = section.FindPropertyRelative("UseSection");
- SerializedProperty type = section.FindPropertyRelative("Type");
- SerializedProperty wantedVectorValue = section.FindPropertyRelative("WantedVectorValue");
- SerializedProperty wantedFloatValue = section.FindPropertyRelative("WantedFloatValue");
- SerializedProperty startAfter = section.FindPropertyRelative("StartAfter");
- SerializedProperty duration = section.FindPropertyRelative("Duration");
- SerializedProperty easingParams = section.FindPropertyRelative("easingParams");
- SerializedProperty curFrame = animationFrames.GetArrayElementAtIndex(chosenFrame);
- SerializedProperty goToStartValue = curFrame.FindPropertyRelative("Start" + section.name.Substring(0, section.name.Length - 7));
- SerializedProperty movementHidingPosition = curFrame.FindPropertyRelative("MovementHidingPosition");
- SerializedProperty edgeGap = curFrame.FindPropertyRelative("EdgeGap");
- SerializedProperty direction = curFrame.FindPropertyRelative("Direction");
- #endregion
- string sectionName = section.displayName.Substring(0, section.displayName.Length - 8);
- #region Header
- EditorStyles.foldout.fontStyle = FontStyle.Bold;
- string chosenType = useSection.boolValue ? type.enumDisplayNames[type.enumValueIndex] : "None";
- foldout = EditorGUILayout.Foldout(foldout, sectionName + " (" + chosenType + ")", true);
- EditorStyles.foldout.fontStyle = FontStyle.Normal;
- #endregion
- if (foldout)
- {
- EditorGUILayout.PropertyField(useSection, new GUIContent("Use " + sectionName + "*"));
- animBool.target = useSection.boolValue;
- if (EditorGUILayout.BeginFadeGroup(animBool.faded))
- {
- EditorGUILayout.PropertyField(type);
- EditorGUILayout.PropertyField(goToStartValue, new GUIContent("Go To Normal?"));
- if (!goToStartValue.boolValue)
- {
- #region Wanted Values
- if (sectionName == "Movement")
- {
- EditorGUILayout.PropertyField(movementHidingPosition, new GUIContent("Hiding Position"));
- if (movementHidingPosition.enumValueIndex == 8) //Custom position
- {
- #region Discard Button
- if (recordingPosition)
- {
- EditorGUILayout.BeginHorizontal();
- GUILayout.FlexibleSpace();
- if (GUILayout.Button("X", GUILayout.Width(50), GUILayout.Height(15)))
- DiscardRecording();
- EditorGUILayout.EndHorizontal();
- }
- #endregion
- #region Record Button
- EditorGUILayout.BeginHorizontal();
- GUILayout.FlexibleSpace();
- if (recordingPosition)
- GUI.color = Color.red;
- recordingPosition = GUILayout.Toggle(recordingPosition, !recordingPosition ? "Record Position" : "Finish Recording", EditorStyles.miniButton);
- GUI.color = Color.white;
- EditorGUILayout.EndHorizontal();
- if (lastRecordingState != recordingPosition)
- {
- //If recording start
- if (recordingPosition)
- {
- for (int i = 0; i < targets.Length; i++)
- {
- UIAnimation uiAnimation = target as UIAnimation;
- UIElement element = uiAnimation.GetComponent<UIElement>();
- RectTransform elementRT = element.GetComponent<RectTransform>();
- RectTransform parentCanvas = element.GetComponentInParent<Canvas>().GetComponent<RectTransform>();
- float canvasWidth = parentCanvas.lossyScale.x * parentCanvas.rect.width;
- float canvasHeight = parentCanvas.lossyScale.y * parentCanvas.rect.height;
- if (wantedVectorValue.vector3Value == new Vector3(9999, 9999))
- {
- float canvasStartX = parentCanvas.position.x - canvasWidth / 2;
- float canvasStartY = parentCanvas.position.y - canvasHeight / 2;
- Undo.RecordObject(uiAnimation, "Position");
- wantedVectorValue.vector3Value = new Vector3((elementRT.position.x - canvasStartX) / canvasWidth,
- (elementRT.position.y - canvasStartY) / canvasHeight, elementRT.position.z);
- }
- element.MovementSection.startVectorValue = elementRT.position;
- Vector3 chp = wantedVectorValue.vector3Value;
- elementRT.position = new Vector3(
- parentCanvas.position.x + (chp.x - 0.5f) * canvasWidth,
- parentCanvas.position.y + (chp.y - 0.5f) * canvasHeight, elementRT.position.z);
- }
- }
- //If recording end
- if (!recordingPosition)
- {
- EndRecording();
- }
- }
- lastRecordingState = recordingPosition;
- #endregion
- Vector2 customVec = wantedVectorValue.vector3Value;
- wantedVectorValue.vector3Value = EditorGUILayout.Vector2Field(new GUIContent("Wanted Position", "Custom position as percentage of the screen."), customVec);
- }
- else
- {
- EditorGUILayout.PropertyField(edgeGap);
- }
- }
- if (sectionName == "Rotation")
- {
- EditorGUILayout.PropertyField(wantedVectorValue, new GUIContent("Wanted Rotation", "The rotation this element should change to."));
- EditorGUILayout.PropertyField(direction);
- }
- if (sectionName == "Scale")
- EditorGUILayout.PropertyField(wantedVectorValue, new GUIContent("Wanted Scale", "The scale this element should change to."));
- if (sectionName == "Opacity")
- EditorGUILayout.Slider(wantedFloatValue, 0, 1, new GUIContent("Wanted Opacity", "The opacity this element should fade to."));
- if (sectionName == "Slice")
- EditorGUILayout.Slider(wantedFloatValue, 0, 1, new GUIContent("Wanted Fill Amount", "The fill amount this element's image should change to."));
- #endregion
- //Ease Function Parameters Drawing
- DrawEasingParams(type.enumNames[type.enumValueIndex], easingParams);
- }
- #region Custom Properties
- #region Custom Properties Drawing
- if (startAfter.floatValue >= 0)
- {
- EditorGUILayout.BeginHorizontal();
- EditorGUILayout.PropertyField(startAfter);
- if (GUILayout.Button("Delete", EditorStyles.miniButtonRight))
- {
- Undo.RecordObject(target, "Delete Custom " + startAfter.displayName);
- UIAnimation.AnimationSection s = GetFrameAnimationSection(section);
- s.StartAfter = -1;
- }
- EditorGUILayout.EndHorizontal();
- }
- if (duration.floatValue >= 0)
- {
- EditorGUILayout.BeginHorizontal();
- EditorGUILayout.PropertyField(duration);
- if (GUILayout.Button("Delete", EditorStyles.miniButtonRight))
- {
- Undo.RecordObject(target, "Delete Custom Duration");
- UIAnimation.AnimationSection s = GetFrameAnimationSection(section);
- s.Duration = -1;
- }
- EditorGUILayout.EndHorizontal();
- }
- #endregion
- //Seperate in case there an option to add custom property
- if (startAfter.floatValue < 0 || duration.floatValue < 0)
- EditorGUILayout.Space();
- #region Custom Properties Adding
- if (startAfter.floatValue < 0)
- {
- string txt = targets.Length == 1 ? "Add custom \"" + startAfter.displayName + "\"" : "Add custom \"" + startAfter.displayName + "\" to all";
- string tooltip = "Add a custom \"" + startAfter.displayName + "\" for this animation section, meaning this section will ignore general \"" + startAfter.displayName + "\".";
- EditorGUILayout.BeginHorizontal();
- GUILayout.FlexibleSpace();
- if (GUILayout.Button(new GUIContent(txt, tooltip), EditorStyles.miniButtonRight, GUILayout.Width(EditorGUIUtility.currentViewWidth / 2)))
- {
- Undo.RecordObject(target, "Add Custom " + startAfter.displayName);
- UIAnimation anim = target as UIAnimation;
- UIAnimation.AnimationSection s = GetFrameAnimationSection(section);
- s.StartAfter = anim.AnimationFrames[chosenFrame].StartAfter;
- }
- EditorGUILayout.EndHorizontal();
- }
- if (duration.floatValue < 0)
- {
- string txt = targets.Length == 1 ? "Add custom \"" + duration.displayName + "\"" : "Add custom \"" + duration.displayName + "\" to all";
- string tooltip = "Add a custom \"" + duration.displayName + "\" for this animation section, meaning this section will ignore general \"" + duration.displayName + "\".";
- EditorGUILayout.BeginHorizontal();
- GUILayout.FlexibleSpace();
- if (GUILayout.Button(new GUIContent(txt, tooltip), EditorStyles.miniButtonRight, GUILayout.Width(EditorGUIUtility.currentViewWidth / 2)))
- {
- Undo.RecordObject(target, "Add Custom Duration");
- UIAnimation anim = target as UIAnimation;
- UIAnimation.AnimationSection s = GetFrameAnimationSection(section);
- s.Duration = anim.AnimationFrames[chosenFrame].Duration;
- }
- EditorGUILayout.EndHorizontal();
- }
- #endregion
- #endregion
- EditorGUILayout.EndFadeGroup();
- }
- }
- }
- void DrawEasingParams(string equationName, SerializedProperty easingParams)
- {
- SerializedProperty property = easingParams.FindPropertyRelative(equationName);
- if (property != null)
- {
- SerializedProperty endProp = property.Copy();
- endProp.Next(false);
- SerializedProperty c = property.Copy();
- c.Next(true);
- while (true)
- {
- EditorGUILayout.PropertyField(c, new GUIContent(c.displayName), true);
- c.Next(true);
- if (SerializedProperty.EqualContents(c, endProp))
- break;
- }
- }
- }
- void OnDisable()
- {
- if (recordingPosition)
- {
- if (EditorUtility.DisplayDialog("Recording", "You are recording a position, would you like to apply it?", "Apply", "No"))
- EndRecording();
- else
- DiscardRecording();
- }
- }
- UIAnimation.AnimationSection GetFrameAnimationSection(SerializedProperty section)
- {
- UIAnimation anim = target as UIAnimation;
- System.Reflection.PropertyInfo frameListPropInfo = anim.GetType().GetProperty("_AnimationFrames");
- IList frameListI = frameListPropInfo.GetValue(anim, null) as IList;
- System.Reflection.PropertyInfo sectionProp = frameListPropInfo.PropertyType.GetGenericArguments()[0].GetProperty("_" + section.name);
- foreach (var listItem in frameListI)
- {
- if (frameListI.IndexOf(listItem) == chosenFrame)
- {
- UIAnimation.AnimationSection s = sectionProp.GetValue(listItem, null) as UIAnimation.AnimationSection;
- return s;
- }
- }
- return null;
- }
- }
|