Skip to content

Commit

Permalink
Bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ErnSur committed Aug 5, 2024
1 parent 67724d5 commit 34be27f
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 23 deletions.
1 change: 1 addition & 0 deletions Packages/com.quickeye.one-asset/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Fixed
- Fixed NRE in header drawer
- Fixed asset creation from InitializeOnLoad callbacks in Unity 6

## [4.0.3] - 2024-01-03

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private static bool ShouldDrawHeader(UnityEditor.Editor editor, out AssetMetadat
return editor.targets.Length == 1 &&
EditorUtility.IsPersistent(editor.target) &&
LoadFromAssetCache.TryGetEntry(editor.serializedObject.targetObject, out metadata) &&
metadata.LoadOptions.Paths.Length > 0;
metadata.LoadOptions?.Paths?.Length > 0;
}

private readonly AssetMetadata _metadata;
Expand Down
20 changes: 11 additions & 9 deletions Packages/com.quickeye.one-asset/Runtime/OneAssetLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,8 @@ public static Object Load(AssetLoadOptions options, Type assetType)
return asset;

// Try to create asset at path
if (options.CreateAssetIfMissing &&
typeof(ScriptableObject).IsAssignableFrom(assetType) &&
TryCreateAsset(assetType, options) &&
TryLoad(assetType, options, out asset))
return asset;
if (options.CreateAssetIfMissing && TryCreateAsset(assetType, options, out var so))
return so;

// Throw if asset is mandatory
if (options.AssetIsMandatory)
Expand Down Expand Up @@ -92,14 +89,19 @@ public static T Load<T>() where T : Object
return Load(typeof(T)) as T;
}

private static bool TryCreateAsset(Type type, AssetLoadOptions options)
private static bool TryCreateAsset(Type type, AssetLoadOptions options, out ScriptableObject obj)
{
if (!Application.isEditor)
return false;
if (EditorFeatures == null)
throw new NotImplementedException(
"Asset trying to be created in editor, but editor features are missing.");
var obj = ScriptableObject.CreateInstance(type);

if (!typeof(ScriptableObject).IsAssignableFrom(type))
{
throw new ArgumentException(
$"Type {type} is not a ScriptableObject and cannot be created as an asset.");
}

obj = ScriptableObject.CreateInstance(type);
try
{
EditorFeatures.CreateAsset(obj, options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,37 @@

namespace QuickEye.OneAsset.Editor.Tests
{
using System;
using UnityEngine;
using static TestUtils;

[TestOf(typeof(OneAssetLoader))]
[InitializeOnLoad]
public class AutomaticAssetCreationTests
{
private static bool _initializeOnLoadTestsPassed;
private static Exception _initializeOnLoadException;

static AutomaticAssetCreationTests()
{
RunInitializeOnLoadTests();
}

private static void RunInitializeOnLoadTests()
{
var tests = new AutomaticAssetCreationTests();
try
{
var tests = new AutomaticAssetCreationTests();
tests.Setup();
tests.Should_CreateNewAsset_When_TypeHasCreateAutomaticallyAttributeAndAssetIsMissing();
tests.Teardown();
tests.OneTimeTearDown();
_initializeOnLoadTestsPassed = true;
}
catch (AssertionException)
catch (Exception e)
{
_initializeOnLoadException = e;
}
finally
{
tests.Teardown();
tests.OneTimeTearDown();
}
}

Expand All @@ -53,16 +60,24 @@ public void OneTimeTearDown()
[Test]
public void InitializeOnLoadTests()
{
Assert.IsTrue(_initializeOnLoadTestsPassed);
if (_initializeOnLoadException == null)
return;
Debug.LogException(_initializeOnLoadException);
Assert.Fail("Failed");
}

[Test]
public void Should_CreateNewAsset_When_TypeHasCreateAutomaticallyAttributeAndAssetIsMissing()
{
var asset = OneAssetLoader.Load<SoWithCreateAutomatically>();

var assetPath = AssetDatabase.GetAssetPath(asset);
StringAssert.Contains(SoWithCreateAutomatically.AbsoluteAssetPath, assetPath);
Assert.IsNotNull(asset);
FileAssert.Exists(SoWithCreateAutomatically.AbsoluteAssetPathWithExtension);

// Since Unity 6 asset database does not see assets created from InitializeOnLoad callback
// https://issuetracker.unity3d.com/issues/resources-dot-load-fails-to-load-assets-created-in-the-same-frame-when-called-from-a-function-with-the-initializeonloadmethod-attribute
// https://unity3d.atlassian.net/servicedesk/customer/portal/2/IN-77788
// StringAssert.Contains(SoWithCreateAutomatically.AbsoluteAssetPathWithExtension, AssetDatabase.GetAssetPath(asset));
}

[Test]
Expand All @@ -82,7 +97,7 @@ public void Should_CreateNewAsset_When_PathHasNoFileExtension()
{
CreateAssetIfMissing = true
};

var asset = OneAssetLoader.Load(options, typeof(SoWithAsset));

Assert.IsTrue(AssetDatabase.Contains(asset));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace QuickEye.OneAsset.Editor.Tests.SampleAssets
{
[LoadFromAsset(AbsoluteAssetPath, CreateAssetIfMissing = true)]
[LoadFromAsset(AbsoluteAssetPathWithExtension, CreateAssetIfMissing = true)]
internal class SoWithCreateAutomatically : ScriptableObject
{
public const string AbsoluteAssetPath =
public const string AbsoluteAssetPathWithExtension =
TestUtils.TempDir
+ "Resources/one-asset-tests/" + nameof(SoWithCreateAutomatically);
+ "Resources/one-asset-tests/" + nameof(SoWithCreateAutomatically) + ".asset";
}
}

0 comments on commit 34be27f

Please sign in to comment.