5using System.Diagnostics.CodeAnalysis;
12using Object = UnityEngine.Object;
22 public sealed partial class Asset
24 private Path m_AssetPath;
25 private Object m_MainObject;
37 public static implicit
operator Object(Asset asset) => asset !=
null ? asset.MainObject :
null;
50 public static implicit
operator Asset(Object asset) => asset !=
null ?
new Asset(asset) : null;
62 public static implicit
operator Asset(
Path path) => path !=
null ?
new Asset(path) : null;
75 public static implicit
operator Asset(String path) => (
Path)path;
88 public static implicit
operator Asset(GUID guid) => guid.Empty() ==
false ?
new Asset(guid) : null;
90 [ExcludeFromCodeCoverage]
private Asset() {}
108 public Asset(Byte[] contents,
Path path, Boolean overwriteExisting =
false)
110 ThrowIf.ArgumentIsNull(contents, nameof(contents));
111 ThrowIf.ArgumentIsNull(path, nameof(path));
113 path =
Path.UniquifyAsNeeded(path, overwriteExisting);
114 var asset =
File.CreateInternal(contents, path);
115 InitWithMainObject(asset);
133 public Asset(String contents,
Path path, Boolean overwriteExisting =
false)
135 ThrowIf.ArgumentIsNull(contents, nameof(contents));
136 ThrowIf.ArgumentIsNull(path, nameof(path));
138 path =
Path.UniquifyAsNeeded(path, overwriteExisting);
139 var asset =
File.CreateInternal(contents, path);
140 InitWithMainObject(asset);
162 public Asset(Object asset,
Path path, Boolean overwriteExisting =
false)
164 ThrowIf.ArgumentIsNull(asset, nameof(asset));
165 ThrowIf.ArgumentIsNull(path, nameof(path));
166 ThrowIf.AlreadyAnAsset(asset);
168 path =
Path.UniquifyAsNeeded(path, overwriteExisting);
169 File.CreateInternal(asset, path);
170 InitWithMainObject(asset);
194 public Asset(GUID assetGuid) => InitWithGuid(assetGuid);
206 public Asset(Object asset) => InitWithMainObject(asset);
259 public Asset
SaveAs(
Path path) =>
File.Copy(m_AssetPath, path) ? new Asset(path) : null;
279 ThrowIf.ArgumentIsNull(path, nameof(path));
281 return SaveAs(path.UniqueFilePath);
300 public void SetDirty() => EditorUtility.SetDirty(m_MainObject);
303 [ExcludeFromCodeCoverage]
304 private void Import() {}
307 private T LoadMain<T>() where T : Object => m_AssetPath != null ? (T)(m_MainObject = File.Load<T>(m_AssetPath)) : null;
322 public T Load<T>() where T : Object =>
File.Load<T>(m_AssetPath);
355 if (
File.
Move(m_AssetPath, destinationPath))
357 SetAssetPathFromObject();
394 public Boolean
Rename(String newFileName)
398 SetAssetPathFromObject();
416 [ExcludeFromCodeCoverage]
427 [ExcludeFromCodeCoverage]
446 var mainObject = m_MainObject;
448 InvalidateInstance();
469 var mainObject = m_MainObject;
471 InvalidateInstance();
485 [ExcludeFromCodeCoverage]
536#if UNITY_6000_6_OR_NEWER
552 [ExcludeFromCodeCoverage]
553 public void ExportPackage(String packagePath, ExportPackageOptions options = ExportPackageOptions.Default,
554 String ownerOrgId =
null) =>
Package.
Export(m_AssetPath, packagePath, options, ownerOrgId);
566 [ExcludeFromCodeCoverage]
567 public void ExportPackage(String packagePath, ExportPackageOptions options = ExportPackageOptions.Default) =>
592 private void InvalidateInstance()
598 private void SetAssetPathFromObject() => m_AssetPath = Path.Get(m_MainObject);
600 private void InitWithPath(Path path)
602 ThrowIf.ArgumentIsNull(path, nameof(path));
603 ThrowIf.DoesNotExistInFileSystem(path);
606 m_MainObject = Status.IsImported(path) ? LoadMain<Object>() : File.ImportAndLoad<Object>(path);
608 ThrowIf.AssetLoadReturnedNull(m_MainObject, m_AssetPath);
611 private void InitWithMainObject(Object mainObject)
613 ThrowIf.ArgumentIsNull(mainObject, nameof(mainObject));
614 ThrowIf.NotInDatabase(mainObject);
616 m_MainObject = mainObject;
617 m_AssetPath = Path.Get(mainObject);
620 private void InitWithGuid(GUID guid)
622 ThrowIf.NotAnAssetGuid(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, String ownerOrgId=null)
Exports the asset and its dependencies to a .unitypackage file.
Groups import/export functionality for .unitypackage files (Asset Packages).
static Path Get([NotNull] Object asset)
Gets the relative path of an asset.
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 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.
void ExportPackage(String packagePath, ExportPackageOptions options=ExportPackageOptions.Default, String ownerOrgId=null)
Exports this asset and its dependencies as a .unitypackage.
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 ...