1 using System;
2 using
System.Collections;
3 using
UnityEngine;
4 #
if UNITY_EDITOR
5 using
UnityEditor;
6 #endif

7
8 namespace
UnityStandardAssets.Utility
9 {
10     
public class TimedObjectActivator : MonoBehaviour
11     {
12         
public enum Action
13         {
14             Activate,
15             Deactivate,
16             Destroy,
17             ReloadLevel,
18             Call,
19         }
20
21
22         
[Serializable]
23         
public class Entry
24         {
25             
public GameObject target;
26             
public Action action;
27             
public float delay;
28         }
29
30
31         
[Serializable]
32         
public class Entries
33         {
34             
public Entry[] entries;
35         }
36         
37         
38         
public Entries entries = new Entries();
39
40         
41         
private void Awake()
42         {
43             
foreach (Entry entry in entries.entries)
44             {
45                 
switch (entry.action)
46                 {
47                     
case Action.Activate:
48                         StartCoroutine(Activate(entry));
49                         
break;
50                     
case Action.Deactivate:
51                         StartCoroutine(Deactivate(entry));
52                         
break;
53                     
case Action.Destroy:
54                         Destroy(entry.target, entry.delay);
55                         
break;
56
57                     
case Action.ReloadLevel:
58                         StartCoroutine(ReloadLevel(entry));
59                         
break;
60                 }
61             }
62         }
63
64
65         
private IEnumerator Activate(Entry entry)
66         {
67             
yield return new WaitForSeconds(entry.delay);
68             entry.target.SetActive(
true);
69         }
70
71
72         
private IEnumerator Deactivate(Entry entry)
73         {
74             
yield return new WaitForSeconds(entry.delay);
75             entry.target.SetActive(
false);
76         }
77
78
79         
private IEnumerator ReloadLevel(Entry entry)
80         {
81             
yield return new WaitForSeconds(entry.delay);
82             Application.LoadLevel(Application.loadedLevel);
83         }
84     }
85 }

86
87
88 namespace
UnityStandardAssets.Utility.Inspector
89 {
90 #
if UNITY_EDITOR
91     
[CustomPropertyDrawer(typeof (TimedObjectActivator.Entries))]
92     
public class EntriesDrawer : PropertyDrawer
93     {
94         
private const float k_LineHeight = 18;
95         
private const float k_Spacing = 4;
96
97
98         
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
99         {
100             EditorGUI.BeginProperty(position, label, property);
101
102             
float x = position.x;
103             
float y = position.y;
104             
float width = position.width;
105
106             
// Draw label
107             EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
108
109             
// Don't make child fields be indented
110             
var indent = EditorGUI.indentLevel;
111             EditorGUI.indentLevel =
0;
112
113             
var entries = property.FindPropertyRelative("entries");
114
115             
if (entries.arraySize > 0)
116             {
117                 
float actionWidth = .25f*width;
118                 
float targetWidth = .6f*width;
119                 
float delayWidth = .1f*width;
120                 
float buttonWidth = .05f*width;
121
122                 
for (int i = 0; i < entries.arraySize; ++i)
123                 {
124                     y += k_LineHeight + k_Spacing;
125
126                     
var entry = entries.GetArrayElementAtIndex(i);
127
128                     
float rowX = x;
129
130                     
// Calculate rects
131                     Rect actionRect =
new Rect(rowX, y, actionWidth, k_LineHeight);
132                     rowX += actionWidth;
133
134                     Rect targetRect =
new Rect(rowX, y, targetWidth, k_LineHeight);
135                     rowX += targetWidth;
136
137                     Rect delayRect =
new Rect(rowX, y, delayWidth, k_LineHeight);
138                     rowX += delayWidth;
139
140                     Rect buttonRect =
new Rect(rowX, y, buttonWidth, k_LineHeight);
141                     rowX += buttonWidth;
142
143                     
// Draw fields - passs GUIContent.none to each so they are drawn without labels
144
145                     
if (entry.FindPropertyRelative("action").enumValueIndex !=
146                         (
int) TimedObjectActivator.Action.ReloadLevel)
147                     {
148                         EditorGUI.PropertyField(actionRect, entry.FindPropertyRelative(
"action"), GUIContent.none);
149                         EditorGUI.PropertyField(targetRect, entry.FindPropertyRelative(
"target"), GUIContent.none);
150                     }
151                     
else
152                     {
153                         actionRect.width = actionRect.width + targetRect.width;
154                         EditorGUI.PropertyField(actionRect, entry.FindPropertyRelative(
"action"), GUIContent.none);
155                     }
156
157                     EditorGUI.PropertyField(delayRect, entry.FindPropertyRelative(
"delay"), GUIContent.none);
158                     
if (GUI.Button(buttonRect, "-"))
159                     {
160                         entries.DeleteArrayElementAtIndex(i);
161                         
break;
162                     }
163                 }
164             }
165             
166             
// add & sort buttons
167             y += k_LineHeight + k_Spacing;
168
169             
var addButtonRect = new Rect(position.x + position.width - 120, y, 60, k_LineHeight);
170             
if (GUI.Button(addButtonRect, "Add"))
171             {
172                 entries.InsertArrayElementAtIndex(entries.arraySize);
173             }
174
175             
var sortButtonRect = new Rect(position.x + position.width - 60, y, 60, k_LineHeight);
176             
if (GUI.Button(sortButtonRect, "Sort"))
177             {
178                 
bool changed = true;
179                 
while (entries.arraySize > 1 && changed)
180                 {
181                     changed =
false;
182                     
for (int i = 0; i < entries.arraySize - 1; ++i)
183                     {
184                         
var e1 = entries.GetArrayElementAtIndex(i);
185                         
var e2 = entries.GetArrayElementAtIndex(i + 1);
186
187                         
if (e1.FindPropertyRelative("delay").floatValue > e2.FindPropertyRelative("delay").floatValue)
188                         {
189                             entries.MoveArrayElement(i +
1, i);
190                             changed =
true;
191                             
break;
192                         }
193                     }
194                 }
195             }
196
197
198             
// Set indent back to what it was
199             EditorGUI.indentLevel = indent;
200             
//
201
202
203             EditorGUI.EndProperty();
204         }
205
206
207         
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
208         {
209             SerializedProperty entries = property.FindPropertyRelative(
"entries");
210             
float lineAndSpace = k_LineHeight + k_Spacing;
211             
return 40 + (entries.arraySize*lineAndSpace) + lineAndSpace;
212         }
213     }
214 #endif
215 }


Draw label

Don't make child fields be indented

Calculate rects

Draw fields - passs GUIContent.none to each so they are drawn without labels

add & sort buttons

Set indent back to what it was



Gõ tìm kiếm nhanh...