Tanoda
ScriptAction.cs
Go to the documentation of this file.
1using System.Collections;
2using System.IO;
3using System.Runtime.Serialization;
4using GILES.Interface;
5using UnityEngine;
6using UnityEngine.UI;
7
8public class ScriptAction : ActionObject, ISerializable
9{
10 public Dropdown selector; //TODO: replace to AutoCompleate ComboBox
11
12#if DANA
13 internal string selectedScript;
14 internal bool autostart;
15 private bool deactivated;
16 private bool once = true;
17
18 public override void Start()
19 {
20 base.Start();
21 StartCoroutine(WaitingStart());
22 }
23
24 private IEnumerator WaitingStart()
25 {
26 yield return null;
27 while (LoadingManager.instance.MainLoading || LoadingManager.instance.isLoading)
28 {
29 yield return null;
30 yield return null;
31 yield return null;
32 }
33
34 FillDropdown();
35 selector.onValueChanged.AddListener(OnDropdownChanged);
36 if (autostart)
37 {
38 GetComponentInChildren<Toggle>().isOn = true;
39 if (!SavedUser.instance.isEditor) MiscLogicManager.instance.onLoadingFinished += Instance_onLoadingFinished;
40 yield return new WaitForSeconds(2f);
41 Triggered("");
42 }
43 }
44
45 private void Instance_onLoadingFinished()
46 {
47 Triggered("");
48 }
49
50 public void OnToggleChanged(bool value)
51 {
52 autostart = value;
53 }
54
55 private void FillDropdown()
56 {
57 selector.ClearOptions();
58 var soundList = pb_PrefabBrowser.instance.Scripts.transform.GetComponentsInChildren<Text>();
59 foreach (var o in soundList) selector.options.Add(new Dropdown.OptionData(o.text));
60
61 if (!string.IsNullOrEmpty(selectedScript))
62 {
63 for (var i = 0; i < selector.options.Count; i++)
64 if (selector.options[i].text == selectedScript)
65 {
66 selector.value = i;
67 OnDropdownChanged(i);
68 return;
69 }
70 }
71 else
72 OnDropdownChanged(0);
73 }
74
75
76 internal void FillScriptText()
77 {
78 if (!string.IsNullOrEmpty(selectedScript))
79 {
80 for (var i = 0; i < selector.options.Count; i++)
81 if (selector.options[i].text == selectedScript)
82 {
83 selector.value = i;
84 OnDropdownChanged(i);
85 return;
86 }
87 }
88 else
89 OnDropdownChanged(0);
90 }
91
92 public void OnDropdownChanged(int value)
93 {
94 selectedScript = selector.options[value].text;
95#if UNITY_WEBGL
96 var result = new [] {"/fileuploader/" + selectedScript};
97#else
98 var result = Directory.GetFiles(Application.streamingAssetsPath, selector.options[value].text,
99 SearchOption.AllDirectories);
100#endif
101 if (result.Length == 1)
102 {
103 // ok
104 }
105 else
106 {
107 Debug.LogError("More than one script file found with the same name! (or zero)");
108 //TODO: lekezelni
109 }
110 }
111
112 public override void Deactivate()
113 {
114 base.Deactivate();
115 deactivated = true;
116 }
117
118 public override void Triggered(string id)
119 {
120 if (deactivated)
121 return;
122
123 once = true;
124
125 base.Triggered(id);
126
127 LUAScriptingManager.instance.RunScriptFromFile(selectedScript);
128
129 TriggerNow();
130 }
131
132 public void TriggerNow()
133 {
134 statusImg.color = Color.green;
135 }
136
137 public new void GetObjectData(SerializationInfo info, StreamingContext context)
138 {
139 base.GetObjectData(info, context);
140 info.AddValue("selectedScript", selectedScript, typeof(string));
141 info.AddValue("autostart", autostart, typeof(bool));
142 }
143
144 public ScriptAction(SerializationInfo info, StreamingContext context) : base(info, context)
145 {
146 selectedScript = (string) info.GetValue("selectedScript", typeof(string));
147 autostart = info.GetBoolean("autostart");
148 }
149#endif
150}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
UnityEngine.Color Color
Definition: TestScript.cs:32
Image statusImg
Definition: ActionObject.cs:21
virtual void Deactivate()
virtual void Start()
virtual void Triggered(string id)
virtual void GetObjectData(SerializationInfo info, StreamingContext context)
static pb_PrefabBrowser instance
Dropdown selector
Definition: ScriptAction.cs:10