Skip to content

Instantly share code, notes, and snippets.

@wallstop
Last active November 23, 2021 05:08
Show Gist options
  • Save wallstop/05fcef37b7750306a1813b95c7982408 to your computer and use it in GitHub Desktop.
Save wallstop/05fcef37b7750306a1813b95c7982408 to your computer and use it in GitHub Desktop.
namespace Core.Attributes
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
[AttributeUsage(AttributeTargets.Field)]
public sealed class ValidateAssignmentAttribute : Attribute
{
}
public static class ValidateAssignmentExtensions
{
public static void ValidateAssignments(this UnityEngine.Object o)
{
#if UNITY_EDITOR
IEnumerable<FieldInfo> properties = o.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
.Where(prop => Attribute.IsDefined(prop, typeof(ValidateAssignmentAttribute)));
foreach (FieldInfo field in properties)
{
object fieldValue = field.GetValue(o);
bool logNotAssigned = fieldValue switch
{
IList list => list.Count <= 0,
ICollection collection => collection.Count <= 0,
UnityEngine.Object unityObject => !unityObject,
_ => fieldValue == null
};
if (logNotAssigned)
{
Debug.Log($"{field.Name} not found.");
}
}
#endif
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment