HKI Core
InitializerEditor.cs
Go to the documentation of this file.
1 using UnityEngine;
2 using UnityEditor;
3 using HKI.Core.Editor.Misc;
4 
5 namespace HKI.Core.Init.Editor
6 {
10  [CustomEditor(typeof(Initializer))]
11  public class InitializerEditor : UnityEditor.Editor
12  {
13  // Create function
17  [MenuItem("HKI/Core/Create Initializer and all subsystems")]
19  {
20  string rootFolder = EditorHelper.GetFolderFromUser("Select root folder for all systems to be saved in", "The given path for the systems is null or empty");
21 
22  if(!CreateInitializer(rootFolder))
23  return;
24 
26 
28 
30 
32 
33  EditorHelper.FindAndSelectIfExists("Initializer");
34  }
35 
39  [MenuItem("HKI/Core/Subsystems/Initializer")]
40  static void CreateInitializerMenu()
41  {
42  CreateInitializer();
43  }
49  static bool CreateInitializer(string rootFolder = null)
50  {
51  if(EditorHelper.FindAndSelectIfExists("Initializer"))
52  return true;
53 
54  // Create a new initializer
55  string folder = null;
56 
57  if(!string.IsNullOrEmpty(rootFolder))
58  folder = rootFolder;
59 
60  if(string.IsNullOrEmpty(folder))
61  {
62  folder = EditorHelper.GetFolderFromUser("Select folder for initializer to be saved in", "The given path for the initializer is null or empty");
63  if(string.IsNullOrEmpty(folder))
64  return false;
65  }
66 
67  EditorUtility.DisplayProgressBar("Create Initializer", "Create Initializer", 0.0f);
68 
69  if (!folder.EndsWith("/Resources/"))
70  folder += "Resources/";
71 
72  EditorUtility.DisplayProgressBar("Create Initializer", "Create Initializer", 0.25f);
73 
74  Selection.activeObject = EditorHelper.CreateAsset<Initializer>("Initializer", folder);
75 
76  EditorUtility.ClearProgressBar();
77 
78  return true;
79  }
80 
85  public static void AddScriptableObjectToInitializer(ScriptableObject obj)
86  {
87  if(obj == null)
88  return;
89 
90  Initializer initializer = null;
91 
92  string[] settingSystemGUIDs = AssetDatabase.FindAssets("t:Initializer");
93  if(settingSystemGUIDs.Length > 0)
94  {
95  for(int i = 0; i < settingSystemGUIDs.Length; i++)
96  {
97  string assetPath = AssetDatabase.GUIDToAssetPath(settingSystemGUIDs[i]);
98  if(assetPath.EndsWith("Initializer.asset"))
99  initializer = (Initializer)AssetDatabase.LoadMainAssetAtPath(assetPath);
100  }
101  }
102 
103  if(initializer == null)
104  return;
105 
106  SerializedObject initializerSO = new SerializedObject(initializer);
107  SerializedProperty scriptableObjectsSP = initializerSO.FindProperty("ScriptableObjects");
108 
109  if(scriptableObjectsSP == null)
110  return;
111 
112  for(int i = 0; i < scriptableObjectsSP.arraySize; i++)
113  {
114  if(scriptableObjectsSP.GetArrayElementAtIndex(i).objectReferenceValue == obj)
115  return;
116  }
117 
118  initializerSO.Update();
119 
120  bool added = false;
121  for(int i = 0; i < scriptableObjectsSP.arraySize; i++)
122  {
123  if (scriptableObjectsSP.GetArrayElementAtIndex(i).objectReferenceValue == null)
124  {
125  scriptableObjectsSP.GetArrayElementAtIndex(i).objectReferenceValue = obj;
126  added = true;
127  }
128  }
129 
130  if (!added)
131  {
132  scriptableObjectsSP.InsertArrayElementAtIndex(scriptableObjectsSP.arraySize);
133  scriptableObjectsSP.GetArrayElementAtIndex(scriptableObjectsSP.arraySize - 1).objectReferenceValue = obj;
134  }
135 
136  initializerSO.ApplyModifiedProperties();
137  }
138  }
139 }
static void AddScriptableObjectToInitializer(ScriptableObject obj)
This function adds a scriptable object as a subasset to the initializer asset.
Custom inspector for the AudioSystem class.
static bool FindAndSelectIfExists(string filter, string fileEnding=".asset")
Definition: EditorHelper.cs:13
static void CreateSceneLoader(string rootFolder=null)
static void CreateLocalizationSystem(string rootFolder=null)
This functio creates a localization system.
static void CreateSettingsSystem(string rootFolder=null)
static bool CreateInitializer(string rootFolder=null)
This function actualy creates the initializer.
This class containes helper functions for editor scripts.
Definition: EditorHelper.cs:10
static string GetFolderFromUser(string panelText, string errorMsg)
Definition: EditorHelper.cs:32
Custom inspector for the SceneLoader class.
This Initializer controlls the initialization of anything that implements the IInit interface...
Definition: Initializer.cs:11
static void CreateInitializerMenu()
This function is for the menu item that creates only the initializer.
This class contains the creation function of the menu for all HKI Core systems.
static void CreateInitializerAndAllSubsystems()
This function is for the menu item that creates the initializer and all subsystems.
Custom inspector for the SettingsSystem class.
Custom inspector for the LocalizationSystem class.
static void CreateAudioSystem(string rootFolder=null)
This function creates audio systems and all the other required assets like playlists for the HKI Core...