CodeSmile AssetDatabase 1.10
Unity's AssetDatabase in enjoyable, consistent, concise, convenient, comprehensible, safe, documented form.
Loading...
Searching...
No Matches
Asset.SubAsset.cs
1// Copyright (C) 2021-2023 Steffen Itterheim
2// Refer to included LICENSE file for terms and conditions.
3
4using System;
5using System.Diagnostics.CodeAnalysis;
6using UnityEditor;
7using Object = UnityEngine.Object;
8
9namespace CodeSmileEditor
10{
11 public sealed partial class Asset
12 {
16 public static class SubAsset
17 {
38 [ExcludeFromCodeCoverage] // simple relay
39 public static Boolean Extract([NotNull] Object subAsset, [NotNull] Path destinationPath)
40 {
41 ThrowIf.ArgumentIsNull(subAsset, nameof(subAsset));
42 ThrowIf.ArgumentIsNull(destinationPath, nameof(destinationPath));
43
44 return Succeeded(AssetDatabase.ExtractAsset(subAsset, destinationPath));
45 }
46
58 public static void Add([NotNull] Object subAssetInstance, [NotNull] Object asset)
59 {
60 ThrowIf.ArgumentIsNull(subAssetInstance, nameof(subAssetInstance));
61 ThrowIf.SubObjectIsGameObject(subAssetInstance);
62 ThrowIf.AlreadyAnAsset(subAssetInstance);
63 ThrowIf.ArgumentIsNull(asset, nameof(asset));
64
65 AssetDatabase.AddObjectToAsset(subAssetInstance, asset);
66 }
67
77 public static void Remove([NotNull] Object subAsset)
78 {
79 ThrowIf.ArgumentIsNull(subAsset, nameof(subAsset));
80
81 AssetDatabase.RemoveObjectFromAsset(subAsset);
82 }
83
99 public static void SetMain([NotNull] Object subAsset, [NotNull] Path path)
100 {
101 AssetDatabase.SetMainObject(subAsset, path);
102
103 // SetMainObject only records which object should become the main one on the next import,
104 // it does not write the asset file. On Unity 6000.7.0a5 and 6000.7.0a6, importing without
105 // saving first re-reads the old main object from disk and the change is lost. Saving the
106 // asset before the import makes the change take effect on all nine editors tested.
107 AssetDatabase.SaveAssetIfDirty(subAsset);
108 File.Import(path);
109 }
110
126 public static void SetMain([NotNull] Object subAsset, [NotNull] Object asset) => SetMain(subAsset, Path.Get(asset));
127
147 public static Object[] LoadAll([NotNull] Path path) => AssetDatabase.LoadAllAssetsAtPath(path);
148
170 public static Object[] LoadVisible([NotNull] Path path) => AssetDatabase.LoadAllAssetRepresentationsAtPath(path);
171 }
172 }
173}
static void Import([NotNull] Path path, ImportAssetOptions options=ImportAssetOptions.Default)
Imports a file at a given path that was created or modified 'externally'. Externally refers to any me...
Groups file related operations.
Definition Asset.File.cs:38
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'.
Definition Asset.Path.cs:25
static Object[] LoadAll([NotNull] Path path)
Loads all sub-asset objects of an asset.
static void SetMain([NotNull] Object subAsset, [NotNull] Object asset)
Sets (changes) an asset's 'main' object to one of its sub-assets.
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 Object[] LoadVisible([NotNull] Path path)
Loads only the visible (representation) sub-asset objects of an asset.
static Boolean Extract([NotNull] Object subAsset, [NotNull] Path destinationPath)
Extracts a sub-object of an asset as an asset of its own.
static void SetMain([NotNull] Object subAsset, [NotNull] Path path)
Sets (changes) an asset's 'main' object to one of its sub-assets.
static void Remove([NotNull] Object subAsset)
Removes a sub-object from the asset it is contained in.
Groups all Sub-Asset related functionality.
Replacement implementation for Unity's massive AssetDatabase class with a cleaner interface and more ...