diff --git a/Assets/Fungus/Editor/FungusScript/FungusScriptEditor.cs b/Assets/Fungus/Editor/FungusScript/FungusScriptEditor.cs index 113677bc..6003b81c 100644 --- a/Assets/Fungus/Editor/FungusScript/FungusScriptEditor.cs +++ b/Assets/Fungus/Editor/FungusScript/FungusScriptEditor.cs @@ -11,16 +11,6 @@ namespace Fungus.Script [CustomEditor (typeof(FungusScript))] public class FungusScriptEditor : Editor { - SerializedProperty variablesProperty; - - void OnEnable() - { - if (this != null && serializedObject != null) - { - variablesProperty = serializedObject.FindProperty("variables"); - } - } - public void OnInspectorUpdate() { Repaint(); @@ -28,8 +18,6 @@ namespace Fungus.Script public override void OnInspectorGUI() { - serializedObject.Update(); - FungusScript t = target as FungusScript; if (t != null) @@ -136,8 +124,6 @@ namespace Fungus.Script DrawAddCommandGUI(t.selectedSequence); } } - - serializedObject.ApplyModifiedProperties(); } public void DrawSequenceGUI(Sequence sequence) @@ -244,7 +230,8 @@ namespace Fungus.Script FungusScript t = target as FungusScript; ReorderableListGUI.Title("Variables"); - + + SerializedProperty variablesProperty = serializedObject.FindProperty("variables"); FungusVariableListAdaptor adaptor = new FungusVariableListAdaptor(variablesProperty, 0); ReorderableListControl.DrawControlFromState(adaptor, null, ReorderableListFlags.DisableContextMenu | ReorderableListFlags.HideAddButton); @@ -277,33 +264,9 @@ namespace Fungus.Script Undo.RecordObject(fungusScript, "Add Variable"); T variable = fungusScript.gameObject.AddComponent(); - variable.key = MakeUniqueKey(fungusScript); + variable.key = fungusScript.GetUniqueVariableKey(""); fungusScript.variables.Add(variable); } - - string MakeUniqueKey(FungusScript fungusScript) - { - int index = 0; - while (true) - { - string key = "Var" + index; - - bool found = false; - foreach(FungusVariable variable in fungusScript.GetComponents()) - { - if (variable.key == key) - { - found = true; - index++; - } - } - - if (!found) - { - return key; - } - } - } } } \ No newline at end of file diff --git a/Assets/Fungus/Editor/FungusScript/FungusVariableListAdaptor.cs b/Assets/Fungus/Editor/FungusScript/FungusVariableListAdaptor.cs index bc2f60fa..b20435a4 100644 --- a/Assets/Fungus/Editor/FungusScript/FungusVariableListAdaptor.cs +++ b/Assets/Fungus/Editor/FungusScript/FungusVariableListAdaptor.cs @@ -5,7 +5,6 @@ using UnityEngine; using UnityEditor; using System; -using System.Linq; using Rotorz.ReorderableList; namespace Fungus.Script @@ -13,7 +12,7 @@ namespace Fungus.Script public class FungusVariableListAdaptor : IReorderableListAdaptor { private SerializedProperty _arrayProperty; - + public float fixedItemHeight; public SerializedProperty this[int index] { @@ -162,19 +161,18 @@ namespace Fungus.Script } VariableScope scope = (VariableScope)EditorGUI.EnumPopup(scopeRect, variable.scope); - + + FungusScript fungusScript = FungusScriptWindow.GetFungusScript(); + if (EditorGUI.EndChangeCheck ()) { Undo.RecordObject(variable, "Set Variable"); - - char[] arr = key.Where(c => (char.IsLetterOrDigit(c) || c == '_')).ToArray(); - key = new string(arr); - - variable.key = key; + + // Modify the key if it clashes with an existing variable key + variable.key = fungusScript.GetUniqueVariableKey(key, variable); variable.scope = scope; } - FungusScript fungusScript = FungusScriptWindow.GetFungusScript(); if (fungusScript != null) { bool highlight = false; diff --git a/Assets/Fungus/VisualScripting/FungusScript.cs b/Assets/Fungus/VisualScripting/FungusScript.cs index d16f2e7a..cb8372ef 100644 --- a/Assets/Fungus/VisualScripting/FungusScript.cs +++ b/Assets/Fungus/VisualScripting/FungusScript.cs @@ -1,8 +1,6 @@ -#if UNITY_EDITOR -using UnityEditor; -#endif -using UnityEngine; +using UnityEngine; using System; +using System.Linq; using System.Collections; using System.Collections.Generic; @@ -73,6 +71,50 @@ namespace Fungus.Script selectedSequence = sequence; sequence.ExecuteNextCommand(); } + + public string GetUniqueVariableKey(string originalKey, FungusVariable ignoreVariable = null) + { + int suffix = 0; + string baseKey = originalKey; + + // Only letters and digits allowed + char[] arr = baseKey.Where(c => (char.IsLetterOrDigit(c) || c == '_')).ToArray(); + baseKey = new string(arr); + + // No leading digits allowed + baseKey = baseKey.TrimStart('0','1','2','3','4','5','6','7','8','9'); + + // No empty keys allowed + if (baseKey.Length == 0) + { + baseKey = "Var"; + } + + string key = baseKey; + while (true) + { + bool collision = false; + foreach(FungusVariable variable in GetComponents()) + { + if (variable == ignoreVariable) + { + continue; + } + + if (variable.key.Equals(key, StringComparison.CurrentCultureIgnoreCase)) + { + collision = true; + suffix++; + key = baseKey + suffix; + } + } + + if (!collision) + { + return key; + } + } + } } } \ No newline at end of file diff --git a/Assets/Shuttle/ShuttleGame.unity b/Assets/Shuttle/ShuttleGame.unity index 3c9f6069..e43a687f 100644 Binary files a/Assets/Shuttle/ShuttleGame.unity and b/Assets/Shuttle/ShuttleGame.unity differ