CodeSmile AssetDatabase 1.9
Unity's AssetDatabase in enjoyable, consistent, concise, convenient, comprehensible, safe, documented form.
Loading...
Searching...
No Matches
Asset.cs
1// Copyright (C) 2021-2024 Steffen Itterheim
2// Refer to included LICENSE file for terms and conditions.
3
4using System;
5using System.Diagnostics.CodeAnalysis;
6using System.IO;
7using UnityEditor;
8using Object = UnityEngine.Object;
9
10namespace CodeSmileEditor
11{
18 public sealed partial class Asset
19 {
20 private Path m_AssetPath;
21 private Object m_MainObject;
22
33 public static implicit operator Object(Asset asset) => asset != null ? asset.MainObject : null;
34
46 public static implicit operator Asset(Object asset) => asset != null ? new Asset(asset) : null;
47
58 public static implicit operator Asset(Path path) => path != null ? new Asset(path) : null;
59
71 public static implicit operator Asset(String path) => (Path)path; // implicit forward to Asset(Path)
72
84 public static implicit operator Asset(GUID guid) => guid.Empty() == false ? new Asset(guid) : null;
85
86 [ExcludeFromCodeCoverage] private Asset() {} // disallow parameterless ctor
87
104 public Asset(Byte[] contents, Path path, Boolean overwriteExisting = false)
105 {
106 ThrowIf.ArgumentIsNull(contents, nameof(contents));
107 ThrowIf.ArgumentIsNull(path, nameof(path));
108
109 path = Path.UniquifyAsNeeded(path, overwriteExisting);
110 var asset = File.CreateInternal(contents, path);
111 InitWithMainObject(asset);
112 }
113
129 public Asset(String contents, Path path, Boolean overwriteExisting = false)
130 {
131 ThrowIf.ArgumentIsNull(contents, nameof(contents));
132 ThrowIf.ArgumentIsNull(path, nameof(path));
133
134 path = Path.UniquifyAsNeeded(path, overwriteExisting);
135 var asset = File.CreateInternal(contents, path);
136 InitWithMainObject(asset);
137 }
138
158 public Asset(Object asset, Path path, Boolean overwriteExisting = false)
159 {
160 ThrowIf.ArgumentIsNull(asset, nameof(asset));
161 ThrowIf.ArgumentIsNull(path, nameof(path));
162 ThrowIf.AlreadyAnAsset(asset);
163
164 path = Path.UniquifyAsNeeded(path, overwriteExisting);
165 File.CreateInternal(asset, path);
166 InitWithMainObject(asset);
167 }
168
179 public Asset(Path path) => InitWithPath(path);
180
190 public Asset(GUID assetGuid) => InitWithGuid(assetGuid);
191
202 public Asset(Object asset) => InitWithMainObject(asset);
203
213 public T GetMain<T>() where T : Object => m_MainObject as T;
214
225 public void Save() => File.Save(m_MainObject);
226
237 public void ForceSave() => File.ForceSave(m_MainObject);
238
255 public Asset SaveAs(Path path) => File.Copy(m_AssetPath, path) ? new Asset(path) : null;
256
273 public Asset SaveAsNew(Path path)
274 {
275 ThrowIf.ArgumentIsNull(path, nameof(path));
276
277 return SaveAs(path.UniqueFilePath);
278 }
279
288 public Asset Duplicate() => SaveAsNew(m_AssetPath);
289
296 public void SetDirty() => EditorUtility.SetDirty(m_MainObject);
297
298 // NOTE: there is no public Import() method needed since the main object is guaranteed to be imported
299 [ExcludeFromCodeCoverage] // private, not used
300 private void Import() {}
301
302 // Private on purpose: the main object is automatically loaded when instantiating an Asset class.
303 private T LoadMain<T>() where T : Object => m_AssetPath != null ? (T)(m_MainObject = File.Load<T>(m_AssetPath)) : null;
304
318 public T Load<T>() where T : Object => File.Load<T>(m_AssetPath);
319
333 public Boolean CanMove(Path destinationPath) => File.CanMove(m_AssetPath, destinationPath);
334
349 public Boolean Move(Path destinationPath)
350 {
351 if (File.Move(m_AssetPath, destinationPath))
352 {
353 SetAssetPathFromObject();
354 return true;
355 }
356
357 return false;
358 }
359
390 public Boolean Rename(String newFileName)
391 {
392 if (File.Rename(m_AssetPath, newFileName))
393 {
394 SetAssetPathFromObject();
395 return true;
396 }
397
398 return false;
399 }
400
412 [ExcludeFromCodeCoverage] // simple relay
413 public Boolean CanOpenInEditor() => File.CanOpenInEditor(m_MainObject);
414
423 [ExcludeFromCodeCoverage] // cannot be tested
424 public void OpenExternal(Int32 lineNumber = -1, Int32 columnNumber = -1) => File.OpenExternal(m_MainObject, lineNumber, columnNumber);
425
440 public Object Delete()
441 {
442 var mainObject = m_MainObject;
443 if (File.Delete(m_AssetPath))
444 InvalidateInstance();
445
446 return mainObject;
447 }
448
463 public Object Trash()
464 {
465 var mainObject = m_MainObject;
466 if (File.Trash(m_AssetPath))
467 InvalidateInstance();
468
469 return mainObject;
470 }
471
481 [ExcludeFromCodeCoverage] // simple relay
482 public void SetLabels(String[] labels) => Label.SetAll(m_MainObject, labels);
483
493 public void ClearLabels() => Label.ClearAll(m_MainObject);
494
502 public void RemoveLabel(String label) => Label.Remove(m_MainObject, label);
503
516 public void AddLabel(String label) => Label.Add(m_MainObject, label);
517
526 public void AddLabels(String[] labels) => Label.Add(m_MainObject, labels);
527
538 [ExcludeFromCodeCoverage] // simple relay
539 public void ExportPackage(String packagePath, ExportPackageOptions options = ExportPackageOptions.Default) =>
540 Package.Export(m_AssetPath, packagePath, options);
541
551 public void AddSubAsset(Object instance) => SubAsset.Add(instance, m_MainObject);
552
561 public void RemoveSubAsset(Object subAsset) => SubAsset.Remove(subAsset);
562
563 private void InvalidateInstance()
564 {
565 m_AssetPath = null;
566 m_MainObject = null;
567 }
568
569 private void SetAssetPathFromObject() => m_AssetPath = Path.Get(m_MainObject);
570
571 private void InitWithPath(Path path)
572 {
573 ThrowIf.ArgumentIsNull(path, nameof(path));
574 ThrowIf.DoesNotExistInFileSystem(path);
575
576 m_AssetPath = path;
577 m_MainObject = Status.IsImported(path) ? LoadMain<Object>() : File.ImportAndLoad<Object>(path);
578
579 ThrowIf.AssetLoadReturnedNull(m_MainObject, m_AssetPath);
580 }
581
582 private void InitWithMainObject(Object mainObject)
583 {
584 ThrowIf.ArgumentIsNull(mainObject, nameof(mainObject));
585 ThrowIf.NotInDatabase(mainObject);
586
587 m_MainObject = mainObject;
588 m_AssetPath = Path.Get(mainObject);
589 }
590
591 private void InitWithGuid(GUID guid)
592 {
593 ThrowIf.NotAnAssetGuid(guid);
594
595 InitWithPath(Path.Get(guid));
596 }
597 }
598}
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.
Definition Asset.File.cs:33
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'.
Definition Asset.Path.cs:25
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.
Definition Asset.cs:390
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.
Definition Asset.cs:349
Asset(Byte[] contents, Path path, Boolean overwriteExisting=false)
Creates an asset file from a byte array.
Definition Asset.cs:104
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.
Definition Asset.cs:129
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.
Definition Asset.cs:463
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.
Definition Asset.cs:158
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.
Definition Asset.cs:273
Object Delete()
Deletes the asset file.
Definition Asset.cs:440
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 ...