SerializedPropertyExtensions.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using System.Linq;
  5. using UnityEngine;
  6. using UnityEditor;
  7. namespace HQFPSWeapons
  8. {
  9. public static class SerializedPropertyExtensions
  10. {
  11. public static void SetValue<T>(this SerializedProperty property, T value)
  12. {
  13. var fullPath = property.propertyPath.Replace(".Array.data[", "[");
  14. object lastArray = null;
  15. int lastIndex = -1;
  16. object obj = property.serializedObject.targetObject;
  17. object lastObject = null;
  18. var children = fullPath.Split('.');
  19. foreach(var child in children)
  20. {
  21. lastObject = obj;
  22. if(child.Contains("["))
  23. {
  24. var arrayName = child.Substring(0, child.IndexOf("["));
  25. var index = System.Convert.ToInt32(child.Substring(child.IndexOf("[")).Replace("[", "").Replace("]", ""));
  26. lastArray = GetValue_Imp(obj, arrayName);
  27. lastIndex = index;
  28. obj = GetValue_Imp(obj, arrayName, index);
  29. }
  30. else
  31. obj = GetValue_Imp(obj, child);
  32. }
  33. var propertyToSet = children.Last();
  34. if(propertyToSet.Contains("["))
  35. ((Array)lastArray).SetValue(value, lastIndex);
  36. else
  37. lastObject.GetType().GetField(propertyToSet, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.FlattenHierarchy | BindingFlags.Instance).SetValue(lastObject, value);
  38. }
  39. public static T GetValue<T>(this SerializedProperty property)
  40. {
  41. var path = property.propertyPath.Replace(".Array.data[", "[");
  42. object root = property.serializedObject.targetObject;
  43. var children = path.Split('.');
  44. foreach (var child in children)
  45. {
  46. if(child.Contains("["))
  47. {
  48. var arrayName = child.Substring(0, child.IndexOf("["));
  49. var index = System.Convert.ToInt32(child.Substring(child.IndexOf("[")).Replace("[", "").Replace("]", ""));
  50. root = GetValue_Imp(root, arrayName, index);
  51. }
  52. else
  53. root = GetValue_Imp(root, child);
  54. }
  55. return (T)root;
  56. }
  57. private static object GetValue_Imp(object source, string name)
  58. {
  59. if (source == null)
  60. return null;
  61. var type = source.GetType();
  62. while (type != null)
  63. {
  64. var f = type.GetField(name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
  65. if (f != null)
  66. return f.GetValue(source);
  67. var p = type.GetProperty(name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
  68. if (p != null)
  69. return p.GetValue(source, null);
  70. type = type.BaseType;
  71. }
  72. return null;
  73. }
  74. private static object GetValue_Imp(object source, string name, int index)
  75. {
  76. var enumerable = GetValue_Imp(source, name) as System.Collections.IEnumerable;
  77. if (enumerable == null)
  78. return null;
  79. var enm = enumerable.GetEnumerator();
  80. //while (index-- >= 0)
  81. // enm.MoveNext();
  82. //return enm.Current;
  83. for (int i = 0; i <= index; i++)
  84. {
  85. if (!enm.MoveNext())
  86. return null;
  87. }
  88. return enm.Current;
  89. }
  90. public static string GetParentPath(this SerializedProperty property)
  91. {
  92. bool isAtDepth0 = true;
  93. string path = property.propertyPath;
  94. for(int i = path.Length - 1;i >= 0; i --)
  95. {
  96. if(path[i] == '.')
  97. {
  98. path = path.Remove(i);
  99. isAtDepth0 = false;
  100. break;
  101. }
  102. }
  103. return isAtDepth0 ? string.Empty : path;
  104. }
  105. public static IEnumerable<SerializedProperty> GetChildren(this SerializedProperty property)
  106. {
  107. property = property.Copy();
  108. var nextElement = property.Copy();
  109. bool hasNextElement = nextElement.NextVisible(false);
  110. if(!hasNextElement)
  111. nextElement = null;
  112. property.NextVisible(true);
  113. while(true)
  114. {
  115. if(SerializedProperty.EqualContents(property, nextElement))
  116. yield break;
  117. yield return property;
  118. bool hasNext = property.NextVisible(false);
  119. if(!hasNext)
  120. break;
  121. }
  122. }
  123. }
  124. }