Last active
November 23, 2021 05:08
-
-
Save wallstop/05fcef37b7750306a1813b95c7982408 to your computer and use it in GitHub Desktop.
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
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