Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
SerializedPropertyExtension.cs
Go to the documentation of this file.
1 using System;
2 #if UNITY_EDITOR
3 using UnityEditor;
4 
5 namespace droid.Runtime.Utilities.ScriptableObjects.SerialisableDictionary {
8  public static class SerializedPropertyExtension {
9  public static int GetObjectCode(this SerializedProperty p) {
10  // Unique code per serialized object and property path
11  return p.propertyPath.GetHashCode() ^ p.serializedObject.GetHashCode();
12  }
13 
14  public static bool EqualBasics(SerializedProperty left, SerializedProperty right) {
15  if (left.propertyType != right.propertyType) {
16  return false;
17  }
18 
19  if (left.propertyType == SerializedPropertyType.Integer) {
20  if (left.type == right.type) {
21  if (left.type == "int") {
22  return left.intValue == right.intValue;
23  }
24 
25  return left.longValue == right.longValue;
26  }
27 
28  return false;
29  }
30 
31  if (left.propertyType == SerializedPropertyType.String) {
32  return left.stringValue == right.stringValue;
33  }
34 
35  if (left.propertyType == SerializedPropertyType.ObjectReference) {
36  return left.objectReferenceValue == right.objectReferenceValue;
37  }
38 
39  if (left.propertyType == SerializedPropertyType.Enum) {
40  return left.enumValueIndex == right.enumValueIndex;
41  }
42 
43  if (left.propertyType == SerializedPropertyType.Boolean) {
44  return left.boolValue == right.boolValue;
45  }
46 
47  if (left.propertyType == SerializedPropertyType.Float) {
48  if (left.type == right.type) {
49  if (left.type == "float") {
50  return Math.Abs(left.floatValue - right.floatValue) < double.Epsilon;
51  }
52 
53  return Math.Abs(left.doubleValue - right.doubleValue) < double.Epsilon;
54  }
55 
56  return false;
57  }
58 
59  if (left.propertyType == SerializedPropertyType.Color) {
60  return left.colorValue == right.colorValue;
61  }
62 
63  if (left.propertyType == SerializedPropertyType.LayerMask) {
64  return left.intValue == right.intValue;
65  }
66 
67  if (left.propertyType == SerializedPropertyType.Vector2) {
68  return left.vector2Value == right.vector2Value;
69  }
70 
71  if (left.propertyType == SerializedPropertyType.Vector3) {
72  return left.vector3Value == right.vector3Value;
73  }
74 
75  if (left.propertyType == SerializedPropertyType.Vector4) {
76  return left.vector4Value == right.vector4Value;
77  }
78 
79  if (left.propertyType == SerializedPropertyType.Rect) {
80  return left.rectValue == right.rectValue;
81  }
82 
83  if (left.propertyType == SerializedPropertyType.ArraySize) {
84  return left.arraySize == right.arraySize;
85  }
86 
87  if (left.propertyType == SerializedPropertyType.Character) {
88  return left.intValue == right.intValue;
89  }
90 
91  if (left.propertyType == SerializedPropertyType.AnimationCurve) {
92  return false;
93  }
94 
95  if (left.propertyType == SerializedPropertyType.Bounds) {
96  return left.boundsValue == right.boundsValue;
97  }
98 
99  if (left.propertyType == SerializedPropertyType.Gradient) {
100  return false;
101  }
102 
103  if (left.propertyType == SerializedPropertyType.Quaternion) {
104  return left.quaternionValue == right.quaternionValue;
105  }
106 
107  return false;
108  }
109 
110  public static void CopyBasics(SerializedProperty source, SerializedProperty target) {
111  if (source.propertyType != target.propertyType) {
112  return;
113  }
114 
115  if (source.propertyType == SerializedPropertyType.Integer) {
116  if (source.type == target.type) {
117  if (source.type == "int") {
118  target.intValue = source.intValue;
119  } else {
120  target.longValue = source.longValue;
121  }
122  }
123  } else if (source.propertyType == SerializedPropertyType.String) {
124  target.stringValue = source.stringValue;
125  } else if (source.propertyType == SerializedPropertyType.ObjectReference) {
126  target.objectReferenceValue = source.objectReferenceValue;
127  } else if (source.propertyType == SerializedPropertyType.Enum) {
128  target.enumValueIndex = source.enumValueIndex;
129  } else if (source.propertyType == SerializedPropertyType.Boolean) {
130  target.boolValue = source.boolValue;
131  } else if (source.propertyType == SerializedPropertyType.Float) {
132  if (source.type == target.type) {
133  if (source.type == "float") {
134  target.floatValue = source.floatValue;
135  } else {
136  target.doubleValue = source.doubleValue;
137  }
138  }
139  } else if (source.propertyType == SerializedPropertyType.Color) {
140  target.colorValue = source.colorValue;
141  } else if (source.propertyType == SerializedPropertyType.LayerMask) {
142  target.intValue = source.intValue;
143  } else if (source.propertyType == SerializedPropertyType.Vector2) {
144  target.vector2Value = source.vector2Value;
145  } else if (source.propertyType == SerializedPropertyType.Vector3) {
146  target.vector3Value = source.vector3Value;
147  } else if (source.propertyType == SerializedPropertyType.Vector4) {
148  target.vector4Value = source.vector4Value;
149  } else if (source.propertyType == SerializedPropertyType.Rect) {
150  target.rectValue = source.rectValue;
151  } else if (source.propertyType == SerializedPropertyType.ArraySize) {
152  target.arraySize = source.arraySize;
153  } else if (source.propertyType == SerializedPropertyType.Character) {
154  target.intValue = source.intValue;
155  } else if (source.propertyType == SerializedPropertyType.AnimationCurve) {
156  target.animationCurveValue = source.animationCurveValue;
157  } else if (source.propertyType == SerializedPropertyType.Bounds) {
158  target.boundsValue = source.boundsValue;
159  } else if (source.propertyType == SerializedPropertyType.Gradient) {
160  // TODO?
161  } else if (source.propertyType == SerializedPropertyType.Quaternion) {
162  target.quaternionValue = source.quaternionValue;
163  } else {
164  if (source.hasChildren && target.hasChildren) {
165  var source_iterator = source.Copy();
166  var target_iterator = target.Copy();
167  while (true) {
168  if (source_iterator.propertyType == SerializedPropertyType.Generic) {
169  if (!source_iterator.Next(true) || !target_iterator.Next(true)) {
170  break;
171  }
172  } else if (!source_iterator.Next(false) || !target_iterator.Next(false)) {
173  break;
174  }
175 
176  CopyBasics(source_iterator, target_iterator);
177  }
178  }
179  }
180  }
181  }
182 }
183 #endif