Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Asset Reference Resolving by labels. Add including only ignored tags/labels. Update AssetRefResolver custom editor. #3

Merged
merged 3 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions Assets/Plugins/Bayat/Core/Editor/AssetReferenceResolverEditor.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using Bayat.Core.EditorWindows;

using Bayat.Core.EditorWindows;
using UnityEditor;

using UnityEngine;

namespace Bayat.Core
Expand All @@ -15,6 +13,9 @@ public class AssetReferenceResolverEditor : Editor
protected SerializedProperty mode;
protected SerializedProperty refreshDependenciesTimeoutInSeconds;
protected SerializedProperty ignoredTags;
protected SerializedProperty includeIgnoredTags;
protected SerializedProperty ignoredLabels;
protected SerializedProperty includeIgnoredLabels;
protected SerializedProperty ignoreStatic;
protected bool foldout = false;

Expand All @@ -24,6 +25,9 @@ private void OnEnable()
this.mode = serializedObject.FindProperty("mode");
this.refreshDependenciesTimeoutInSeconds = serializedObject.FindProperty("refreshDependenciesTimeoutInSeconds");
this.ignoredTags = serializedObject.FindProperty("ignoredTags");
this.includeIgnoredTags = serializedObject.FindProperty("includeIgnoredTags");
this.ignoredLabels = serializedObject.FindProperty("ignoredLabels");
this.includeIgnoredLabels = serializedObject.FindProperty("includeIgnoredLabels");
this.ignoreStatic = serializedObject.FindProperty("ignoreStatic");
}

Expand All @@ -40,6 +44,9 @@ public override void OnInspectorGUI()
EditorGUILayout.Space();
EditorGUILayout.PropertyField(this.mode);
EditorGUILayout.PropertyField(this.ignoredTags);
EditorGUILayout.PropertyField(this.includeIgnoredTags);
EditorGUILayout.PropertyField(this.ignoredLabels);
EditorGUILayout.PropertyField(this.includeIgnoredLabels);
EditorGUILayout.PropertyField(this.ignoreStatic);

EditorGUI.BeginDisabledGroup(this.assetReferenceResolver.Mode == ReferenceResolverMode.Manual);
Expand Down Expand Up @@ -78,4 +85,4 @@ public override void OnInspectorGUI()

}

}
}
45 changes: 40 additions & 5 deletions Assets/Plugins/Bayat/Core/Runtime/AssetReferenceResolver.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;

Expand All @@ -15,6 +15,7 @@
using Bayat.Core.Utilities;

using UnityObject = UnityEngine.Object;
using System.Threading;

namespace Bayat.Core
{
Expand Down Expand Up @@ -78,6 +79,19 @@ public static AssetReferenceResolver Current
[Tooltip("Ignore GameObjects that have any of these tags")]
[SerializeField]
protected string[] ignoredTags = new string[] { "EditorOnly" };

[Tooltip("Include GameObjects that have any of these tags, ignore the others.")]
[SerializeField]
protected bool includeIgnoredTags = false;

[Tooltip("Ignore Assets that have any of these labels (bottom right icon in the asset inspector)")]
[SerializeField]
protected string[] ignoredLabels = new string[0];

[Tooltip("Include Assets that have any of these labels, ignore the others.")]
[SerializeField]
protected bool includeIgnoredLabels = false;

[Tooltip("Whether to ignore static GameObjects")]
[SerializeField]
protected bool ignoreStatic = false;
Expand Down Expand Up @@ -630,6 +644,9 @@ public virtual bool IsValidUnityObject(UnityObject unityObject)
return false;
}
}

if (HasInvalidLabel(unityObject)) return false;

GameObject gameObject = null;
if (unityObject is GameObject)
{
Expand Down Expand Up @@ -657,19 +674,37 @@ public virtual bool IsValidUnityObject(UnityObject unityObject)
/// Checks whether the given GameObject has any invalid tags determined using <see cref="ignoredTags"/> field.
/// </summary>
/// <param name="gameObject">The GameObject to check</param>
/// <returns>Returns true if the GameObject has any invalid tag, otherwise false</returns>
/// <returns>Returns true if the GameObject has any invalid tag, otherwise false.
/// In case <see cref="includeIgnoredTags"/> is true, returns false when GameObject has "invalid" label and vise versa.</returns>
public virtual bool HasInvalidTag(GameObject gameObject)
{
for (int i = 0; i < this.ignoredTags.Length; i++)
{
if (gameObject.CompareTag(this.ignoredTags[i]))
{
return true;
return !includeIgnoredTags;
}
}
return false;
return includeIgnoredTags;
}


/// <summary>
/// Checks whether the given Asset has any invalid labels (they're located at the bottom of the asset's inspector view) inspector view),
/// determined using <see cref="ignoredLabels"/> field.
/// </summary>
/// <param name="unityObject">The Asset to check</param>
/// <returns>Returns true if the Asset has any invalid labels, otherwise false.
/// In case <see cref="includeIgnoredLabels"/> is true, returns false when Asset has "invalid" label and vise versa.</returns>
public bool HasInvalidLabel(UnityObject unityObject)
{
if (ignoredLabels.Length < 1) return false;

var objLabels = AssetDatabase.GetLabels(unityObject);
return objLabels.Any(label => ignoredLabels.Contains(label, StringComparer.InvariantCultureIgnoreCase)) ^ includeIgnoredLabels;
}


[InitializeOnLoadMethod]
public static void UpdateLocation()
{
Expand Down Expand Up @@ -1105,4 +1140,4 @@ public static void UpdateLocation()

}

}
}