5 namespace HKI.Core.Editor.Misc
15 string[] GUIDs = AssetDatabase.FindAssets(filter);
18 for (
int i = 0; i < GUIDs.Length; i++)
20 string assetPath = AssetDatabase.GUIDToAssetPath(GUIDs[i]);
21 if(assetPath.EndsWith(fileEnding))
23 Selection.activeObject = AssetDatabase.LoadMainAssetAtPath(assetPath);
36 folder = EditorUtility.SaveFolderPanel(panelText,
"Assets",
"") +
"/";
38 while (
string.IsNullOrEmpty(folder))
40 if (EditorUtility.DisplayDialog(
"Error", errorMsg,
"Try again",
"Cancel"))
41 folder = EditorUtility.SaveFolderPanel(panelText,
"Assets",
"") +
"/";
46 folder = folder.Remove(0, Application.dataPath.Length);
48 folder =
"Assets" + folder;
53 public static T CreateAsset<T>(
string name,
string folder) where T : ScriptableObject
55 if (
string.IsNullOrEmpty(folder) ||
string.IsNullOrEmpty(name))
57 Debug.LogError(
"You try to create an asset but name or folder is null or empty");
61 if (!Directory.Exists(folder))
62 Directory.CreateDirectory(folder);
64 T scriptableObject = ScriptableObject.CreateInstance<T>();
66 scriptableObject.name = name;
68 AssetDatabase.CreateAsset(scriptableObject, folder + name +
".asset");
70 AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(scriptableObject));
72 return scriptableObject;
75 public static T FindAndLoad<T>(
string fileEnding =
".asset") where T : Object
77 string[] GUIDs = AssetDatabase.FindAssets(
"t:" + typeof(T).Name);
80 for (
int i = 0; i < GUIDs.Length; i++)
82 string assetPath = AssetDatabase.GUIDToAssetPath(GUIDs[i]);
83 if (assetPath.EndsWith(fileEnding))
84 return AssetDatabase.LoadMainAssetAtPath(assetPath) as T;
91 public static Object
FindAndLoad(
string objectType,
string fileEnding =
".asset")
93 string[] GUIDs = AssetDatabase.FindAssets(
"t:" + objectType);
96 for (
int i = 0; i < GUIDs.Length; i++)
98 string assetPath = AssetDatabase.GUIDToAssetPath(GUIDs[i]);
99 if (assetPath.EndsWith(fileEnding))
100 return AssetDatabase.LoadMainAssetAtPath(assetPath);
109 serializedObject.Update();
111 if(propertyNames != null && propertyNames.Length > 0)
113 for(
int i = 0; i < propertyNames.Length; i++)
115 SerializedProperty sp = serializedObject.FindProperty(propertyNames[i]);
119 if(sp.objectReferenceValue == null)
120 sp.objectReferenceValue = FindAndLoad(sp.type.Replace(
"PPtr<$",
"").Replace(
">",
""));
123 Debug.LogError(
"Couldn't find property with name: " + propertyNames[i]);
127 serializedObject.ApplyModifiedProperties();
static bool FindAndSelectIfExists(string filter, string fileEnding=".asset")
static void TryToLoadAllRequieredAssets(SerializedObject serializedObject, string[] propertyNames)
This class containes helper functions for editor scripts.
static string GetFolderFromUser(string panelText, string errorMsg)
static Object FindAndLoad(string objectType, string fileEnding=".asset")