-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: allow specifying asset path with AssetPathAttribute
BREAKING CHANGE: LayoutAttribute and StylesheetAttribute's RelativeTo property has been removed.
- Loading branch information
Showing
4 changed files
with
98 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System; | ||
using JetBrains.Annotations; | ||
|
||
namespace UIComponents.Core | ||
{ | ||
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class, AllowMultiple = true)] | ||
[BaseTypeRequired(typeof(UIComponent))] | ||
public class AssetPathAttribute : Attribute | ||
{ | ||
public readonly string Path; | ||
|
||
public AssetPathAttribute(string path) | ||
{ | ||
Path = path; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,75 @@ | ||
using System; | ||
using UnityEditor; | ||
|
||
namespace UIComponents.Core | ||
{ | ||
public abstract class PathAttribute : Attribute | ||
{ | ||
public string Path { get; protected set; } | ||
|
||
public string RelativeTo { get; set; } | ||
|
||
public string GetAssetPath() | ||
public string GetAssetPathForComponent(UIComponent component) | ||
{ | ||
if (Path == null) | ||
Path = string.Empty; | ||
|
||
if (!string.IsNullOrEmpty(RelativeTo)) | ||
return string.Join("/", RelativeTo, Path); | ||
if (!ConfiguredPathIsComplete()) | ||
{ | ||
if (TryGetPathFromComponent(component, out var pathFromComponent)) | ||
Path = pathFromComponent; | ||
else if (TryGetPathFromAssembly(component, out var pathFromAssembly)) | ||
Path = pathFromAssembly; | ||
} | ||
|
||
return Path; | ||
} | ||
|
||
private bool ConfiguredPathIsComplete() | ||
{ | ||
return Path.StartsWith("Assets/") || | ||
Path.StartsWith("Packages/"); | ||
} | ||
|
||
private bool TryGetPathFromComponent(UIComponent component, out string path) | ||
{ | ||
path = ""; | ||
|
||
return TryGetValidAssetPathFromAttributes(component.AssetPathAttributes, out path); | ||
} | ||
|
||
private bool TryGetPathFromAssembly(UIComponent component, out string path) | ||
{ | ||
path = ""; | ||
|
||
var assembly = component.GetType().Assembly; | ||
|
||
var assetPathAttributes = | ||
assembly.GetCustomAttributes(typeof(AssetPathAttribute), false); | ||
|
||
return TryGetValidAssetPathFromAttributes((AssetPathAttribute[]) assetPathAttributes, out path); | ||
} | ||
|
||
private bool TryGetValidAssetPathFromAttributes( | ||
AssetPathAttribute[] attributes, | ||
out string path) | ||
{ | ||
path = ""; | ||
|
||
if (attributes.Length == 0) | ||
return false; | ||
|
||
foreach (var attribute in attributes) | ||
{ | ||
var filePath = string.Join("/", attribute.Path, Path); | ||
|
||
if (string.IsNullOrEmpty(AssetDatabase.AssetPathToGUID(filePath))) | ||
continue; | ||
|
||
path = filePath; | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters