Documentation for the Unity C# Library
Loading...
Searching...
No Matches
StepsDataSO.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Linq;
5using System.Text.RegularExpressions;
6#if UNITY_EDITOR
7using Sirenix.OdinInspector;
8using Sirenix.Utilities.Editor;
10using UnityEditor;
11#endif
12using UnityEngine;
13
14namespace PixoVR.Event
15{
16 [CreateAssetMenu(menuName = "Scenarios Data Menu/Create Steps Data", fileName = "StepsData", order = 2)]
18 {
19#if UNITY_EDITOR
20 [InlineEditor,
21 ListDrawerSettings(
22 CustomRemoveElementFunction = "Remove",
23 CustomRemoveIndexFunction = "RemoveAt",
24 CustomAddFunction = "Add",
25 OnBeginListElementGUI = "OnBeginListElementGUI"
26 ), DisableContextMenu(DisableForCollectionElements = true, DisableForMember = false),
27 Searchable]
28#endif
29 public List<StepItemSO> listOfSteps;
30
31#if UNITY_EDITOR
32 [ReadOnly] public ScenarioInfoSO owner;
33
34 public void Add()
35 {
36 TextInputDialogue.ShowDialogue(
37 "Enter ID for new step",
38 stepId =>
39 {
40 if (!string.IsNullOrEmpty(stepId))
41 {
42 var stepData = this;
43
44 var scenarioPath =
45 $"{Path.GetDirectoryName(AssetDatabase.GetAssetPath(this))}";
46
47 if (!stepData.listOfSteps.Any(d => d.stepID.Equals(stepId)))
48 {
49 var stepPath =
50 $"{scenarioPath}/{ScenarioEditorWindow.StepsDirectoryName}/{stepId}.asset";
51
52 var stepItem = ScriptableObject.CreateInstance<StepItemSO>();
53 stepItem.stepID = stepId;
54 AssetDatabase.CreateAsset(stepItem, stepPath);
55 }
56 else
57 {
58 EditorUtility.DisplayDialog(
59 "Add Step Error",
60 "Step id exist!",
61 "ok"
62 );
63 }
64 }
65 else
66 {
67 EditorUtility.DisplayDialog(
68 "Add Step Error",
69 "Step id is empty",
70 "ok"
71 );
72 }
73
74 AssetDatabase.Refresh();
75 }
76 );
77 }
78
79 public void RemoveAt(int index)
80 {
81 AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(this.listOfSteps[index]));
82 AssetDatabase.Refresh();
83 Validate();
84 }
85
86 public void Remove(StepItemSO item)
87 {
88 RemoveAt(listOfSteps.IndexOf(item));
89 }
90
91 public void OnBeginListElementGUI(int index)
92 {
93 SirenixEditorGUI.BeginHorizontalToolbar();
94
95 if (SirenixEditorGUI.ToolbarButton("Duplicate"))
96 {
97 TextInputDialogue.ShowDialogue(
98 "Enter ID for new step",
99 stepId =>
100 {
101 if (!string.IsNullOrEmpty(stepId))
102 {
103 var stepData = this;
104
105 var scenarioPath =
106 $"{Path.GetDirectoryName(AssetDatabase.GetAssetPath(this))}";
107
108 if (!stepData.listOfSteps.Any(d => d.stepID.Equals(stepId)))
109 {
110 var stepPath =
111 $"{scenarioPath}/{ScenarioEditorWindow.StepsDirectoryName}/{stepId}.asset";
112
113 var stepItem =
114 ScriptableObject.Instantiate(listOfSteps[index]);
115
116 stepItem.stepID = stepId;
117 AssetDatabase.CreateAsset(stepItem, stepPath);
118 }
119 else
120 {
121 EditorUtility.DisplayDialog(
122 "Add Step Error",
123 "Step id exist!",
124 "ok"
125 );
126 }
127 }
128 else
129 {
130 EditorUtility.DisplayDialog(
131 "Add Step Error",
132 "Step id is empty",
133 "ok"
134 );
135 }
136
137 AssetDatabase.Refresh();
138 }
139 );
140 }
141
142 if (SirenixEditorGUI.ToolbarButton("Rename"))
143 {
144 TextInputDialogue.ShowDialogue(
145 "Enter new ID name",
146 stepId =>
147 {
148 var step = listOfSteps[index];
149
150 if (string.IsNullOrEmpty(stepId) || !step.ValidateID(stepId))
151
152 {
153 EditorUtility.DisplayDialog(
154 "Rename Error",
155 "Invalid step ID: " + stepId,
156 "ok"
157 );
158 }
159 else
160
161 {
162 step.stepID = stepId;
163 EditorUtility.SetDirty(step);
164 AssetDatabase.SaveAssetIfDirty(step);
165 AssetDatabase.Refresh();
166
167 AssetDatabase.RenameAsset(
168 AssetDatabase.GetAssetPath(step),
169 stepId
170 );
171
172 AssetDatabase.Refresh();
173 }
174 }
175 );
176 }
177
178 SirenixEditorGUI.EndHorizontalToolbar();
179 }
180
181 public void Rebuild()
182 {
183 var scenarioPath = $"{Path.GetDirectoryName(AssetDatabase.GetAssetPath(this))}";
184
185 var stepsPath = $"{scenarioPath}/{ScenarioEditorWindow.StepsDirectoryName}";
186
187 listOfSteps = new List<StepItemSO>();
188
189 // Find all Texture2Ds that have 'co' in their filename, that are labelled with 'architecture' and are placed in 'MyAwesomeProps' folder
190 string[] guids = AssetDatabase.FindAssets("t:StepItemSO", new[] {stepsPath});
191
192 foreach (string guid in guids)
193 {
194 listOfSteps.Add(AssetDatabase.LoadAssetAtPath<StepItemSO>(AssetDatabase.GUIDToAssetPath(guid)));
195 }
196
197 // listOfSteps.Sort((s, s2) => String.Compare(s.item.stepID, s2.item.stepID, StringComparison.Ordinal));
198 EditorUtility.SetDirty(this);
199 }
200#endif
201
202 public StepItem GetStepItem(string stepID = "")
203 {
204 if (stepID == string.Empty)
205 {
206 return listOfSteps[0].item;
207 }
208
209 for (int i = 0; i < listOfSteps.Count; i++)
210 {
211 if (listOfSteps[i].item.stepID == stepID)
212 {
213 return listOfSteps[i].item;
214 }
215 }
216
217 return null;
218 }
219#if UNITY_EDITOR
220 public void Validate()
221 {
222 Rebuild();
223 Debug.Log($"StepsData {name} validation begin");
224
225 for (int i = 0; i < listOfSteps.Count; i++)
226 {
227 listOfSteps[i].Validate();
228 var v1 = listOfSteps[i].stepID;
229
230 if (Regex.IsMatch(v1, @"\p{IsCyrillic}"))
231 {
232 Debug.LogError($"StepsData {v1} Cyrillic symbol presented!!");
233 }
234
235 for (int j = i + 1; j < listOfSteps.Count; j++)
236 {
237 var v2 = listOfSteps[j].stepID;
238
239 if (v1.Equals(v2))
240 {
241 Debug.LogError($"StepsData {v1} duplicates at position {i}:{j}");
242 }
243 }
244 }
245
246 Debug.Log($"StepsData {name} validation finish");
247 }
248#endif
249 }
250}
[Serializable]
Definition StepItem.cs:12
StepItem GetStepItem(string stepID="")
List< StepItemSO > listOfSteps