Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
7.1 KiB

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