Skip to content

Commit

Permalink
feat: add AssetDatabaseAssetResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
jonisavo committed May 26, 2023
1 parent 42cb716 commit 57153a8
Show file tree
Hide file tree
Showing 13 changed files with 134 additions and 4 deletions.
@@ -0,0 +1,29 @@
using NUnit.Framework;
using UIComponents.Editor;
using Unity.PerformanceTesting;

namespace UIComponents.Benchmarks.AssetLoading
{
[TestFixture]
public partial class AssetDatabaseAssetLoadBenchmarks
{
[AssetPrefix("Assets/Samples/Addressables/Data/")]
[Layout("AddressablesExampleComponent.uxml")]
[Stylesheet("AddressablesExampleComponent.uss")]
[Stylesheet("Box.uss")]
[Dependency(typeof(IAssetResolver), provide: typeof(AssetDatabaseAssetResolver), Scope.Transient)]
private partial class ComponentWithAssets : UIComponent {}

[Test, Performance, Version(BenchmarkUtils.Version)]
public void InitializeComponentWithWarmCache()
{
BenchmarkUtils.MeasureComponentInitWithWarmCache<ComponentWithAssets>();
}

[Test, Performance, Version(BenchmarkUtils.Version)]
public void InitializeComponentWithColdCache()
{
BenchmarkUtils.MeasureComponentInitWithColdCache<ComponentWithAssets>();
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Assets/UIComponents.Benchmarks/BenchmarkUtils.cs
Expand Up @@ -5,7 +5,7 @@ namespace UIComponents.Benchmarks
{
public static class BenchmarkUtils
{
public const string Version = "0.30.0.0";
public const string Version = "0.31.0.0";

private static SampleGroup[] GetProfilerMarkers()
{
Expand Down
Expand Up @@ -6,7 +6,8 @@
"UnityEngine.TestRunner",
"UnityEditor.TestRunner",
"UIComponents",
"UIComponents.Addressables"
"UIComponents.Addressables",
"UIComponents.Editor"
],
"includePlatforms": [
"Editor"
Expand Down
@@ -0,0 +1,34 @@
using System.Collections;
using NUnit.Framework;
using UIComponents.Editor;
using UnityEngine.TestTools;
using UnityEngine.UIElements;

namespace UIComponents.Tests.Editor
{
[TestFixture]
public class AssetDatabaseAssetResolverTests : AssetResolverTestSuite<AssetDatabaseAssetResolver>
{
[UnityTest]
public IEnumerator Should_Be_Able_To_Load_Existing_Asset()
{
yield return Assert_Loads_Existing_Asset<StyleSheet>(
"Assets/UIComponents.Tests/Addressables/Assets/Component.uss"
);
yield return Assert_Loads_Existing_Asset<VisualTreeAsset>(
"Assets/UIComponents.Tests/Addressables/Assets/Component.uxml"
);
}

[UnityTest]
public IEnumerator Should_Be_Able_To_Tell_If_Asset_Exists()
{
yield return Assert_Tells_If_Asset_Exists(
"Assets/UIComponents.Tests/Addressables/Assets/Component.uss"
);
yield return Assert_Tells_If_Asset_Exists(
"Assets/UIComponents.Tests/Addressables/Assets/Component.uxml"
);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Expand Up @@ -6,7 +6,8 @@
"UnityEditor.TestRunner",
"UIComponents",
"UIComponents.Tests",
"UIComponents.Testing"
"UIComponents.Testing",
"UIComponents.Editor"
],
"includePlatforms": [
"Editor"
Expand Down
3 changes: 3 additions & 0 deletions Assets/UIComponents/Editor.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions Assets/UIComponents/Editor/AssetDatabaseAssetResolver.cs
@@ -0,0 +1,28 @@
using System.Threading.Tasks;
using UnityEditor;
using UnityEngine;

namespace UIComponents.Editor
{
/// <summary>
/// An IAssetResolver which loads assets with AssetDatabase.
/// </summary>
/// <seealso cref="DependencyAttribute"/>
/// <seealso cref="UIComponent"/>
public class AssetDatabaseAssetResolver : IAssetResolver
{
public Task<T> LoadAsset<T>(string assetPath) where T : Object
{
var asset = AssetDatabase.LoadAssetAtPath<T>(assetPath);

return Task.FromResult(asset);
}

public Task<bool> AssetExists(string assetPath)
{
var asset = AssetDatabase.LoadAssetAtPath<Object>(assetPath);

return Task.FromResult(asset != null);
}
}
}
3 changes: 3 additions & 0 deletions Assets/UIComponents/Editor/AssetDatabaseAssetResolver.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions Assets/UIComponents/Editor/UIComponents.Editor.asmdef
@@ -0,0 +1,18 @@
{
"name": "UIComponents.Editor",
"rootNamespace": "UIComponents",
"references": [
"GUID:d593635333b4cae48bac5b5f0b596e90"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
7 changes: 7 additions & 0 deletions Assets/UIComponents/Editor/UIComponents.Editor.asmdef.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Expand Up @@ -6,7 +6,7 @@
{
"type": "System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
"key": "IncludeAssemblies",
"value": "{\"m_Value\":\"UIComponents,UIComponents.Addressables,UIComponents.Testing\"}"
"value": "{\"m_Value\":\"UIComponents,UIComponents.Addressables,UIComponents.Editor,UIComponents.Testing\"}"
},
{
"type": "System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
Expand Down

0 comments on commit 57153a8

Please sign in to comment.