Skip to content

Commit

Permalink
moved several getters from Asset to Asset.File to make for better API…
Browse files Browse the repository at this point in the history
… discovery
  • Loading branch information
CodeSmile-0000011110110111 committed Jan 31, 2024
1 parent 4573ff4 commit 31d5d9a
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 124 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
120 changes: 120 additions & 0 deletions Editor/Asset.File.cs
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,126 @@ public static Boolean Delete([NotNull] IEnumerable<String> paths) =>
public static Boolean Trash([NotNull] IEnumerable<String> paths) =>
AssetDatabase.MoveAssetsToTrash(paths.ToArray(), s_PathsNotDeleted = new List<String>());

/// <summary>
/// Returns the type of the main asset at the path.
/// </summary>
/// <param name="path">Path to an asset.</param>
/// <returns>Type of the asset. Null if the path does not exist.</returns>
/// <seealso cref="">
/// -
/// <a href="https://docs.unity3d.com/ScriptReference/AssetDatabase.GetMainAssetTypeAtPath.html">AssetDatabase.GetMainAssetTypeAtPath</a>
/// </seealso>
public static Type GetMainType([NotNull] Path path) => AssetDatabase.GetMainAssetTypeAtPath(path);

/// <summary>
/// Returns the type of the main asset for the GUID.
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
/// <param name="guid">Guid of an asset.</param>
/// <returns>Type of the asset. Null if the guid is not known or not an asset.</returns>
/// <seealso cref="">
/// -
/// <a href="https://docs.unity3d.com/ScriptReference/AssetDatabase.GetMainAssetTypeFromGUID.html">AssetDatabase.GetMainAssetTypeFromGUID</a>
/// </seealso>
[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
}

/// <summary>
/// Gets the type of a sub asset by the main asset's path and the local file ID of the sub-asset.
/// </summary>
/// <param name="path">Path to an asset.</param>
/// <param name="fileId">Local file ID of the sub-asset.</param>
/// <returns>Type of the SubAsset, or null.</returns>
/// <seealso cref="">
/// -
/// <a href="https://docs.unity3d.com/ScriptReference/AssetDatabase.GetTypeFromPathAndFileID.html">AssetDatabase.GetTypeFromPathAndFileID</a>
/// </seealso>
[ExcludeFromCodeCoverage] // simple relay
public static Type GetSubType([NotNull] Path path, Int64 fileId) => AssetDatabase.GetTypeFromPathAndFileID(path, fileId);

/// <example>
/// Example usage:
/// <code>
/// var (guid, fileId) = Asset.GetGuidAndFileId(obj);
/// </code>
/// </example>
/// <param name="asset">Object from which GUID and FileId should be obtained.</param>
/// <returns>The GUID and local File ID of the object. Returns an empty GUID and 0 if obj is null or not an asset. </returns>
/// <seealso cref="">
/// - <see cref="CodeSmileEditor.Asset.GetGuid" />
/// - <see cref="CodeSmileEditor.Asset.GetFileId" />
/// -
/// <a href="https://docs.unity3d.com/ScriptReference/AssetDatabase.TryGetGUIDAndLocalFileIdentifier.html">AssetDatabase.TryGetGUIDAndLocalFileIdentifier</a>
/// </seealso>

// ValueTuple makes doxygen accept it as documented, see: https://github.com/doxygen/doxygen/issues/9618
public static ValueTuple<GUID, Int64> 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);
}

/// <summary>
/// Returns the GUID of an object. Returns an empty GUID if the object is null or not an asset.
/// </summary>
/// <param name="asset">An asset instance.</param>
/// <returns>The GUID of the asset. Returns empty GUID if the asset is null or not an asset.</returns>
/// <seealso cref="">
/// - <see cref="CodeSmileEditor.Asset.GetFileId" />
/// - <see cref="CodeSmileEditor.Asset.GetGuidAndFileId" />
/// -
/// <a href="https://docs.unity3d.com/ScriptReference/AssetDatabase.TryGetGUIDAndLocalFileIdentifier.html">AssetDatabase.TryGetGUIDAndLocalFileIdentifier</a>
/// </seealso>
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();
}

/// <summary>
/// Returns the local FileID of the object.
/// </summary>
/// <param name="asset"></param>
/// <returns>The local fileID or 0 if obj is null or not an asset.</returns>
/// <seealso cref="">
/// - <see cref="CodeSmileEditor.Asset.GetGuid" />
/// - <see cref="CodeSmileEditor.Asset.GetGuidAndFileId" />
/// -
/// <a href="https://docs.unity3d.com/ScriptReference/AssetDatabase.TryGetGUIDAndLocalFileIdentifier.html">AssetDatabase.TryGetGUIDAndLocalFileIdentifier</a>
/// </seealso>
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();
Expand Down
2 changes: 1 addition & 1 deletion Editor/Asset.Importer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static Type GetActive(GUID guid)
/// -
/// <a href="https://docs.unity3d.com/ScriptReference/AssetDatabase.GetImporterType.html">AssetDatabase.GetImporterType</a>
/// </seealso>
public static Type GetActive([NotNull] Object asset) => GetActive(GetGuid(asset));
public static Type GetActive([NotNull] Object asset) => GetActive(File.GetGuid(asset));

