diff --git a/Source/Editor/CustomEditors/Dedicated/UIControlEditor.cs b/Source/Editor/CustomEditors/Dedicated/UIControlEditor.cs index 0fcf9d78a..87c3a8877 100644 --- a/Source/Editor/CustomEditors/Dedicated/UIControlEditor.cs +++ b/Source/Editor/CustomEditors/Dedicated/UIControlEditor.cs @@ -405,7 +405,13 @@ namespace FlaxEditor.CustomEditors.Dedicated for (int i = 0; i < uiControls.Count; i++) { var uiControl = (UIControl)uiControls[i]; + string previousName = uiControl.Control?.GetType()?.Name ?? typeof(UIControl).Name; uiControl.Control = (Control)controlType.CreateInstance(); + if (uiControl.Name.StartsWith(previousName)) + { + string newName = controlType.Name + uiControl.Name.Substring(previousName.Length); + uiControl.Name = StringUtils.IncrementNameNumber(newName, x => uiControl.Parent.GetChild(x) == null); + } } } } @@ -414,7 +420,13 @@ namespace FlaxEditor.CustomEditors.Dedicated for (int i = 0; i < uiControls.Count; i++) { var uiControl = (UIControl)uiControls[i]; + string previousName = uiControl.Control?.GetType()?.Name ?? typeof(UIControl).Name; uiControl.Control = (Control)controlType.CreateInstance(); + if (uiControl.Name.StartsWith(previousName)) + { + string newName = controlType.Name + uiControl.Name.Substring(previousName.Length); + uiControl.Name = StringUtils.IncrementNameNumber(newName, x => uiControl.Parent.GetChild(x) == null); + } } } diff --git a/Source/Engine/Utilities/StringUtils.cs b/Source/Engine/Utilities/StringUtils.cs index 057dd9b86..4dd28e705 100644 --- a/Source/Engine/Utilities/StringUtils.cs +++ b/Source/Engine/Utilities/StringUtils.cs @@ -244,8 +244,8 @@ namespace FlaxEngine return new string(charArray); } - private static readonly Regex IncNameRegex1 = new Regex("^(\\d+)"); - private static readonly Regex IncNameRegex2 = new Regex("^\\)(\\d+)\\("); + private static readonly Regex IncNameRegex1 = new Regex("(\\d+)$"); + private static readonly Regex IncNameRegex2 = new Regex("\\((\\d+)\\)$"); /// /// Tries to parse number in the name brackets at the end of the value and then increment it to create a new name. @@ -264,14 +264,13 @@ namespace FlaxEngine int index; int MaxChecks = 10000; string result; - string reversed = name.Reverse(); // Find '' case - var match = IncNameRegex1.Match(reversed); + var match = IncNameRegex1.Match(name); if (match.Success && match.Groups.Count == 2) { // Get result - string num = match.Groups[0].Value.Reverse(); + string num = match.Groups[0].Value; // Parse value if (int.TryParse(num, out index)) @@ -294,12 +293,12 @@ namespace FlaxEngine } // Find ' ()' case - match = IncNameRegex2.Match(reversed); + match = IncNameRegex2.Match(name); if (match.Success && match.Groups.Count == 2) { // Get result string num = match.Groups[0].Value; - num = num.Substring(1, num.Length - 2).Reverse(); + num = num.Substring(1, num.Length - 2); // Parse value if (int.TryParse(num, out index))