SerializedPropertyExtensions.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using UnityEngine;
  2. using UnityEditor;
  3. namespace GitMerge
  4. {
  5. public static class SerializedPropertyExtensions
  6. {
  7. public static object GetValue(this SerializedProperty p)
  8. {
  9. switch (p.propertyType)
  10. {
  11. case SerializedPropertyType.AnimationCurve:
  12. return p.animationCurveValue;
  13. case SerializedPropertyType.ArraySize:
  14. return p.intValue;
  15. case SerializedPropertyType.Boolean:
  16. return p.boolValue;
  17. case SerializedPropertyType.Bounds:
  18. return p.boundsValue;
  19. case SerializedPropertyType.Character:
  20. return p.stringValue; //TODO: might be bullshit
  21. case SerializedPropertyType.Color:
  22. return p.colorValue;
  23. case SerializedPropertyType.Enum:
  24. return p.enumValueIndex;
  25. case SerializedPropertyType.Float:
  26. return p.floatValue;
  27. case SerializedPropertyType.Generic: //(arrays)
  28. if (p.isArray)
  29. {
  30. var arr = new object[p.arraySize];
  31. for (int i = 0; i < arr.Length; ++i)
  32. {
  33. arr[i] = p.GetArrayElementAtIndex(i).GetValue();
  34. }
  35. return arr;
  36. }
  37. else
  38. {
  39. return null;
  40. }
  41. case SerializedPropertyType.Gradient:
  42. return 0; //TODO: erm
  43. case SerializedPropertyType.Integer:
  44. return p.intValue;
  45. case SerializedPropertyType.LayerMask:
  46. return p.intValue;
  47. case SerializedPropertyType.ObjectReference:
  48. return p.objectReferenceValue;
  49. case SerializedPropertyType.Quaternion:
  50. return p.quaternionValue;
  51. case SerializedPropertyType.Rect:
  52. return p.rectValue;
  53. case SerializedPropertyType.String:
  54. return p.stringValue;
  55. case SerializedPropertyType.Vector2:
  56. return p.vector2Value;
  57. case SerializedPropertyType.Vector3:
  58. return p.vector3Value;
  59. case SerializedPropertyType.Vector4:
  60. return p.vector4Value;
  61. default:
  62. return 0;
  63. }
  64. }
  65. public static void SetValue(this SerializedProperty p, object value)
  66. {
  67. switch (p.propertyType)
  68. {
  69. case SerializedPropertyType.AnimationCurve:
  70. p.animationCurveValue = value as AnimationCurve;
  71. break;
  72. case SerializedPropertyType.ArraySize:
  73. //TODO: erm
  74. p.intValue = (int)value;
  75. break;
  76. case SerializedPropertyType.Boolean:
  77. p.boolValue = (bool)value;
  78. break;
  79. case SerializedPropertyType.Bounds:
  80. p.boundsValue = (Bounds)value;
  81. break;
  82. case SerializedPropertyType.Character:
  83. p.stringValue = (string)value; //TODO: might be bullshit
  84. break;
  85. case SerializedPropertyType.Color:
  86. p.colorValue = (Color)value;
  87. break;
  88. case SerializedPropertyType.Enum:
  89. p.enumValueIndex = (int)value;
  90. break;
  91. case SerializedPropertyType.Float:
  92. p.floatValue = (float)value;
  93. break;
  94. case SerializedPropertyType.Generic: //(arrays)
  95. if (p.isArray)
  96. {
  97. //var size = p.arraySize;
  98. //var resetPath = p.propertyPath;
  99. var values = (object[])value;
  100. /*
  101. for(int i = 0; i < p.arraySize; ++i)
  102. {
  103. Debug.Log(i + ": " + p.GetArrayElementAtIndex(i).GetValue());
  104. }
  105. */
  106. p.ClearArray();
  107. for (int i = 0; i < values.Length; ++i)
  108. {
  109. p.InsertArrayElementAtIndex(i);
  110. //Debug.Log(i + ": " + pv.GetArrayElementAtIndex(i).GetValue());
  111. p.GetArrayElementAtIndex(i).SetValue(values[i]);
  112. }
  113. //p.FindPropertyRelative(resetPath);
  114. }
  115. else
  116. {
  117. //p.stringValue = (string)value;
  118. }
  119. break;
  120. case SerializedPropertyType.Gradient:
  121. //TODO: erm
  122. break;
  123. case SerializedPropertyType.Integer:
  124. p.intValue = (int)value;
  125. break;
  126. case SerializedPropertyType.LayerMask:
  127. p.intValue = (int)value;
  128. break;
  129. case SerializedPropertyType.ObjectReference:
  130. p.objectReferenceValue = value as Object; //TODO: what about non-UnityEngine objects?
  131. break;
  132. case SerializedPropertyType.Quaternion:
  133. p.quaternionValue = (Quaternion)value;
  134. break;
  135. case SerializedPropertyType.Rect:
  136. p.rectValue = (Rect)value;
  137. break;
  138. case SerializedPropertyType.String:
  139. p.stringValue = (string)value;
  140. break;
  141. case SerializedPropertyType.Vector2:
  142. p.vector2Value = (Vector2)value;
  143. break;
  144. case SerializedPropertyType.Vector3:
  145. p.vector3Value = (Vector3)value;
  146. break;
  147. case SerializedPropertyType.Vector4:
  148. p.vector4Value = (Vector4)value;
  149. break;
  150. default:
  151. break;
  152. }
  153. }
  154. public static string GetPlainName(this SerializedProperty p)
  155. {
  156. var s = p.name;
  157. var i = s.IndexOf('_');
  158. if (i >= 0)
  159. {
  160. s = s.Substring(i + 1);
  161. }
  162. return s;
  163. }
  164. public static bool IsRealArray(this SerializedProperty p)
  165. {
  166. return p.propertyType == SerializedPropertyType.Generic && p.isArray;
  167. }
  168. }
  169. }