diff --git a/Editor/Extensions/SerializedPropertyExtensions.cs b/Editor/Extensions/SerializedPropertyExtensions.cs index 731a7d3..fbc75c8 100644 --- a/Editor/Extensions/SerializedPropertyExtensions.cs +++ b/Editor/Extensions/SerializedPropertyExtensions.cs @@ -24,13 +24,11 @@ public static T GetValue(this SerializedProperty property) where T : class object obj = property.serializedObject.targetObject; string path = property.propertyPath.Replace(".Array.data", ""); string[] fieldStructure = path.Split('.'); - Regex rgx = new Regex(@"\[\d+\]"); for (int i = 0; i < fieldStructure.Length; i++) { - if (fieldStructure[i].Contains("[")) + if (TryParseFieldStructure(fieldStructure[i], out string fieldName, out int index)) { - int index = System.Convert.ToInt32(new string(fieldStructure[i].Where(char.IsDigit).ToArray())); - obj = GetFieldValueWithIndex(rgx.Replace(fieldStructure[i], ""), obj, index); + obj = GetFieldValueWithIndex(fieldName, obj, index); } else { @@ -41,6 +39,22 @@ public static T GetValue(this SerializedProperty property) where T : class return (T)obj; } + private static bool TryParseFieldStructure(string input, out string fieldName, out int index) + { + int endIndex = input.LastIndexOf(']'); + if (endIndex == -1) + { + fieldName = null; + index = -1; + return false; + } + + int startIndex = input.LastIndexOf('[', endIndex - 1); + index = int.Parse(input.Substring(startIndex + 1, endIndex - startIndex - 1)); + fieldName = input.Substring(0, startIndex) + input.Substring(endIndex + 1); + return true; + } + public static bool SetValue(this SerializedProperty property, T value) where T : class { object obj = property.serializedObject.targetObject;