1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
| namespace Core.Editor { public static class TempGUIHelper { public const int DRAW_DEPTH_LIMIT = 2;
public static void DrawTempShowInInspectorField(UnityObject target, FieldInfo field, bool isReadonly = false) { object value = field.GetValue(target); string niceVariableName = ObjectNames.NicifyVariableName(field.Name); if (field.IsStatic) { niceVariableName = $"{niceVariableName}(static) "; } if (null == value) { return; } if (TryDrawField(value, niceVariableName, field.FieldType, 0, out object nextValueObject, out bool setNextValue, isReadonly)) { if (setNextValue && !isReadonly) { field.SetValue(target, nextValueObject); } } }
public static bool TryDrawField(object value, string lable, Type expectedType, int depth, out object nextValue, out bool setNextValue, bool readonlyField = false) { nextValue = null; setNextValue = false; string showLableName = lable; if (0 < depth) { showLableName = lable.PadLeft(lable.Length + depth * 4); } if (depth >= DRAW_DEPTH_LIMIT) { EditorGUILayout.BeginVertical(); EditorGUILayout.LabelField($"{showLableName}(OutOfDrawDepthLimit):"); string tempContent = value.ToString(); tempContent = tempContent.PadLeft(tempContent.Length + depth * 4); EditorGUILayout.LabelField(tempContent); EditorGUILayout.EndVertical(); return true; } using (new EditorGUI.DisabledScope(disabled: readonlyField)) { bool isDrawn = true; Type valueType = Equals(value, null) ? expectedType : value.GetType(); if (null == valueType) { return false; } if (valueType == typeof(bool)) { bool prevBool = (bool)value; bool nextBool = EditorGUILayout.Toggle(showLableName, prevBool); if (setNextValue = prevBool != nextBool) { nextValue = nextBool; } } else if (valueType == typeof(short)) { short prevShort = (short)value; short nextShort = (short)Mathf.Clamp(EditorGUILayout.IntField(showLableName, (int)prevShort), short.MinValue, short.MaxValue); if (setNextValue = prevShort != nextShort) { nextValue = nextShort; } } else if (valueType == typeof(ushort)) { ushort prevUshort = (ushort)value; ushort nextUshort = (ushort)Mathf.Clamp(EditorGUILayout.IntField(showLableName, (ushort)value), ushort.MinValue, ushort.MaxValue); if (setNextValue = prevUshort != nextUshort) { nextValue = nextUshort; } } else if (valueType == typeof(int)) { int prevInt = (int)value; int nextInt = EditorGUILayout.IntField(showLableName, prevInt); if (setNextValue = prevInt != nextInt) { nextValue = nextInt; } } else if (valueType == typeof(uint)) { uint prevUint = (uint)value; uint nextUint = (uint)Mathf.Clamp(EditorGUILayout.LongField(showLableName, prevUint), uint.MinValue, uint.MaxValue); if (setNextValue = prevUint != nextUint) { nextValue = nextUint; } } else if (valueType == typeof(long)) { long prevLong = (long)value; long nextLong = EditorGUILayout.LongField(showLableName, prevLong); if (setNextValue = prevLong != nextLong) { nextValue = nextLong; } } else if (valueType == typeof(ulong)) { ulong prevUlong = (ulong)value; string nextUlongStr = EditorGUILayout.TextField(showLableName, prevUlong.ToString()); nextUlongStr = Regex.Replace(nextUlongStr, @"[^a-zA-Z0-9 ]", ""); if (ulong.TryParse(nextUlongStr, out ulong nextUlong) && (setNextValue = prevUlong != nextUlong)) { nextValue = nextUlongStr; } } else if (valueType == typeof(float)) { float prevFloat = (float)value; float nextFloat = EditorGUILayout.FloatField(showLableName, (float)value); if (setNextValue = !Mathf.Approximately(prevFloat, nextFloat)) { nextValue = nextFloat; } } else if (valueType == typeof(double)) { double prevDouble = (double)value; double nextDouble = EditorGUILayout.DoubleField(showLableName, prevDouble); if (setNextValue = prevDouble != nextDouble) { nextValue = nextDouble; } } else if (valueType == typeof(string)) { string prevString = (string)value; string nextString = EditorGUILayout.TextField(showLableName, prevString); if (setNextValue = prevString != nextString) { nextValue = nextString; } } else if (valueType == typeof(Vector2)) { Vector2 prevVector2 = (Vector2)value; Vector2 nextVector2 = EditorGUILayout.Vector2Field(showLableName, prevVector2); if (setNextValue = prevVector2 != nextVector2) { nextValue = prevVector2; } } else if (valueType == typeof(Vector3)) { Vector3 prevVector3 = (Vector3)value; Vector3 nextVector3 = EditorGUILayout.Vector3Field(showLableName, prevVector3); if (setNextValue = prevVector3 != nextVector3) { nextValue = nextVector3; } } else if (valueType == typeof(Vector4)) { Vector4 prevVector4 = (Vector4)value; Vector4 nextVector4 = EditorGUILayout.Vector4Field(showLableName, prevVector4); if (setNextValue = prevVector4 != nextVector4) { nextValue = nextVector4; } } else if (valueType == typeof(Vector2Int)) { Vector2Int prevV2int = (Vector2Int)value; Vector2Int nextV2int = EditorGUILayout.Vector2IntField(showLableName, prevV2int); if (setNextValue = prevV2int != nextV2int) { nextValue = nextV2int; } } else if (valueType == typeof(Vector3Int)) { Vector3Int prevV3int = (Vector3Int)value; Vector3Int nextV3int = EditorGUILayout.Vector3IntField(showLableName, prevV3int); if (setNextValue = prevV3int != nextV3int) { nextValue = nextV3int; } } else if (valueType == typeof(Color)) { Color prevColor = (Color)value; Color nextColor = EditorGUILayout.ColorField(showLableName, prevColor); if (setNextValue = prevColor != nextColor) { nextValue = nextColor; } } else if (valueType == typeof(Bounds)) { Bounds prevBounds = (Bounds)value; Bounds nextBounds = EditorGUILayout.BoundsField(showLableName, prevBounds); if (setNextValue = prevBounds != nextBounds) { nextValue = nextBounds; } } else if (valueType == typeof(Rect)) { Rect prevRect = (Rect)value; Rect nextRect = EditorGUILayout.RectField(showLableName, prevRect); if (setNextValue = prevRect != nextRect) { nextValue = nextRect; } } else if (valueType == typeof(RectInt)) { RectInt prevRectInt = (RectInt)value; RectInt nextRectInt = EditorGUILayout.RectIntField(showLableName, prevRectInt); if (setNextValue = !prevRectInt.Equals(nextRectInt)) { nextValue = nextRectInt; } } else if (typeof(UnityObject).IsAssignableFrom(valueType)) { UnityObject prevUnityObj = (UnityObject)value; UnityObject nextUnityObj = EditorGUILayout.ObjectField(showLableName, prevUnityObj, valueType, true); if (setNextValue = prevUnityObj != nextUnityObj) { nextValue = nextUnityObj; } } else if (valueType.BaseType == typeof(Enum)) { Enum prevEnum = (Enum)value; Enum nextEnum = EditorGUILayout.EnumPopup(showLableName, prevEnum); if (setNextValue = prevEnum != nextEnum) { nextValue = nextEnum; } } else if (valueType.BaseType == typeof(TypeInfo)) { bool tempPrev = GUI.enabled; GUI.enabled = tempPrev; EditorGUILayout.TextField(showLableName, value.ToString()); GUI.enabled = tempPrev; } else { bool isSerializable = null != valueType.GetCustomAttribute<SerializableAttribute>(); if (isSerializable) { object structValueObject = value; EditorGUILayout.LabelField(showLableName); FieldInfo[] members = valueType.GetFields(); for (int i = 0, length = members.Length; i < length; i++) { FieldInfo memberInfo = members[i]; if (MemberTypes.Field == memberInfo.MemberType && !memberInfo.IsStatic) { Type fieldType = memberInfo.FieldType; object prevValueObj = memberInfo.GetValue(value); if (TryDrawField(prevValueObj, memberInfo.Name, fieldType, depth + 1, out object nextValueObj, out bool setNext, readonlyField)) { if (setNext) { setNextValue = true; memberInfo.SetValue(structValueObject, nextValueObj); } } isDrawn = true; } } nextValue = structValueObject; } else { EditorGUILayout.BeginVertical(); EditorGUILayout.LabelField($"{showLableName}(NonSupprtType/NonSerializable):"); string valueStr = value.ToString(); valueStr = valueStr.PadLeft(valueStr.Length + (depth + 1) * 4); EditorGUILayout.LabelField(valueStr); EditorGUILayout.EndVertical(); isDrawn = true; } } return isDrawn; } }
public static IEnumerable<FieldInfo> GetAllFields(object target, Func<FieldInfo, bool> predicate) { if (target == null) { Debug.LogError("The target object is null. Check for missing scripts."); yield break; }
List<Type> types = GetSelfAndBaseTypes(target);
for (int i = types.Count - 1; i >= 0; i--) { IEnumerable<FieldInfo> fieldInfos = types[i] .GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.DeclaredOnly) .Where(predicate);
foreach (var fieldInfo in fieldInfos) { yield return fieldInfo; } } }
private static List<Type> GetSelfAndBaseTypes(object target) { List<Type> types = new List<Type>() { target.GetType() };
while (types.Last().BaseType != null) { types.Add(types.Last().BaseType); }
return types; } } }
|