Documentation for the Unity C# Library
Loading...
Searching...
No Matches
ScenarioEditorWindow.cs
Go to the documentation of this file.
1using System.Collections.Generic;
2using System.IO;
3using UnityEngine;
4#if UNITY_EDITOR
5using Sirenix.OdinInspector.Editor;
6using UnityEditor;
7using UnityEditor.Build;
8
10{
11 public class ScenarioEditorWindow : OdinMenuEditorWindow
12 {
13 public const string ScenariosRootPath = "Assets/ScenariosData";
14 public const string StepsDirectoryName = "Steps";
15 private static ScenarioEditorWindow _instance;
16 private List<ScenariosDataSO> _modules;
17
18 private OdinMenuTree _tree;
19 private string _lastModuleName;
20
21 [MenuItem("Window/PIXO/Scenario Editor")]
22 public static void Open()
23 {
24 _instance = GetWindow(typeof(ScenarioEditorWindow), false, "Scenario Editor") as ScenarioEditorWindow;
25 }
26
27 protected override void OnEnable()
28 {
29 base.OnEnable();
30 Init();
31 }
32
33 protected override void OnDisable()
34 {
35 base.OnEnable();
36 _modules = null;
37 }
38
39 protected override OdinMenuTree BuildMenuTree()
40 {
41 return _tree;
42 }
43
44 public static void Reinit()
45 {
46 _instance?.Init();
47 }
48
49 protected override void OnGUI()
50 {
51 base.OnGUI();
52
53 EditorGUILayout.BeginHorizontal();
54
55 _lastModuleName = GUILayout.TextField(_lastModuleName, GUILayout.Width(150));
56
57 if (GUILayout.Button("Add", GUILayout.Width(50)))
58 {
59 AddModule(_lastModuleName);
60 }
61
62 EditorGUILayout.EndHorizontal();
63 }
64
65 private void AddModule(string name)
66 {
67 if (!string.IsNullOrEmpty(name))
68 {
69 var scenarioPath = $"{ScenariosRootPath}/{name}";
70
71 if (!Directory.Exists(scenarioPath))
72 {
73 Directory.CreateDirectory(ScenariosRootPath);
74 var senariosData = ScriptableObject.CreateInstance<ScenariosDataSO>();
75 AssetDatabase.CreateAsset(senariosData, $"{ScenariosRootPath}/{name}.asset");
76 _modules.Add(senariosData);
77 Init();
78 }
79 else
80 {
81 EditorUtility.DisplayDialog("Add Module Error", "Module already exist!", "ok");
82 }
83 }
84 else
85 {
86 EditorUtility.DisplayDialog("Add Module Error", "Module name is empty", "ok");
87 }
88
89 AssetDatabase.Refresh();
90 }
91
92 private void Init()
93 {
94 _tree = new OdinMenuTree();
95
96 string[] guids = AssetDatabase.FindAssets("t:ScenariosDataSO", new[] {ScenariosRootPath});
97 _modules = new List<ScenariosDataSO>();
98
99 foreach (string guid in guids)
100 {
101 _modules.Add(AssetDatabase.LoadAssetAtPath<ScenariosDataSO>(AssetDatabase.GUIDToAssetPath(guid)));
102 }
103
104 if (_modules.Count == 0)
105 {
106 Directory.CreateDirectory(ScenariosRootPath);
107 var senariosData = ScriptableObject.CreateInstance<ScenariosDataSO>();
108 AssetDatabase.CreateAsset(senariosData, $"{ScenariosRootPath}/DefaultScenarios/DefaultScenariosData.asset");
109 _modules.Add(senariosData);
110 }
111
112 for (int j = 0; j < _modules.Count; j++)
113 {
114 var senariosData = _modules[j];
115 senariosData.Validate();
116 _tree.Add($"{senariosData.name}", senariosData);
117
118 for (int i = 0; i < senariosData.listOfScenariosInfo.Count; i++)
119 {
120 _tree.Add(
121 $"{senariosData.name}/{senariosData.listOfScenariosInfo[i].name}",
122 senariosData.listOfScenariosInfo[i]
123 );
124 }
125 }
126
127 _tree.Selection.SupportsMultiSelect = false;
128 }
129 }
130}
131#endif