SerializedPropertyExtensions.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <author>
  3. // HiddenMonk
  4. // http://answers.unity3d.com/users/496850/hiddenmonk.html
  5. //
  6. // Johannes Deml
  7. // send@johannesdeml.com
  8. // </author>
  9. // --------------------------------------------------------------------------------------------------------------------
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using UnityEngine;
  14. using UnityEditor;
  15. using System.Reflection;
  16. using System.Text.RegularExpressions;
  17. namespace TheraBytes.BetterUi.Editor
  18. {
  19. /// <summary>
  20. /// Extension class for SerializedProperties
  21. /// See also: http://answers.unity3d.com/questions/627090/convert-serializedproperty-to-custom-class.html
  22. /// </summary>
  23. public static class SerializedPropertyExtensions
  24. {
  25. /// <summary>
  26. /// Get the object the serialized property holds by using reflection
  27. /// </summary>
  28. /// <typeparam name="T">The object type that the property contains</typeparam>
  29. /// <param name="property"></param>
  30. /// <returns>Returns the object type T if it is the type the property actually contains</returns>
  31. public static T GetValue<T>(this SerializedProperty property)
  32. {
  33. return GetNestedObject<T>(property.propertyPath, GetSerializedPropertyRoot(property));
  34. }
  35. /// <summary>
  36. /// Set the value of a field of the property with the type T
  37. /// </summary>
  38. /// <typeparam name="T">The type of the field that is set</typeparam>
  39. /// <param name="property">The serialized property that should be set</param>
  40. /// <param name="value">The new value for the specified property</param>
  41. /// <returns>Returns if the operation was successful or failed</returns>
  42. public static bool SetValue<T>(this SerializedProperty property, T value)
  43. {
  44. object obj = GetSerializedPropertyRoot(property);
  45. //Iterate to parent object of the value, necessary if it is a nested object
  46. string[] fieldStructure = property.propertyPath.Split('.');
  47. for (int i = 0; i < fieldStructure.Length - 1; i++)
  48. {
  49. obj = GetFieldOrPropertyValue<object>(fieldStructure[i], obj);
  50. }
  51. string fieldName = fieldStructure.Last();
  52. return SetFieldOrPropertyValue(fieldName, obj, value);
  53. }
  54. /// <summary>
  55. /// Get the container object of a serialized property (Component or ScriptableObject)
  56. /// </summary>
  57. /// <param name="property">The property that is part of the object</param>
  58. /// <returns>The root of the property</returns>
  59. public static UnityEngine.Object GetSerializedPropertyRoot(SerializedProperty property)
  60. {
  61. return property.serializedObject.targetObject;
  62. }
  63. /// <summary>
  64. /// Iterates through objects to handle objects that are nested in the root object
  65. /// </summary>
  66. /// <typeparam name="T">The type of the nested object</typeparam>
  67. /// <param name="path">Path to the object through other properties e.g. PlayerInformation.Health</param>
  68. /// <param name="obj">The root object from which this path leads to the property</param>
  69. /// <param name="includeAllBases">Include base classes and interfaces as well</param>
  70. /// <returns>Returns the nested object casted to the type T</returns>
  71. public static T GetNestedObject<T>(string path, object obj, bool includeAllBases = true)
  72. {
  73. // replace "Array.data[x]" just with "x"
  74. Regex regex = new Regex(@"([a-zA-Z\.]+.)?Array.data\[(\d+)\]");
  75. path = regex.Replace(path, "$1$2");
  76. foreach (string part in path.Split('.'))
  77. {
  78. obj = GetFieldOrPropertyValue<object>(part, obj, includeAllBases);
  79. }
  80. return (T)obj;
  81. }
  82. public static T GetFieldOrPropertyValue<T>(string fieldName, object obj, bool includeAllBases = true, BindingFlags bindings = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
  83. {
  84. int idx;
  85. if (int.TryParse(fieldName, out idx))
  86. {
  87. var method = obj.GetType().GetMethod("get_Item", bindings);
  88. if (method != null)
  89. return (T)method.Invoke(obj, new object[] { idx });
  90. }
  91. FieldInfo field = obj.GetType().GetField(fieldName, bindings);
  92. if (field != null) return (T)field.GetValue(obj);
  93. PropertyInfo property = obj.GetType().GetProperty(fieldName, bindings);
  94. if (property != null) return (T)property.GetValue(obj, null);
  95. if (includeAllBases)
  96. {
  97. foreach (Type type in GetBaseClassesAndInterfaces(obj.GetType()))
  98. {
  99. field = type.GetField(fieldName, bindings);
  100. if (field != null) return (T)field.GetValue(obj);
  101. property = type.GetProperty(fieldName, bindings);
  102. if (property != null) return (T)property.GetValue(obj, null);
  103. }
  104. }
  105. return default(T);
  106. }
  107. public static bool SetFieldOrPropertyValue(string fieldName, object obj, object value, bool includeAllBases = false, BindingFlags bindings = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)
  108. {
  109. FieldInfo field = obj.GetType().GetField(fieldName, bindings);
  110. if (field != null)
  111. {
  112. field.SetValue(obj, value);
  113. return true;
  114. }
  115. PropertyInfo property = obj.GetType().GetProperty(fieldName, bindings);
  116. if (property != null)
  117. {
  118. property.SetValue(obj, value, null);
  119. return true;
  120. }
  121. if (includeAllBases)
  122. {
  123. foreach (Type type in GetBaseClassesAndInterfaces(obj.GetType()))
  124. {
  125. field = type.GetField(fieldName, bindings);
  126. if (field != null)
  127. {
  128. field.SetValue(obj, value);
  129. return true;
  130. }
  131. property = type.GetProperty(fieldName, bindings);
  132. if (property != null)
  133. {
  134. property.SetValue(obj, value, null);
  135. return true;
  136. }
  137. }
  138. }
  139. return false;
  140. }
  141. public static IEnumerable<Type> GetBaseClassesAndInterfaces(this Type type, bool includeSelf = false)
  142. {
  143. List<Type> allTypes = new List<Type>();
  144. if (includeSelf) allTypes.Add(type);
  145. if (type.BaseType == typeof(object))
  146. {
  147. allTypes.AddRange(type.GetInterfaces());
  148. }
  149. else {
  150. allTypes.AddRange(
  151. Enumerable
  152. .Repeat(type.BaseType, 1)
  153. .Concat(type.GetInterfaces())
  154. .Concat(type.BaseType.GetBaseClassesAndInterfaces())
  155. .Distinct());
  156. }
  157. return allTypes;
  158. }
  159. }
  160. }