summaryrefslogtreecommitdiff
path: root/Assets/UI_Extension/Scripts/Animation/Tween/Editor/TweenControllerInspector.cs
blob: e83e87e221086e7eb2e012f611636de07548c2c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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<int> deleted = new List<int>();
                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<TweenAnimation> temp = controller.animations;
                    controller.animations = new List<TweenAnimation>();
                    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);
            }

        }

    }
}