SerializedPropertyUtil.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using UnityEditor;
  7. using UnityEngine;
  8. namespace TheraBytes.BetterUi.Editor
  9. {
  10. public static class SerializedPropertyUtil
  11. {
  12. public static void Copy(SerializedProperty source, SerializedProperty target)
  13. {
  14. //Debug.LogFormat("source: {0} - {1} \ntarget: {2} - {3}",
  15. // source.name, source.propertyType, target.name, target.propertyType);
  16. if (source.isArray)
  17. {
  18. target.arraySize = source.arraySize;
  19. target.serializedObject.ApplyModifiedPropertiesWithoutUndo();
  20. for (int i = 0; i < source.arraySize; i++)
  21. {
  22. Copy(source.GetArrayElementAtIndex(i), target.GetArrayElementAtIndex(i));
  23. }
  24. }
  25. else if (source.hasVisibleChildren)
  26. {
  27. var endSource = source.GetEndProperty(true).propertyPath;
  28. var endTarget = target.GetEndProperty(true).propertyPath;
  29. source.NextVisible(true);
  30. target.NextVisible(true);
  31. while (source.propertyPath != endSource && target.propertyPath != endTarget)
  32. {
  33. Copy(source, target);
  34. bool enterChildren = !(source.isArray);
  35. bool stop = (source.propertyPath == endSource) || (target.propertyPath == endTarget);
  36. try
  37. {
  38. stop = stop || !(source.NextVisible(enterChildren));
  39. stop = stop || !(target.NextVisible(enterChildren));
  40. }
  41. catch (Exception ex)
  42. {
  43. stop = true;
  44. Debug.LogErrorFormat("An Error occured: {0}", ex);
  45. }
  46. if (stop || source.propertyType != target.propertyType)
  47. break;
  48. }
  49. }
  50. else
  51. {
  52. object val = GetValue(source);
  53. SetValue(target, val);
  54. }
  55. }
  56. public static object GetValue(SerializedProperty prop)
  57. {
  58. switch (prop.propertyType)
  59. {
  60. case SerializedPropertyType.ObjectReference: return prop.objectReferenceValue;
  61. case SerializedPropertyType.Integer: return prop.intValue;
  62. case SerializedPropertyType.Boolean: return prop.boolValue;
  63. case SerializedPropertyType.Float: return prop.floatValue;
  64. case SerializedPropertyType.String: return prop.stringValue;
  65. case SerializedPropertyType.Color: return prop.colorValue;
  66. case SerializedPropertyType.Enum: return prop.enumValueIndex;
  67. case SerializedPropertyType.Vector2: return prop.vector2Value;
  68. case SerializedPropertyType.Vector3: return prop.vector3Value;
  69. case SerializedPropertyType.Vector4: return prop.vector4Value;
  70. case SerializedPropertyType.Rect: return prop.rectValue;
  71. case SerializedPropertyType.ArraySize: return prop.arraySize;
  72. case SerializedPropertyType.AnimationCurve: return prop.animationCurveValue;
  73. case SerializedPropertyType.Bounds: return prop.boundsValue;
  74. case SerializedPropertyType.Quaternion: return prop.quaternionValue;
  75. case SerializedPropertyType.Generic: return prop.objectReferenceValue;
  76. case SerializedPropertyType.LayerMask:
  77. case SerializedPropertyType.Gradient:
  78. case SerializedPropertyType.Character:
  79. default:
  80. throw new ArgumentException();
  81. }
  82. }
  83. public static void SetValue(SerializedProperty prop, object value)
  84. {
  85. switch (prop.propertyType)
  86. {
  87. case SerializedPropertyType.ObjectReference:
  88. prop.objectReferenceValue = (UnityEngine.Object)value;
  89. break;
  90. case SerializedPropertyType.Integer: prop.intValue = (int)value; break;
  91. case SerializedPropertyType.Boolean: prop.boolValue = (bool)value; break;
  92. case SerializedPropertyType.Float: prop.floatValue = (float)value; break;
  93. case SerializedPropertyType.String: prop.stringValue = (string)value; break;
  94. case SerializedPropertyType.Color: prop.colorValue = (Color)value; break;
  95. case SerializedPropertyType.Enum: prop.enumValueIndex = (int)value; break;
  96. case SerializedPropertyType.Vector2: prop.vector2Value = (Vector2)value; break;
  97. case SerializedPropertyType.Vector3: prop.vector3Value = (Vector3)value; break;
  98. case SerializedPropertyType.Vector4: prop.vector4Value = (Vector4)value; break;
  99. case SerializedPropertyType.Rect: prop.rectValue = (Rect)value; break;
  100. case SerializedPropertyType.ArraySize: prop.arraySize = (int)value; break;
  101. case SerializedPropertyType.Bounds: prop.boundsValue = (Bounds)value; break;
  102. case SerializedPropertyType.Quaternion: prop.quaternionValue = (Quaternion)value; break;
  103. case SerializedPropertyType.AnimationCurve:
  104. prop.animationCurveValue = (AnimationCurve)value;
  105. break;
  106. case SerializedPropertyType.Generic:
  107. case SerializedPropertyType.LayerMask:
  108. case SerializedPropertyType.Gradient:
  109. case SerializedPropertyType.Character:
  110. default:
  111. throw new ArgumentException();
  112. }
  113. }
  114. }
  115. }