Skip to content

Commit

Permalink
feat!: rework asset handling
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The new ResourcesAssetResolver is
now used by default. AssetDatabaseAssetResolver has been
moved to the new UIComponents.Editor assembly.
  • Loading branch information
jonisavo committed Apr 29, 2022
1 parent 7803390 commit 1c50b26
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 53 deletions.
12 changes: 0 additions & 12 deletions Core/AssetDatabaseAssetLoader.cs

This file was deleted.

4 changes: 3 additions & 1 deletion Core/IAssetLoader.cs → Core/IAssetResolver.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
namespace UIComponents.Core
{
public interface IAssetLoader
public interface IAssetResolver
{
public T LoadAsset<T>(string assetPath) where T : UnityEngine.Object;

public bool AssetExists(string assetPath);
}
}
File renamed without changes.
37 changes: 4 additions & 33 deletions Core/PathAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;

namespace UIComponents.Core
Expand All @@ -14,13 +12,8 @@ public string GetAssetPathForComponent(UIComponent component)
if (Path == null)
Path = string.Empty;

if (!ConfiguredPathIsComplete())
{
if (TryGetPathFromComponent(component, out var pathFromComponent))
Path = pathFromComponent;
else if (TryGetPathFromAssembly(component, out var pathFromAssembly))
Path = pathFromAssembly;
}
if (!ConfiguredPathIsComplete() && TryGetPathFromComponent(component, out var path))
Path = path;

return Path;
}
Expand All @@ -35,33 +28,11 @@ private bool TryGetPathFromComponent(UIComponent component, out string path)
{
path = "";

return TryGetValidAssetPath(component.GetAssetPaths(), out path);
}

private bool TryGetPathFromAssembly(UIComponent component, out string path)
{
path = "";

var assembly = component.GetType().Assembly;

var paths =
assembly.GetCustomAttributes(typeof(AssetPathAttribute), false)
.Select(attribute => ((AssetPathAttribute)attribute).Path );

return TryGetValidAssetPath(paths, out path);
}

private bool TryGetValidAssetPath(
IEnumerable<string> paths,
out string path)
{
path = "";

foreach (var pathPart in paths)
foreach (var pathPart in component.GetAssetPaths())
{
var filePath = string.Join("/", pathPart, Path);

if (string.IsNullOrEmpty(AssetDatabase.AssetPathToGUID(filePath)))
if (!component.AssetResolver.AssetExists(filePath))
continue;

path = filePath;
Expand Down
17 changes: 17 additions & 0 deletions Core/ResourcesAssetResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using UnityEngine;

namespace UIComponents.Core
{
public class ResourcesAssetResolver : IAssetResolver
{
public T LoadAsset<T>(string assetPath) where T : UnityEngine.Object
{
return Resources.Load<T>(assetPath);
}

public bool AssetExists(string assetPath)
{
return Resources.Load(assetPath) != null;
}
}
}
3 changes: 3 additions & 0 deletions Core/ResourcesAssetResolver.cs.meta

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

29 changes: 22 additions & 7 deletions Core/UIComponent.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using UnityEngine;
using UnityEngine.UIElements;

namespace UIComponents.Core
{
[Dependency(typeof(IAssetLoader), provide: typeof(AssetDatabaseAssetLoader))]
[Dependency(typeof(IAssetResolver), provide: typeof(ResourcesAssetResolver))]
public abstract class UIComponent : VisualElement
{
private static readonly Dictionary<Type, LayoutAttribute> LayoutAttributeDictionary =
Expand All @@ -18,7 +19,7 @@ public abstract class UIComponent : VisualElement

internal readonly DependencyInjector DependencyInjector;

private readonly IAssetLoader _assetLoader;
internal readonly IAssetResolver AssetResolver;

private readonly Type _componentType;

Expand All @@ -27,13 +28,13 @@ protected UIComponent()
_componentType = GetType();
DependencyInjector = DependencyInjector.GetInjector(_componentType);

_assetLoader = DependencyInjector.Provide<IAssetLoader>();
AssetResolver = DependencyInjector.Provide<IAssetResolver>();

if (!LayoutAttributeDictionary.ContainsKey(_componentType))
LayoutAttributeDictionary[_componentType] = GetSingleAttribute<LayoutAttribute>();

if (!AssetPathAttributesDictionary.ContainsKey(_componentType))
AssetPathAttributesDictionary[_componentType] = GetAttributes<AssetPathAttribute>();
AssetPathAttributesDictionary[_componentType] = GetAssetPathAttributes();

if (!StylesheetAttributesDictionary.ContainsKey(_componentType))
StylesheetAttributesDictionary[_componentType] = GetAttributes<StylesheetAttribute>();
Expand Down Expand Up @@ -65,7 +66,7 @@ protected virtual VisualTreeAsset GetLayout()

var assetPath = layoutAttribute.GetAssetPathForComponent(this);

return _assetLoader.LoadAsset<VisualTreeAsset>(assetPath);
return AssetResolver.LoadAsset<VisualTreeAsset>(assetPath);
}

protected virtual StyleSheet[] GetStyleSheets()
Expand All @@ -77,7 +78,7 @@ protected virtual StyleSheet[] GetStyleSheets()
for (var i = 0; i < stylesheetAttributes.Length; i++)
{
var assetPath = stylesheetAttributes[i].GetAssetPathForComponent(this);
var styleSheet = _assetLoader.LoadAsset<StyleSheet>(assetPath);
var styleSheet = AssetResolver.LoadAsset<StyleSheet>(assetPath);

if (styleSheet == null)
{
Expand Down Expand Up @@ -107,6 +108,19 @@ protected virtual StyleSheet[] GetStyleSheets()
return (T[]) _componentType.GetCustomAttributes(typeof(T), true);
}

private AssetPathAttribute[] GetAssetPathAttributes()
{
var assembly = _componentType.Assembly;

var assetPathAttributes =
GetAttributes<AssetPathAttribute>()
.Concat(
(AssetPathAttribute[]) assembly.GetCustomAttributes(typeof(AssetPathAttribute), false)
).ToArray();

return assetPathAttributes;
}

private void LoadLayout()
{
var layoutAsset = GetLayout();
Expand All @@ -127,7 +141,8 @@ private void LoadStyles()
}

foreach (var sheet in loadedStyleSheets)
styleSheets.Add(sheet);
if (sheet != null)
styleSheets.Add(sheet);
}
}
}
3 changes: 3 additions & 0 deletions Editor.meta

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

18 changes: 18 additions & 0 deletions Editor/AssetDatabaseAssetResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using UIComponents.Core;
using UnityEditor;

namespace UIComponents.Editor
{
public class AssetDatabaseAssetResolver : IAssetResolver
{
public T LoadAsset<T>(string assetPath) where T : UnityEngine.Object
{
return AssetDatabase.LoadAssetAtPath<T>(assetPath);
}

public bool AssetExists(string assetPath)
{
return !string.IsNullOrEmpty(AssetDatabase.AssetPathToGUID(assetPath));
}
}
}
File renamed without changes.
18 changes: 18 additions & 0 deletions Editor/UIComponents.Editor.asmdef
Original file line number Diff line number Diff line change
@@ -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 Editor/UIComponents.Editor.asmdef.meta

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

0 comments on commit 1c50b26

Please sign in to comment.