Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/Serialized Property Regex removal #40

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions Editor/Extensions/SerializedPropertyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@ public static T GetValue<T>(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
{
Expand All @@ -41,6 +39,22 @@ public static T GetValue<T>(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<T>(this SerializedProperty property, T value) where T : class
{
object obj = property.serializedObject.targetObject;
Expand Down