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

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

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


Xem tìm kiếm...