5using System.Diagnostics.CodeAnalysis;
8using Object = UnityEngine.Object;
18 public sealed partial class Asset
20 private Path m_AssetPath;
21 private Object m_MainObject;
33 public static implicit
operator Object(
Asset asset) => asset !=
null ? asset.MainObject :
null;
46 public static implicit
operator Asset(Object asset) => asset !=
null ?
new Asset(asset) : null;
58 public static implicit
operator Asset(
Path path) => path !=
null ?
new Asset(path) : null;
71 public static implicit
operator Asset(String path) => (
Path)path;
84 public static implicit
operator Asset(GUID guid) => guid.Empty() ==
false ?
new Asset(guid) : null;
86 [ExcludeFromCodeCoverage]
private Asset() {}
104 public Asset(Byte[] contents,
Path path, Boolean overwriteExisting =
false)
106 ThrowIf.ArgumentIsNull(contents, nameof(contents));
107 ThrowIf.ArgumentIsNull(path, nameof(path));
109 path =
Path.UniquifyAsNeeded(path, overwriteExisting);
110 var asset =
File.CreateInternal(contents, path);
111 InitWithMainObject(asset);
129 public Asset(String contents,
Path path, Boolean overwriteExisting =
false)
131 ThrowIf.ArgumentIsNull(contents, nameof(contents));
132 ThrowIf.ArgumentIsNull(path, nameof(path));
134 path =
Path.UniquifyAsNeeded(path, overwriteExisting);
135 var asset =
File.CreateInternal(contents, path);
136 InitWithMainObject(asset);
158 public Asset(Object asset,
Path path, Boolean overwriteExisting =
false)
160 ThrowIf.ArgumentIsNull(asset, nameof(asset));
161 ThrowIf.ArgumentIsNull(path, nameof(path));
162 ThrowIf.AlreadyAnAsset(asset);
164 path =
Path.UniquifyAsNeeded(path, overwriteExisting);
165 File.CreateInternal(asset, path);
166 InitWithMainObject(asset);
190 public Asset(GUID assetGuid) => InitWithGuid(assetGuid);
202 public Asset(Object asset) => InitWithMainObject(asset);
275 ThrowIf.ArgumentIsNull(path, nameof(path));
277 return SaveAs(path.UniqueFilePath);
296 public void SetDirty() => EditorUtility.SetDirty(m_MainObject);
299 [ExcludeFromCodeCoverage]
300 private void Import() {}
303 private T LoadMain<T>() where T : Object => m_AssetPath != null ? (T)(m_MainObject = File.Load<T>(m_AssetPath)) : null;
318 public T Load<T>() where T : Object =>
File.Load<T>(m_AssetPath);
351 if (
File.
Move(m_AssetPath, destinationPath))
353 SetAssetPathFromObject();
390 public Boolean
Rename(String newFileName)
394 SetAssetPathFromObject();
412 [ExcludeFromCodeCoverage]
423 [ExcludeFromCodeCoverage]
442 var mainObject = m_MainObject;
444 InvalidateInstance();
465 var mainObject = m_MainObject;
467 InvalidateInstance();
481 [ExcludeFromCodeCoverage]
538 [ExcludeFromCodeCoverage]
539 public void ExportPackage(String packagePath, ExportPackageOptions options = ExportPackageOptions.Default) =>
563 private void InvalidateInstance()
569 private void SetAssetPathFromObject() => m_AssetPath = Path.Get(m_MainObject);
571 private void InitWithPath(Path path)
573 ThrowIf.ArgumentIsNull(path, nameof(path));
574 ThrowIf.DoesNotExistInFileSystem(path);
577 m_MainObject = Status.IsImported(path) ? LoadMain<Object>() : File.ImportAndLoad<Object>(path);
579 ThrowIf.AssetLoadReturnedNull(m_MainObject, m_AssetPath);
582 private void InitWithMainObject(Object mainObject)
584 ThrowIf.ArgumentIsNull(mainObject, nameof(mainObject));
585 ThrowIf.NotInDatabase(mainObject);
587 m_MainObject = mainObject;
588 m_AssetPath = Path.Get(mainObject);
591 private void InitWithGuid(GUID guid)
593 ThrowIf.NotAnAssetGuid(guid);
595 InitWithPath(Path.Get(guid));
static Boolean Rename([NotNull] Path path, String newFileName)
Renames an asset's file or folder name.
static Boolean CanOpenInEditor([NotNull] Object instance)
Returns true if the given object can be opened (edited) by the Unity editor.
static Boolean Delete([NotNull] Path path)
Deletes an asset file or folder.
static Boolean Trash([NotNull] Path path)
Moves an asset file or folder to the OS trash.
static Boolean Move([NotNull] Path sourcePath, [NotNull] Path destinationPath)
Moves an asset file to destination path.
static void OpenExternal([NotNull] Object asset, Int32 lineNumber=-1, Int32 columnNumber=-1)
Opens the asset in the application associated with the file's extension.
Groups file related operations.
static void SetAll([NotNull] Object asset, [NotNull] String[] labels)
Sets an asset's labels. Replaces any existing labels.
static void Remove(Object asset, String label)
Removes a label from an asset. Does nothing if the label doesn't exist.
static void ClearAll([NotNull] Object asset)
Clears all labels of an asset.
static void Add([NotNull] Object asset, [NotNull] String label)
Adds a single label to an asset's list of labels.
Groups all asset label related static methods.
static void Export([NotNull] Path assetPath, [NotNull] String packagePath, ExportPackageOptions options=ExportPackageOptions.Default)
Exports the asset and its dependencies to a .unitypackage file.
Groups import/export functionality for .unitypackage files (Asset Packages).
Represents a relative path to an asset file or folder, typically under 'Assets' or 'Packages'.
static void Add([NotNull] Object subAssetInstance, [NotNull] Object asset)
Adds an object as sub-asset to the asset. This change is implicitly saved to disk.
static void Remove([NotNull] Object subAsset)
Removes a sub-object from the asset it is contained in.
Groups all Sub-Asset related functionality.
void OpenExternal(Int32 lineNumber=-1, Int32 columnNumber=-1)
Opens the asset in the external (associated) application.
T GetMain< T >()
Gets the main object cast to T.
Asset(GUID assetGuid)
Loads the asset using its GUID.
Asset(Object asset)
Uses an existing asset reference.
Asset SaveAs(Path path)
Saves a copy of the asset to a new path. Overwrites any existing asset at path.
void ClearLabels()
Removes all labels from the asset.
Asset(Path path)
Loads the asset at path.
Boolean Rename(String newFileName)
Renames an asset's file name (without extension) or a folder.
void AddLabel(String label)
Adds a label to the asset.
Boolean CanMove(Path destinationPath)
Tests if a Move operation will be successful without actually moving the asset.
Boolean Move(Path destinationPath)
Moves asset to destination path.
Asset(Byte[] contents, Path path, Boolean overwriteExisting=false)
Creates an asset file from a byte array.
void Save()
Saves any changes to the asset to disk.
void AddLabels(String[] labels)
Adds several labels to the asset.
void ExportPackage(String packagePath, ExportPackageOptions options=ExportPackageOptions.Default)
Exports this asset and its dependencies as a .unitypackage.
void AddSubAsset(Object instance)
Adds an object as a sub-object to the asset. The object must not already be an asset.
void ForceSave()
Saves the asset to disk, regardless of whether it is marked as 'dirty'.
Asset(String contents, Path path, Boolean overwriteExisting=false)
Creates an asset file from a string.
void RemoveLabel(String label)
Removes a label from an asset. Does nothing if the label doesn't exist.
Object Trash()
Moves the asset to the OS trash. Same as Delete, but recoverable.
Boolean CanOpenInEditor()
Returns true if the asset can be opened (edited) by the Unity Editor itself.
Asset(Object asset, Path path, Boolean overwriteExisting=false)
Creates an asset file from an existing UnityEngine.Object instance.
void SetDirty()
Marks the main object as dirty.
Asset Duplicate()
Creates a duplicate of the asset with a new, unique file name.
void RemoveSubAsset(Object subAsset)
Removes an object from the asset's sub-objects.
Asset SaveAsNew(Path path)
Saves a copy of the asset to a new path. Generates a unique file/folder name if path already exists.
Object Delete()
Deletes the asset file.
void SetLabels(String[] labels)
Sets the asset's labels, replacing all previously existing labels.
Replacement implementation for Unity's massive AssetDatabase class with a cleaner interface and more ...