diff --git a/CHANGELOG.md b/CHANGELOG.md index ac10751..de2dc93 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,15 @@ # Change Log +#### v1.9.0 - Jan 31, 2024 + +- API Changes: These static methods have been MOVED from **Asset** to **Asset.File**: + - GetMainType + - GetSubType + - GetGuid + - GetFileId + - GetGuidAndFileId +- Rationale: These methods hindered API discovery via auto-completion by cluttering the Asset namespace, and they truly belong to the File API since they query asset file attributes. + #### v1.8.6 - Jan 30, 2024 - added Sample: Asset Workflow with GUI. Allows selecting between CodeSmile and Unity API. Unity API code is 33% more lines/characters, with folder creation hardcoded. diff --git a/Editor/Asset.File.cs b/Editor/Asset.File.cs index 4ff30b3..19ad63a 100644 --- a/Editor/Asset.File.cs +++ b/Editor/Asset.File.cs @@ -880,6 +880,126 @@ public static Boolean Delete([NotNull] IEnumerable paths) => public static Boolean Trash([NotNull] IEnumerable paths) => AssetDatabase.MoveAssetsToTrash(paths.ToArray(), s_PathsNotDeleted = new List()); + /// + /// Returns the type of the main asset at the path. + /// + /// Path to an asset. + /// Type of the asset. Null if the path does not exist. + /// + /// - + /// AssetDatabase.GetMainAssetTypeAtPath + /// + public static Type GetMainType([NotNull] Path path) => AssetDatabase.GetMainAssetTypeAtPath(path); + + /// + /// Returns the type of the main asset for the GUID. + /// + /// + /// In Unity 2023.2 it uses AssetDatabase.GetMainAssetTypeFromGUID. + /// The method exists in 2022.2 but not in the early patch versions 0f1 through 6f1. + /// In earlier versions the type is obtained from the path's GUID. + /// + /// Guid of an asset. + /// Type of the asset. Null if the guid is not known or not an asset. + /// + /// - + /// AssetDatabase.GetMainAssetTypeFromGUID + /// + [ExcludeFromCodeCoverage] // simple relay + public static Type GetMainType(GUID guid) + { +#if UNITY_2023_2_OR_NEWER // It's also available in 2022.2 but not in the early patch versions (eg 7f1 onwards) + return AssetDatabase.GetMainAssetTypeFromGUID(guid); +#else + return GetMainType(Path.Get(guid)); +#endif + } + + /// + /// Gets the type of a sub asset by the main asset's path and the local file ID of the sub-asset. + /// + /// Path to an asset. + /// Local file ID of the sub-asset. + /// Type of the SubAsset, or null. + /// + /// - + /// AssetDatabase.GetTypeFromPathAndFileID + /// + [ExcludeFromCodeCoverage] // simple relay + public static Type GetSubType([NotNull] Path path, Int64 fileId) => AssetDatabase.GetTypeFromPathAndFileID(path, fileId); + + /// + /// Example usage: + /// + /// var (guid, fileId) = Asset.GetGuidAndFileId(obj); + /// + /// + /// Object from which GUID and FileId should be obtained. + /// The GUID and local File ID of the object. Returns an empty GUID and 0 if obj is null or not an asset. + /// + /// - + /// - + /// - + /// AssetDatabase.TryGetGUIDAndLocalFileIdentifier + /// + + // ValueTuple makes doxygen accept it as documented, see: https://github.com/doxygen/doxygen/issues/9618 + public static ValueTuple GetGuidAndFileId([NotNull] Object asset) + { + if (asset == null) + return (new GUID(), 0L); + + // explicit variable + assign because Unity 2021 has both long and int variants of the TryGetGUID* method + var localId = Int64.MaxValue; + return AssetDatabase.TryGetGUIDAndLocalFileIdentifier(asset, out var guid, out localId) + ? (new GUID(guid), localId) + : (new GUID(), 0L); + } + + /// + /// Returns the GUID of an object. Returns an empty GUID if the object is null or not an asset. + /// + /// An asset instance. + /// The GUID of the asset. Returns empty GUID if the asset is null or not an asset. + /// + /// - + /// - + /// - + /// AssetDatabase.TryGetGUIDAndLocalFileIdentifier + /// + public static GUID GetGuid([NotNull] Object asset) + { + if (asset == null) + return new GUID(); + + // explicit variable + assign because Unity 2021 has both long and int variants of the TryGetGUID* method + var localId = Int64.MaxValue; + return AssetDatabase.TryGetGUIDAndLocalFileIdentifier(asset, out var guid, out localId) + ? new GUID(guid) + : new GUID(); + } + + /// + /// Returns the local FileID of the object. + /// + /// + /// The local fileID or 0 if obj is null or not an asset. + /// + /// - + /// - + /// - + /// AssetDatabase.TryGetGUIDAndLocalFileIdentifier + /// + public static Int64 GetFileId([NotNull] Object asset) + { + if (asset == null) + return 0L; + + // explicit variable + assign because Unity 2021 has both long and int variants of the TryGetGUID* method + var localId = Int64.MaxValue; + return AssetDatabase.TryGetGUIDAndLocalFileIdentifier(asset, out var _, out localId) ? localId : 0L; + } + // Internal on purpose: use Asset.File.BatchEditing(Action) instead [ExcludeFromCodeCoverage] // untestable internal static void StartAssetEditing() => AssetDatabase.StartAssetEditing(); diff --git a/Editor/Asset.Importer.cs b/Editor/Asset.Importer.cs index 53d6700..a2079eb 100644 --- a/Editor/Asset.Importer.cs +++ b/Editor/Asset.Importer.cs @@ -72,7 +72,7 @@ public static Type GetActive(GUID guid) /// - /// AssetDatabase.GetImporterType /// - public static Type GetActive([NotNull] Object asset) => GetActive(GetGuid(asset)); + public static Type GetActive([NotNull] Object asset) => GetActive(File.GetGuid(asset)); /// /// Gets the active AssetImporter types used for the given assets. diff --git a/Editor/Asset.Properties.cs b/Editor/Asset.Properties.cs index ca0e960..ed1a632 100644 --- a/Editor/Asset.Properties.cs +++ b/Editor/Asset.Properties.cs @@ -50,7 +50,7 @@ public Object MainObject /// /// - /// - public Type MainObjectType => GetMainType(m_AssetPath); + public Type MainObjectType => File.GetMainType(m_AssetPath); /// /// Returns the path to the asset (file or folder). @@ -84,7 +84,7 @@ public Object MainObject /// - /// [ExcludeFromCodeCoverage] // simple relay - public Int64 FileId => GetFileId(m_MainObject); + public Int64 FileId => File.GetFileId(m_MainObject); /// /// Returns the icon texture associated with the asset type. diff --git a/Editor/Asset.Static.cs b/Editor/Asset.Static.cs index 01d54af..f1c3339 100644 --- a/Editor/Asset.Static.cs +++ b/Editor/Asset.Static.cs @@ -13,125 +13,7 @@ public sealed partial class Asset { private static String s_LastErrorMessage = String.Empty; - /// - /// Returns the type of the main asset at the path. - /// - /// Path to an asset. - /// Type of the asset. Null if the path does not exist. - /// - /// - - /// AssetDatabase.GetMainAssetTypeAtPath - /// - public static Type GetMainType([NotNull] Path path) => AssetDatabase.GetMainAssetTypeAtPath(path); - - /// - /// Returns the type of the main asset for the GUID. - /// - /// - /// In Unity 2023.2 it uses AssetDatabase.GetMainAssetTypeFromGUID. - /// The method exists in 2022.2 but not in the early patch versions 0f1 through 6f1. - /// In earlier versions the type is obtained from the path's GUID. - /// - /// Guid of an asset. - /// Type of the asset. Null if the guid is not known or not an asset. - /// - /// - - /// AssetDatabase.GetMainAssetTypeFromGUID - /// - [ExcludeFromCodeCoverage] // simple relay - public static Type GetMainType(GUID guid) - { -#if UNITY_2023_2_OR_NEWER // It's also available in 2022.2 but not in the early patch versions (eg 7f1 onwards) - return AssetDatabase.GetMainAssetTypeFromGUID(guid); -#else - return GetMainType(Path.Get(guid)); -#endif - } - - /// - /// Gets the type of a sub asset by the main asset's path and the local file ID of the sub-asset. - /// - /// Path to an asset. - /// Local file ID of the sub-asset. - /// Type of the SubAsset, or null. - /// - /// - - /// AssetDatabase.GetTypeFromPathAndFileID - /// - [ExcludeFromCodeCoverage] // simple relay - public static Type GetSubType([NotNull] Path path, Int64 fileId) => AssetDatabase.GetTypeFromPathAndFileID(path, fileId); - - /// - /// Example usage: - /// - /// var (guid, fileId) = Asset.GetGuidAndFileId(obj); - /// - /// - /// Object from which GUID and FileId should be obtained. - /// The GUID and local File ID of the object. Returns an empty GUID and 0 if obj is null or not an asset. - /// - /// - - /// - - /// - - /// AssetDatabase.TryGetGUIDAndLocalFileIdentifier - /// - - // ValueTuple makes doxygen accept it as documented, see: https://github.com/doxygen/doxygen/issues/9618 - public static ValueTuple GetGuidAndFileId([NotNull] Object asset) - { - if (asset == null) - return (new GUID(), 0L); - - // explicit variable + assign because Unity 2021 has both long and int variants of the TryGetGUID* method - var localId = Int64.MaxValue; - return AssetDatabase.TryGetGUIDAndLocalFileIdentifier(asset, out var guid, out localId) - ? (new GUID(guid), localId) - : (new GUID(), 0L); - } - /// - /// Returns the GUID of an object. Returns an empty GUID if the object is null or not an asset. - /// - /// An asset instance. - /// The GUID of the asset. Returns empty GUID if the asset is null or not an asset. - /// - /// - - /// - - /// - - /// AssetDatabase.TryGetGUIDAndLocalFileIdentifier - /// - public static GUID GetGuid([NotNull] Object asset) - { - if (asset == null) - return new GUID(); - - // explicit variable + assign because Unity 2021 has both long and int variants of the TryGetGUID* method - var localId = Int64.MaxValue; - return AssetDatabase.TryGetGUIDAndLocalFileIdentifier(asset, out var guid, out localId) - ? new GUID(guid) - : new GUID(); - } - - /// - /// Returns the local FileID of the object. - /// - /// - /// The local fileID or 0 if obj is null or not an asset. - /// - /// - - /// - - /// - - /// AssetDatabase.TryGetGUIDAndLocalFileIdentifier - /// - public static Int64 GetFileId([NotNull] Object asset) - { - if (asset == null) - return 0L; - - // explicit variable + assign because Unity 2021 has both long and int variants of the TryGetGUID* method - var localId = Int64.MaxValue; - return AssetDatabase.TryGetGUIDAndLocalFileIdentifier(asset, out var _, out localId) ? localId : 0L; - } /// /// Returns the icon associated with the asset type. diff --git a/Tests/Editor/AssetLoadTests.cs b/Tests/Editor/AssetLoadTests.cs index 7da64b5..b367a9a 100644 --- a/Tests/Editor/AssetLoadTests.cs +++ b/Tests/Editor/AssetLoadTests.cs @@ -74,7 +74,7 @@ [Test] public void LoadMainStatic_ExistingGuid_Succeeds() Assert.NotNull(loaded); Assert.AreEqual(obj, loaded); Assert.AreEqual(obj.GetType(), loaded.GetType()); - Assert.AreEqual(obj.GetType(), Asset.GetMainType((String)TestAssetPath)); + Assert.AreEqual(obj.GetType(), Asset.File.GetMainType((String)TestAssetPath)); } [Test] public void LoadMainStatic_TypeMismatch_ReturnsNull() diff --git a/Tests/Editor/AssetStatusTests.cs b/Tests/Editor/AssetStatusTests.cs index 0edcbf8..a4dc0db 100644 --- a/Tests/Editor/AssetStatusTests.cs +++ b/Tests/Editor/AssetStatusTests.cs @@ -22,6 +22,6 @@ [Test] public void IsImported_ExistingAsset_True() => [Test] public void IsImported_ExistingPath_True() => Assert.IsTrue(Asset.Status.IsImported(CreateTestAsset(TestAssetPath).AssetPath)); - [Test] public void GetMainType_NullPath_False() => Assert.Null(Asset.GetMainType((String)TestAssetPath)); + [Test] public void GetMainType_NullPath_False() => Assert.Null(Asset.File.GetMainType((String)TestAssetPath)); } } diff --git a/package.json b/package.json index c7addae..c18e7c1 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "de.codesmile.assetdatabase", "displayName": "CodeSmile AssetDatabase", "description": "The AssetDatabase in enjoyable, consistent, concise, convenient, comprehensible, safe, documented form.\n\nAvailable under multiple licenses:\n- GPL 3.0 for personal & educational use\n- Asset Store Terms and EULA for commercial use", - "version": "1.8.6", + "version": "1.9.0", "unity": "2021.3", "unityRelease": "3f1", "author": {