diff --git a/Editor/Extensions/SerializedPropertyExtension.cs b/Editor/Extensions/SerializedPropertyExtension.cs index c0651a0..8049573 100644 --- a/Editor/Extensions/SerializedPropertyExtension.cs +++ b/Editor/Extensions/SerializedPropertyExtension.cs @@ -1,4 +1,5 @@ using System; +using System.Globalization; using System.Reflection; using System.Runtime.CompilerServices; using Module.Inspector.Editor.Utilities; @@ -23,10 +24,24 @@ namespace Module.Inspector.Editor string propertyPath = GetRootPath(sp.propertyPath); if (string.IsNullOrEmpty(propertyPath)) + { propertyPath = fieldName; + } else + { + if (propertyPath.EndsWith(".Array")) + { + propertyPath = propertyPath.Substring(0, propertyPath.Length - 6); + SerializedProperty spArray = sp.serializedObject.FindProperty(propertyPath); + int index = spArray.IndexOfProperty(sp); + + if (index != -1) + propertyPath += ".Array.data[" + index + "]"; + } + propertyPath += "." + fieldName; - + } + return sp.serializedObject.FindProperty(propertyPath); } @@ -97,7 +112,7 @@ namespace Module.Inspector.Editor return ECompareType.Unknown; } - public static ECompareType IsSmallerOrEqualToSiblingValue(this SerializedProperty sp, string siblingFieldName) + public static ECompareType IsSmallerOrEqualToSiblingValue(this SerializedProperty sp, string siblingFieldName) { if (string.IsNullOrEmpty(siblingFieldName)) return ECompareType.Unknown; @@ -301,16 +316,16 @@ namespace Module.Inspector.Editor sp.quaternionValue = q; return true; case SerializedPropertyType.Vector2Int: - if (!(value is Vector2Int v2i)) + if (!(value is Vector2Int v2Int)) return false; - sp.vector2IntValue = v2i; + sp.vector2IntValue = v2Int; return true; case SerializedPropertyType.Vector3Int: - if (!(value is Vector3Int v3i)) + if (!(value is Vector3Int v3Int)) return false; - sp.vector3IntValue = v3i; + sp.vector3IntValue = v3Int; return true; case SerializedPropertyType.RectInt: if (!(value is RectInt ri)) @@ -358,10 +373,10 @@ namespace Module.Inspector.Editor case SerializedPropertyType.String: return Convert.ToString(value).Equals(sp.stringValue); case SerializedPropertyType.ObjectReference: - if (!(value is Object)) + if (!(value is Object obj)) return false; - return sp.objectReferenceValue == (Object)value; + return sp.objectReferenceValue == obj; } } catch (Exception e) @@ -426,5 +441,79 @@ namespace Module.Inspector.Editor sp.objectReferenceValue = array[i]; } } + + public static string GetValueAsString(this SerializedProperty property) + { + switch (property.propertyType) + { + case SerializedPropertyType.Generic: + break; + case SerializedPropertyType.Integer: + return property.intValue.ToString(); + case SerializedPropertyType.Boolean: + return property.boolValue.ToString(); + case SerializedPropertyType.Float: + return property.floatValue.ToString(CultureInfo.InvariantCulture); + case SerializedPropertyType.String: + return property.stringValue; + case SerializedPropertyType.Color: + return property.colorValue.ToString(); + case SerializedPropertyType.ObjectReference: + return property.objectReferenceValue != null ? property.objectReferenceValue.ToString() : "null"; + case SerializedPropertyType.LayerMask: + // TODO: Return as (Layer0 | Layer1) instead for all set bits + return property.intValue.ToString(); + case SerializedPropertyType.Enum: + return property.enumNames[property.enumValueIndex]; + case SerializedPropertyType.Vector2: + return property.vector2Value.ToString(); + case SerializedPropertyType.Vector3: + return property.vector3Value.ToString(); + case SerializedPropertyType.Vector4: + return property.vector4Value.ToString(); + case SerializedPropertyType.Rect: + return property.rectValue.ToString(); + case SerializedPropertyType.ArraySize: + return property.arraySize.ToString(); + case SerializedPropertyType.Character: + return ((char)property.intValue).ToString(); + case SerializedPropertyType.Bounds: + return property.boundsValue.ToString(); + case SerializedPropertyType.Quaternion: + return property.quaternionValue.ToString(); + case SerializedPropertyType.ExposedReference: + return property.exposedReferenceValue != null ? property.exposedReferenceValue.ToString() : "null"; + case SerializedPropertyType.FixedBufferSize: + return property.fixedBufferSize.ToString(); + case SerializedPropertyType.Vector2Int: + return property.vector2IntValue.ToString(); + case SerializedPropertyType.Vector3Int: + return property.vector3IntValue.ToString(); + case SerializedPropertyType.RectInt: + return property.rectIntValue.ToString(); + case SerializedPropertyType.BoundsInt: + return property.boundsIntValue.ToString(); + case SerializedPropertyType.ManagedReference: + return property.objectReferenceValue != null ? property.objectReferenceValue.ToString() : "null"; + } + + return "Unable to draw value as string"; + } + + public static int IndexOfProperty(this SerializedProperty sp, SerializedProperty element) + { + if (!sp.isArray) + return -1; + + for (var i = 0; i < sp.arraySize; i++) + { + SerializedProperty e = sp.GetArrayElementAtIndex(i); + + if (e.propertyPath.Equals(element.propertyPath)) + return i; + } + + return -1; + } } } \ No newline at end of file