diff --git a/Assets/Fungus/FungusScript/Editor/FungusScriptWindow.cs b/Assets/Fungus/FungusScript/Editor/FungusScriptWindow.cs index 29c3b3f1..720f2983 100755 --- a/Assets/Fungus/FungusScript/Editor/FungusScriptWindow.cs +++ b/Assets/Fungus/FungusScript/Editor/FungusScriptWindow.cs @@ -630,7 +630,7 @@ namespace Fungus Sequence oldSequence = sequence; Sequence newSequence = FungusScriptWindow.CreateSequence(fungusScript, newPosition); - newSequence.sequenceName = oldSequence.sequenceName + " (Copy)"; + newSequence.sequenceName = fungusScript.GetUniqueSequenceKey(oldSequence.sequenceName + " (Copy)"); Undo.RecordObject(newSequence, "Duplicate Sequence"); diff --git a/Assets/Fungus/FungusScript/Editor/SequenceEditor.cs b/Assets/Fungus/FungusScript/Editor/SequenceEditor.cs index 10c5543a..969f483a 100644 --- a/Assets/Fungus/FungusScript/Editor/SequenceEditor.cs +++ b/Assets/Fungus/FungusScript/Editor/SequenceEditor.cs @@ -47,6 +47,13 @@ namespace Fungus Rect sequenceNameRect = new Rect(45, 21, 180, 16); EditorGUI.PropertyField(sequenceNameRect, sequenceNameProperty, new GUIContent("")); + // Ensure sequence name is unique for this Fungus Script + string uniqueName = fungusScript.GetUniqueSequenceKey(sequenceNameProperty.stringValue, sequence); + if (uniqueName != sequence.sequenceName) + { + sequenceNameProperty.stringValue = uniqueName; + } + SerializedProperty commandListProperty = serializedObject.FindProperty("commandList"); ReorderableListGUI.Title("Commands"); diff --git a/Assets/Fungus/FungusScript/Scripts/FungusScript.cs b/Assets/Fungus/FungusScript/Scripts/FungusScript.cs index faffc429..dbbfaee9 100644 --- a/Assets/Fungus/FungusScript/Scripts/FungusScript.cs +++ b/Assets/Fungus/FungusScript/Scripts/FungusScript.cs @@ -98,6 +98,7 @@ namespace Fungus Sequence s = CreateSequenceComponent(gameObject); s.nodeRect.x = position.x; s.nodeRect.y = position.y; + s.sequenceName = GetUniqueSequenceKey(s.sequenceName, s); return s; } @@ -220,6 +221,49 @@ namespace Fungus } } + /** + * Returns a new Sequence key that is guaranteed not to clash with any existing Sequence in the Fungus Script. + */ + public virtual string GetUniqueSequenceKey(string originalKey, Sequence ignoreSequence = null) + { + int suffix = 0; + string baseKey = originalKey.Trim(); + + // No empty keys allowed + if (baseKey.Length == 0) + { + baseKey = "Sequence"; + } + + Sequence[] sequences = GetComponentsInChildren(); + + string key = baseKey; + while (true) + { + bool collision = false; + foreach(Sequence sequence in sequences) + { + if (sequence == ignoreSequence || + sequence.sequenceName == null) + { + continue; + } + + if (sequence.sequenceName.Equals(key, StringComparison.CurrentCultureIgnoreCase)) + { + collision = true; + suffix++; + key = baseKey + suffix; + } + } + + if (!collision) + { + return key; + } + } + } + /** * Gets the value of a boolean variable. * Returns false if the variable key does not exist. diff --git a/Assets/Fungus/FungusScript/Scripts/Sequence.cs b/Assets/Fungus/FungusScript/Scripts/Sequence.cs index 2314addb..df7da868 100644 --- a/Assets/Fungus/FungusScript/Scripts/Sequence.cs +++ b/Assets/Fungus/FungusScript/Scripts/Sequence.cs @@ -9,7 +9,7 @@ namespace Fungus [RequireComponent(typeof(FungusScript))] public class Sequence : Node { - public string sequenceName = "New Sequence"; + public string sequenceName = "Sequence"; [TextArea(2, 5)] [Tooltip("Description text to display under the sequence node")]