Documentation for the Unity C# Library
Loading...
Searching...
No Matches
ScenariosDataSO.cs
Go to the documentation of this file.
1using System.Collections.Generic;
2using System.IO;
3using System.Linq;
5using Sirenix.OdinInspector;
6#if UNITY_EDITOR
7using UnityEditor;
8#endif
9using UnityEngine;
10
11namespace PixoVR.Event
12{
14 {
15 [SerializeField,
16 ListDrawerSettings(
17 CustomRemoveElementFunction = "Remove",
18 CustomRemoveIndexFunction = "RemoveAt",
19 CustomAddFunction = "Add"
20 ), DisableContextMenu(DisableForCollectionElements = true, DisableForMember = false)]
21 public List<ScenarioInfoSO> listOfScenariosInfo = new List<ScenarioInfoSO>();
22
24 {
25 var scenarioInfo = listOfScenariosInfo.SingleOrDefault(x => x.Scenario == scenario);
26
27 if (scenarioInfo == null)
28 {
29 Debug.LogError($"Scenario {scenario} info not exist");
30 }
31
32 return scenarioInfo;
33 }
34
35#if UNITY_EDITOR
36 public void Add()
37 {
38 TextInputDialogue.ShowDialogue(
39 "Enter name for new scenario",
40 scenarioName => { AddScenario(scenarioName); }
41 );
42 }
43
44 public void RemoveAt(int index)
45 {
46 var path = AssetDatabase.GetAssetPath(listOfScenariosInfo[index]);
47 listOfScenariosInfo.RemoveAt(index);
48
49 AssetDatabase.DeleteAsset(Path.GetDirectoryName(path));
50 AssetDatabase.Refresh();
51 Validate();
52 ScenarioEditorWindow.Reinit();
53 }
54
55 public void Remove(ScenarioInfoSO item)
56 {
57 RemoveAt(listOfScenariosInfo.IndexOf(item));
58 }
59
60 private void AddScenario(string name)
61 {
62 if (!string.IsNullOrEmpty(name))
63 {
64 var scenarioPath = $"{ScenarioEditorWindow.ScenariosRootPath}/{this.name}/{name}";
65
66 if (!Directory.Exists(scenarioPath))
67 {
68 var scenarioInfoPath = $"{scenarioPath}/{name}ScenarioInfo.asset";
69 var scenarioStepDataPath = $"{scenarioPath}/{name}StepData.asset";
70
71 var scenarioStepsPath = $"{scenarioPath}/{ScenarioEditorWindow.StepsDirectoryName}";
72
73 Directory.CreateDirectory(scenarioPath);
74 Directory.CreateDirectory(scenarioStepsPath);
75
76 var stepsData = ScriptableObject.CreateInstance<StepsDataSO>();
77 AssetDatabase.CreateAsset(stepsData, scenarioStepDataPath);
78
79 var info = ScriptableObject.CreateInstance<ScenarioInfoSO>();
80 info.StepsDataLink = stepsData;
81 AssetDatabase.CreateAsset(info, scenarioInfoPath);
82
83 listOfScenariosInfo.Add(info);
84 EditorUtility.SetDirty(this);
85
86 ScenarioEditorWindow.Reinit();
87 }
88 else
89 {
90 EditorUtility.DisplayDialog("Add Scenario Error", "Scenario already exist!", "ok");
91 }
92 }
93 else
94 {
95 EditorUtility.DisplayDialog("Add Scenario Error", "Scenario name is empty", "ok");
96 }
97
98 AssetDatabase.Refresh();
99 }
100
101 private void OnValidate()
102 {
103 Validate();
104 }
105
106 public void Validate()
107 {
108 for (int i = listOfScenariosInfo.Count - 1; i >= 0; i--)
109 {
110 if (listOfScenariosInfo[i] == null)
111 {
112 listOfScenariosInfo.RemoveAt(i);
113 }
114 else
115 {
116 listOfScenariosInfo[i].Validate();
117 }
118 }
119 }
120#endif
121 }
122}
ScenarioInfoSO GetScenarioInfo(Scenario scenario)
List< ScenarioInfoSO > listOfScenariosInfo