34 private static List<String> s_PathsNotDeleted =
new();
45 [ExcludeFromCodeCoverage]
77 public static void BatchEditing([NotNull] Action massAssetFileEditAction)
83 massAssetFileEditAction?.Invoke();
103 public static Object
Create([NotNull] Byte[] contents, [NotNull]
Path path) => CreateInternal(contents, path);
118 public static Object
CreateAsNew([NotNull] Byte[] contents, [NotNull]
Path path) => CreateInternal(contents, path.UniqueFilePath);
132 public static Object
Create([NotNull] String contents, [NotNull]
Path path) => CreateInternal(contents, path);
147 public static Object
CreateAsNew([NotNull] String contents, [NotNull]
Path path) => CreateInternal(contents, path.UniqueFilePath);
163 public static Object
Create([NotNull] Object instance, [NotNull]
Path path) => CreateInternal(instance, path);
179 public static Object
CreateAsNew([NotNull] Object instance, [NotNull]
Path path) => CreateInternal(instance, path.UniqueFilePath);
200 LoadOrCreate(path, getInstance);
214 public static void Save([NotNull] Object asset) => SaveInternal(asset);
234 public static void ForceSave([NotNull] Object asset) => SaveInternal(asset,
true);
245 public static void Save(GUID guid)
247 ThrowIf.NotAnAssetGuid(guid);
249 AssetDatabase.SaveAssetIfDirty(guid);
271 public static void Import([NotNull]
Path path, ImportAssetOptions options = ImportAssetOptions.Default)
273 ThrowIf.ArgumentIsNull(path, nameof(path));
274 ThrowIf.DoesNotExistInFileSystem(path);
276 AssetDatabase.ImportAsset(path, options);
304 ImportAssetOptions options = ImportAssetOptions.Default)
305 where T : Object => Load<T>(path);
321 Import([NotNull] IEnumerable<
Path> paths, ImportAssetOptions options = ImportAssetOptions.Default) =>
322 Import(
Path.ToStrings(paths.ToArray()), options);
338 Import([NotNull] IEnumerable<
string> paths, ImportAssetOptions options = ImportAssetOptions.Default) =>
BatchEditing(
341 foreach (var path
in paths)
342 AssetDatabase.ImportAsset(path, options);
368 ThrowIf.ArgumentIsNull(path, nameof(path));
370 ImportIfNotImported(path);
371 return AssetDatabase.LoadAssetAtPath<T>(path);
390 if (path.ExistsInFileSystem ==
false)
391 return Create(getInstance.Invoke(), path) as T;
411 public static T LoadMain<T>([NotNull]
Path path) where T : Object
413 ThrowIf.ArgumentIsNull(path, nameof(path));
414 ThrowIf.DoesNotExistInFileSystem(path);
416 ImportIfNotImported(path);
417 return AssetDatabase.LoadMainAssetAtPath(path) as T;
435 public static T LoadMain<T>(GUID guid) where T : Object
437 ThrowIf.NotAnAssetGuid(guid);
440 ImportIfNotImported(path);
441 return LoadMain<T>(path);
459 [ExcludeFromCodeCoverage]
460 public static AssetDatabaseLoadOperation
LoadAsync([NotNull]
Path path, Int64 localFileId)
462#if UNITY_2022_2_OR_NEWER
463 return AssetDatabase.LoadObjectAsync(path, localFileId);
465 throw new NotSupportedException(
"AssetDatabase.LoadObjectAsync not available in this editor version");
488 [ExcludeFromCodeCoverage]
489 public static String[]
Find([NotNull] String filter, String[] searchInFolders =
null) => searchInFolders ==
null
490 ? AssetDatabase.FindAssets(filter)
491 : AssetDatabase.FindAssets(filter, searchInFolders);
513 [ExcludeFromCodeCoverage]
514 public static GUID[]
FindGuids([NotNull] String filter, String[] searchInFolders =
null) =>
515 Find(filter, searchInFolders).Select(guid =>
new GUID(guid)).ToArray();
536 [ExcludeFromCodeCoverage]
537 public static Path[]
FindPaths([NotNull] String filter, String[] searchInFolders =
null) =>
538 Find(filter, searchInFolders).Select(guid =>
Path.
Get(
new GUID(guid))).ToArray();
559 [ExcludeFromCodeCoverage]
580 public static Boolean
Copy([NotNull]
Path sourcePath, [NotNull]
Path destinationPath) =>
581 CopyInternal(sourcePath, destinationPath,
true);
602 CopyInternal(sourcePath, destinationPath.UniqueFilePath,
false);
623 public static Boolean
CanMove([NotNull]
Path sourcePath, [NotNull]
Path destinationPath)
625 if (sourcePath ==
null || destinationPath ==
null)
628 return Succeeded(AssetDatabase.ValidateMoveAsset(sourcePath, destinationPath));
649 public static Boolean
Move([NotNull]
Path sourcePath, [NotNull]
Path destinationPath)
651 if (sourcePath ==
null || destinationPath ==
null)
654 destinationPath.CreateFolders();
655 return Succeeded(AssetDatabase.MoveAsset(sourcePath, destinationPath));
675 public static Boolean
Rename([NotNull]
Path path, String newFileName) => String.IsNullOrEmpty(newFileName) ==
false &&
676 Succeeded(AssetDatabase.RenameAsset(path, newFileName));
689 [ExcludeFromCodeCoverage]
703 [ExcludeFromCodeCoverage]
704 public static Boolean
CanOpenInEditor(Int32 instanceId) => AssetDatabase.CanOpenAssetInEditor(instanceId);
719 [ExcludeFromCodeCoverage]
720 public static void OpenExternal([NotNull] Object asset, Int32 lineNumber = -1, Int32 columnNumber = -1) =>
721 AssetDatabase.OpenAsset(asset, lineNumber, columnNumber);
736 [ExcludeFromCodeCoverage]
737 public static void OpenExternal(Int32 instanceId, Int32 lineNumber = -1, Int32 columnNumber = -1) =>
738 AssetDatabase.OpenAsset(instanceId, lineNumber, columnNumber);
753 [ExcludeFromCodeCoverage]
754 public static void OpenExternal([NotNull]
Path path, Int32 lineNumber = -1, Int32 columnNumber = -1) =>
755 OpenExternal(Load<Object>(path), lineNumber, columnNumber);
770 path !=
null && path.Exists && AssetDatabase.DeleteAsset(path);
813 public static Boolean
Delete([NotNull] IEnumerable<String> paths) =>
814 AssetDatabase.DeleteAssets(paths.ToArray(), s_PathsNotDeleted =
new List<String>());
831 public static Boolean
Trash([NotNull]
Path path) => path !=
null && AssetDatabase.MoveAssetToTrash(path);
880 public static Boolean
Trash([NotNull] IEnumerable<String> paths) =>
881 AssetDatabase.MoveAssetsToTrash(paths.ToArray(), s_PathsNotDeleted =
new List<String>());
892 public static Type
GetMainType([NotNull]
Path path) => AssetDatabase.GetMainAssetTypeAtPath(path);
908 [ExcludeFromCodeCoverage]
911#if UNITY_2023_2_OR_NEWER
912 return AssetDatabase.GetMainAssetTypeFromGUID(guid);
928 [ExcludeFromCodeCoverage]
929 public static Type
GetSubType([NotNull]
Path path, Int64 fileId) => AssetDatabase.GetTypeFromPathAndFileID(path, fileId);
950 return (
new GUID(), 0L);
953 var localId = Int64.MaxValue;
954 return AssetDatabase.TryGetGUIDAndLocalFileIdentifier(asset, out var guid, out localId)
955 ? (
new GUID(guid), localId)
970 public static GUID
GetGuid([NotNull] Object asset)
976 var localId = Int64.MaxValue;
977 return AssetDatabase.TryGetGUIDAndLocalFileIdentifier(asset, out var guid, out localId)
999 var localId = Int64.MaxValue;
1000 return AssetDatabase.TryGetGUIDAndLocalFileIdentifier(asset, out var _, out localId) ? localId : 0L;
1004 [ExcludeFromCodeCoverage]
1005 internal static void StartAssetEditing() => AssetDatabase.StartAssetEditing();
1008 [ExcludeFromCodeCoverage]
1009 internal static void StopAssetEditing() => AssetDatabase.StopAssetEditing();
1011 internal static Object CreateInternal([NotNull] Byte[] bytes, [NotNull]
Path path)
1013 ThrowIf.ArgumentIsNull(bytes, nameof(bytes));
1014 ThrowIf.ArgumentIsNull(path, nameof(path));
1016 path.CreateFolders();
1017 System.IO.File.WriteAllBytes(path, bytes);
1018 return ImportAndLoad<Object>(path);
1021 internal static Object CreateInternal([NotNull] String contents, [NotNull] Path path)
1023 ThrowIf.ArgumentIsNull(contents, nameof(contents));
1024 ThrowIf.ArgumentIsNull(path, nameof(path));
1026 path.CreateFolders();
1027 System.IO.File.WriteAllText(path, contents, Encoding.UTF8);
1028 return ImportAndLoad<Object>(path);
1031 internal static Object CreateInternal([NotNull] Object instance, [NotNull] Path path)
1033 ThrowIf.ArgumentIsNull(instance, nameof(instance));
1034 ThrowIf.ArgumentIsNull(path, nameof(path));
1036 path.CreateFolders();
1037 AssetDatabase.CreateAsset(instance, path);
1041 internal static Boolean CopyInternal([NotNull] Path sourcePath, [NotNull] Path destinationPath,
1042 Boolean overwriteExisting)
1044 ThrowIf.ArgumentIsNull(sourcePath, nameof(sourcePath));
1045 ThrowIf.ArgumentIsNull(destinationPath, nameof(destinationPath));
1046 ThrowIf.AssetPathNotInDatabase(sourcePath);
1047 ThrowIf.SourceAndDestPathAreEqual(sourcePath, destinationPath);
1049 destinationPath.CreateFolders();
1051#if UNITY_2022_1_OR_NEWER
1052 var success = AssetDatabase.CopyAsset(sourcePath, destinationPath);
1053 SetLastErrorMessage(success ? String.Empty : $
"failed to copy {sourcePath} to {destinationPath}");
1058 var original = LoadMain<Object>(sourcePath);
1059 var copy = Object.Instantiate(original);
1060 copy =
Create(copy, destinationPath);
1061 return copy !=
null;
1065 private static void SaveInternal([NotNull] Object asset, Boolean forceSave =
false)
1067 ThrowIf.ArgumentIsNull(asset, nameof(asset));
1068 ThrowIf.NotInDatabase(asset);
1071 EditorUtility.SetDirty(asset);
1073 AssetDatabase.SaveAssetIfDirty(asset);
1076 private static void ImportIfNotImported([NotNull] Path path,
1077 ImportAssetOptions options = ImportAssetOptions.Default)
1081 if (path.Exists ==
false && path.ExistsInFileSystem)
1085#if !UNITY_2022_2_OR_NEWER
1086 public class AssetDatabaseLoadOperation {}