RdResetAttribute.cs 948 B

123456789101112131415161718192021222324252627282930313233
  1. using System;
  2. using System.Reflection;
  3. using UnityEngine;
  4. namespace Ragdoll
  5. {
  6. [AttributeUsage(AttributeTargets.Field)]
  7. public class RdResetAttribute : Attribute
  8. {
  9. public string DefaultValue { get; set; }
  10. public RdResetAttribute(string defaultValue)
  11. {
  12. DefaultValue = defaultValue;
  13. }
  14. }
  15. public static class RdResetTool
  16. {
  17. public static void ResetAttribute(object obj)
  18. {
  19. var type = obj.GetType();
  20. foreach (var member in type.GetMembers())
  21. {
  22. if (Attribute.GetCustomAttribute(member, typeof(RdResetAttribute)) is not
  23. RdResetAttribute
  24. attribute || member is not FieldInfo field) continue;
  25. Debug.Log("重置 " + field.Name);
  26. field.SetValue(obj, Convert.ChangeType(attribute.DefaultValue, field.FieldType));
  27. }
  28. }
  29. }
  30. }