/// <summary>
/// Gets the active AssetImporter types used for the given assets.
Expand Down
4 changes: 2 additions & 2 deletions Editor/Asset.Properties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public Object MainObject
/// <seealso cref="">
/// - <see cref="CodeSmileEditor.Asset.GetMainType(CodeSmileEditor.Asset.Path)" />
/// </seealso>
public Type MainObjectType => GetMainType(m_AssetPath);
public Type MainObjectType => File.GetMainType(m_AssetPath);

/// <summary>
/// Returns the path to the asset (file or folder).
Expand Down Expand Up @@ -84,7 +84,7 @@ public Object MainObject
/// - <see cref="CodeSmileEditor.Asset.Guid" />
/// </seealso>
[ExcludeFromCodeCoverage] // simple relay
public Int64 FileId => GetFileId(m_MainObject);
public Int64 FileId => File.GetFileId(m_MainObject);

/// <summary>
/// Returns the icon texture associated with the asset type.
Expand Down
118 changes: 0 additions & 118 deletions Editor/Asset.Static.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,125 +13,7 @@ public sealed partial class Asset
{
private static String s_LastErrorMessage = String.Empty;

/// <summary>
/// Returns the type of the main asset at the path.
/// </summary>
/// <param name="path">Path to an asset.</param>
/// <returns>Type of the asset. Null if the path does not exist.</returns>
/// <seealso cref="">
/// -
/// <a href="https://docs.unity3d.com/ScriptReference/AssetDatabase.GetMainAssetTypeAtPath.html">AssetDatabase.GetMainAssetTypeAtPath</a>
/// </seealso>
public static Type GetMainType([NotNull] Path path) => AssetDatabase.GetMainAssetTypeAtPath(path);

/// <summary>
/// Returns the type of the main asset for the GUID.
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
/// <param name="guid">Guid of an asset.</param>
/// <returns>Type of the asset. Null if the guid is not known or not an asset.</returns>
/// <seealso cref="">
/// -
/// <a href="https://docs.unity3d.com/ScriptReference/AssetDatabase.GetMainAssetTypeFromGUID.html">AssetDatabase.GetMainAssetTypeFromGUID</a>
/// </seealso>
[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
}

/// <summary>
/// Gets the type of a sub asset by the main asset's path and the local file ID of the sub-asset.
/// </summary>
/// <param name="path">Path to an asset.</param>
/// <param name="fileId">Local file ID of the sub-asset.</param>
/// <returns>Type of the SubAsset, or null.</returns>
/// <seealso cref="">
/// -
/// <a href="https://docs.unity3d.com/ScriptReference/AssetDatabase.GetTypeFromPathAndFileID.html">AssetDatabase.GetTypeFromPathAndFileID</a>
/// </seealso>
[ExcludeFromCodeCoverage] // simple relay
public static Type GetSubType([NotNull] Path path, Int64 fileId) => AssetDatabase.GetTypeFromPathAndFileID(path, fileId);

/// <example>
/// Example usage:
/// <code>
/// var (guid, fileId) = Asset.GetGuidAndFileId(obj);
/// </code>
/// </example>
/// <param name="asset">Object from which GUID and FileId should be obtained.</param>
/// <returns>The GUID and local File ID of the object. Returns an empty GUID and 0 if obj is null or not an asset. </returns>
/// <seealso cref="">
/// - <see cref="CodeSmileEditor.Asset.GetGuid" />
/// - <see cref="CodeSmileEditor.Asset.GetFileId" />
/// -
/// <a href="https://docs.unity3d.com/ScriptReference/AssetDatabase.TryGetGUIDAndLocalFileIdentifier.html">AssetDatabase.TryGetGUIDAndLocalFileIdentifier</a>
/// </seealso>

// ValueTuple makes doxygen accept it as documented, see: https://github.com/doxygen/doxygen/issues/9618
public static ValueTuple<GUID, Int64> 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);
}

/// <summary>
/// Returns the GUID of an object. Returns an empty GUID if the object is null or not an asset.
/// </summary>
/// <param name="asset">An asset instance.</param>
/// <returns>The GUID of the asset. Returns empty GUID if the asset is null or not an asset.</returns>
/// <seealso cref="">
/// - <see cref="CodeSmileEditor.Asset.GetFileId" />
/// - <see cref="CodeSmileEditor.Asset.GetGuidAndFileId" />
/// -
/// <a href="https://docs.unity3d.com/ScriptReference/AssetDatabase.TryGetGUIDAndLocalFileIdentifier.html">AssetDatabase.TryGetGUIDAndLocalFileIdentifier</a>
/// </seealso>
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();
}

/// <summary>
/// Returns the local FileID of the object.
/// </summary>
/// <param name="asset"></param>
/// <returns>The local fileID or 0 if obj is null or not an asset.</returns>
/// <seealso cref="">
/// - <see cref="CodeSmileEditor.Asset.GetGuid" />
/// - <see cref="CodeSmileEditor.Asset.GetGuidAndFileId" />
/// -
/// <a href="https://docs.unity3d.com/ScriptReference/AssetDatabase.TryGetGUIDAndLocalFileIdentifier.html">AssetDatabase.TryGetGUIDAndLocalFileIdentifier</a>
/// </seealso>
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;
}

/// <summary>
/// Returns the icon associated with the asset type.
Expand Down
2 changes: 1 addition & 1 deletion Tests/Editor/AssetLoadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion Tests/Editor/AssetStatusTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit 31d5d9a

Please sign in to comment.