HKI Core
EditorHelper.cs
Go to the documentation of this file.
1 using UnityEngine;
2 using UnityEditor;
3 using System.IO;
4 
5 namespace HKI.Core.Editor.Misc
6 {
10  public static class EditorHelper
11  {
12  // Static interface functions
13  public static bool FindAndSelectIfExists(string filter, string fileEnding = ".asset")
14  {
15  string[] GUIDs = AssetDatabase.FindAssets(filter);
16  if (GUIDs.Length > 0)
17  {
18  for (int i = 0; i < GUIDs.Length; i++)
19  {
20  string assetPath = AssetDatabase.GUIDToAssetPath(GUIDs[i]);
21  if(assetPath.EndsWith(fileEnding))
22  {
23  Selection.activeObject = AssetDatabase.LoadMainAssetAtPath(assetPath);
24  return true;
25  }
26  }
27  }
28 
29  return false;
30  }
31 
32  public static string GetFolderFromUser(string panelText, string errorMsg)
33  {
34  string folder = "";
35 
36  folder = EditorUtility.SaveFolderPanel(panelText, "Assets", "") + "/";
37 
38  while (string.IsNullOrEmpty(folder))
39  {
40  if (EditorUtility.DisplayDialog("Error", errorMsg, "Try again", "Cancel"))
41  folder = EditorUtility.SaveFolderPanel(panelText, "Assets", "") + "/";
42  else
43  return null;
44  }
45 
46  folder = folder.Remove(0, Application.dataPath.Length);
47 
48  folder = "Assets" + folder;
49 
50  return folder;
51  }
52 
53  public static T CreateAsset<T>(string name, string folder) where T : ScriptableObject
54  {
55  if (string.IsNullOrEmpty(folder) || string.IsNullOrEmpty(name))
56  {
57  Debug.LogError("You try to create an asset but name or folder is null or empty");
58  return null;
59  }
60 
61  if (!Directory.Exists(folder))
62  Directory.CreateDirectory(folder);
63 
64  T scriptableObject = ScriptableObject.CreateInstance<T>();
65 
66  scriptableObject.name = name;
67 
68  AssetDatabase.CreateAsset(scriptableObject, folder + name + ".asset");
69 
70  AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(scriptableObject));
71 
72  return scriptableObject;
73  }
74 
75  public static T FindAndLoad<T>(string fileEnding = ".asset") where T : Object
76  {
77  string[] GUIDs = AssetDatabase.FindAssets("t:" + typeof(T).Name);
78  if (GUIDs.Length > 0)
79  {
80  for (int i = 0; i < GUIDs.Length; i++)
81  {
82  string assetPath = AssetDatabase.GUIDToAssetPath(GUIDs[i]);
83  if (assetPath.EndsWith(fileEnding))
84  return AssetDatabase.LoadMainAssetAtPath(assetPath) as T;
85  }
86  }
87 
88  return null;
89  }
90 
91  public static Object FindAndLoad(string objectType, string fileEnding = ".asset")
92  {
93  string[] GUIDs = AssetDatabase.FindAssets("t:" + objectType);
94  if (GUIDs.Length > 0)
95  {
96  for (int i = 0; i < GUIDs.Length; i++)
97  {
98  string assetPath = AssetDatabase.GUIDToAssetPath(GUIDs[i]);
99  if (assetPath.EndsWith(fileEnding))
100  return AssetDatabase.LoadMainAssetAtPath(assetPath);
101  }
102  }
103 
104  return null;
105  }
106 
107  public static void TryToLoadAllRequieredAssets(SerializedObject serializedObject, string[] propertyNames)
108  {
109  serializedObject.Update();
110 
111  if(propertyNames != null && propertyNames.Length > 0)
112  {
113  for(int i = 0; i < propertyNames.Length; i++)
114  {
115  SerializedProperty sp = serializedObject.FindProperty(propertyNames[i]);
116 
117  if(sp != null)
118  {
119  if(sp.objectReferenceValue == null)
120  sp.objectReferenceValue = FindAndLoad(sp.type.Replace("PPtr<$", "").Replace(">", ""));
121  }
122  else
123  Debug.LogError("Couldn't find property with name: " + propertyNames[i]);
124  }
125  }
126 
127  serializedObject.ApplyModifiedProperties();
128  }
129  }
130 }
static bool FindAndSelectIfExists(string filter, string fileEnding=".asset")
Definition: EditorHelper.cs:13
static void TryToLoadAllRequieredAssets(SerializedObject serializedObject, string[] propertyNames)
This class containes helper functions for editor scripts.
Definition: EditorHelper.cs:10
static string GetFolderFromUser(string panelText, string errorMsg)
Definition: EditorHelper.cs:32
static Object FindAndLoad(string objectType, string fileEnding=".asset")
Definition: EditorHelper.cs:91