17 path = AbsoluteFolder(path);
18 asset = AssetDatabase.LoadAssetAtPath(path, typeof(
Object));
19 Error = asset == null;
20 if (!Error) CurrentFolder = path;
24 public static string[] FindByFilter(
string filter) {
25 string[] list = AssetDatabase.FindAssets(filter);
27 for (
int i = 0; i < list.Length; i++) list[i] = (AssetDatabase.GUIDToAssetPath(list[i]));
31 public static string[] FindByType<T>() where T :
Object => FindByFilter($
"t:{typeof(T)}");
32 public static string[] FindByLabel(
string label) => FindByFilter($
"l:{label}");
33 public static string[] FindByName(
string name) => FindByFilter($
"name");
36 public void Dispose() {
43 public string CurrentFolder =
"";
45 public static string ProjectFolder() {
46 EditorUtility.FocusProjectWindow();
47 foreach (
Object asset
in Selection.GetFiltered(typeof(
Object), SelectionMode.Assets)) {
48 var path = SetFolderFor(asset);
49 if (path != null)
return path;
54 public AssetDb ProjectFolder(out
string path) {
55 path = ProjectFolder();
58 private static string SetFolderFor(
Object asset) {
59 var path = AssetDatabase.GetAssetPath(asset);
60 if (!
string.IsNullOrEmpty(path))
61 if (Directory.Exists(path)) {
63 }
else if (File.Exists(path)) {
64 return Path.GetDirectoryName(path);
69 if (Error)
return this;
70 EditorUtility.FocusProjectWindow();
71 CurrentFolder = SetFolderFor(Selection.activeObject = asset);
72 EditorGUIUtility.PingObject(asset);
76 asset = Selection.activeObject;
79 public AssetDb Select(
string path = null) => Find(path ?? CurrentFolder, out var asset).Select(asset);
83 public AssetDb CreateFolders(
string path) {
84 if (Error)
return this;
85 if (!path.StartsWith(
"./")) CurrentFolder =
"";
87 var folders = path.Split(
'/');
88 for (
int i = 0; i < folders.Length; i++) {
89 if ((folders[i].Length != 0) && (folders[i] !=
".")) {
90 Find($
"./{folders[i]}", out
Object _);
92 Error = AssetDatabase.CreateFolder(CurrentFolder, folders[i]) == null;
93 if (Error)
return this;
94 CurrentFolder = CurrentFolder.Length == 0 ? folders[i] : $
"{CurrentFolder}/{folders[i]}";
100 public AssetDb Delete(
string path) {
101 if (Error)
return this;
102 Error = AssetDatabase.MoveAssetToTrash(AbsoluteFolder(path));
105 public AssetDb SubFolders(out
string[] subFolders) {
106 subFolders = AssetDatabase.GetSubFolders(CurrentFolder);
110 private string AbsoluteFolder(
string path) {
111 if (path.StartsWith(
"./")) {
112 path = path.Substring(2);
113 return (CurrentFolder ==
"") ? path : $
"{CurrentFolder}/{path}";
115 return (path ==
"Assets") || path.StartsWith(
"Assets/") ? path : $
"Assets/{path}";
120 public static void Labels(
Object asset, params
string[] labels) => AssetDatabase.SetLabels(asset, labels);
121 public static string[] Labels(
Object asset) => AssetDatabase.GetLabels(asset);
124 #region Asset Creation and Loading (Editor only) 125 public static Object Load(
string path, Type type) {
126 path = Objects.FindFile(path);
127 return (path == null) ? null : AssetDatabase.LoadAssetAtPath(path, type);
130 public static T Load<T>(
string path) where T :
Object => (T) Load(path, typeof(T));
132 public AssetDb Load(
string path, out
Object asset, Type type) {
133 asset = Load(path, type);
134 Error = asset == null;
138 public AssetDb Load<T>(
string path, out T asset) where T :
Object {
139 asset = Load<T>(path);
149 public static Object LoadOrCreate(
string path, Type type) {
150 var asset = Load(path, type);
151 if (asset != null)
return asset;
152 AssetDatabase.CreateAsset(asset = ScriptableObject.CreateInstance(type), $
"Assets/{path}");
153 AssetDatabase.SaveAssets();
154 AssetDatabase.Refresh();
158 public AssetDb LoadOrCreate(
string path, out
Object asset, Type type) {
159 asset = LoadOrCreate(path, type);
163 public static T LoadOrCreate<T>(
string path) where T : ScriptableObject => (T) LoadOrCreate(path, typeof(T));
165 public AssetDb LoadOrCreate<T>(
string path, out T asset) where T : ScriptableObject {
166 asset = LoadOrCreate<T>(path);