protected void Start() { // Dialog always starts invisible, will be faded in when writing starts GetCanvasGroup().alpha = 0f; // Add a raycaster if none already exists so we can handle dialog input GraphicRaycaster raycaster = GetComponent <GraphicRaycaster>(); if (raycaster == null) { gameObject.AddComponent <GraphicRaycaster>(); } // It's possible that SetCharacterImage() has already been called from the // Start method of another component, so check that no image has been set yet. // Same for nameText. if (nameText != null && nameText.text == "") { SetCharacterName("", Color.white); } if (currentCharacterImage == null) { // Character image is hidden by default. SetCharacterImage(null); } stringSubstituter.CacheSubstitutionHandlers(); }
public virtual string SubstituteVariables(string input) { if (stringSubstituer == null) { stringSubstituer = new StringSubstituter(); stringSubstituer.CacheSubstitutionHandlers(); } // Use the string builder from StringSubstituter for efficiency. StringBuilder sb = stringSubstituer._StringBuilder; sb.Length = 0; sb.Append(input); // Instantiate the regular expression object. Regex r = new Regex("{\\$.*?}"); bool changed = false; // Match the regular expression pattern against a text string. var results = r.Matches(input); foreach (Match match in results) { string key = match.Value.Substring(2, match.Value.Length - 3); // Look for any matching private variables in this Flowchart first foreach (Variable variable in variables) { if (variable == null) { continue; } if (variable.Scope == VariableScope.Private && variable.Key == key) { string value = variable.ToString(); sb.Replace(match.Value, value); changed = true; } } } // Now do all other substitutions in the scene changed |= stringSubstituer.SubstituteStrings(sb); if (changed) { return(sb.ToString()); } else { return(input); } }
/// <summary> /// Register some commonly used Unity classes and objects for Lua interop. /// To register more class objects externally to this class, register them in the Awake method of any /// monobehavior in your scene. /// </summary> protected virtual void InitFungusModule() { if (fungusModule == FungusModuleOptions.NoFungusModule) { return; } MoonSharp.Interpreter.Script interpreter = luaEnvironment.Interpreter; // Require the Fungus module and assign it to the global 'fungus' Table fungusTable = null; MoonSharp.Interpreter.DynValue value = interpreter.RequireModule("fungus"); if (value != null && value.Type == DataType.Function) { fungusTable = value.Function.Call().Table; } if (fungusTable == null) { UnityEngine.Debug.LogError("Failed to create Fungus table"); return; } interpreter.Globals["fungus"] = fungusTable; // Static classes fungusTable["time"] = UserData.CreateStatic(typeof(Time)); fungusTable["playerprefs"] = UserData.CreateStatic(typeof(PlayerPrefs)); fungusTable["prefs"] = UserData.CreateStatic(typeof(FungusPrefs)); fungusTable["factory"] = UserData.CreateStatic(typeof(PODTypeFactory)); // Lua Environment and Lua Utils components fungusTable["luaenvironment"] = luaEnvironment; fungusTable["luautils"] = this; // Provide access to the Unity Test Tools (if available). Type testType = Type.GetType("IntegrationTest"); if (testType != null) { UserData.RegisterType(testType); fungusTable["test"] = UserData.CreateStatic(testType); } // Populate the string table by parsing the string table JSON files stringTable = new Table(interpreter); fungusTable["stringtable"] = stringTable; foreach (TextAsset stringFile in stringTables) { if (stringFile.text == "") { continue; } JSONObject stringsJSON = new JSONObject(stringFile.text); if (stringsJSON == null || stringsJSON.type != JSONObject.Type.OBJECT) { UnityEngine.Debug.LogError("String table JSON format is not correct " + stringFile.name); continue; } foreach (string stringKey in stringsJSON.keys) { if (stringKey == "") { UnityEngine.Debug.LogError("String table JSON format is not correct " + stringFile.name); continue; } Table entriesTable = new Table(interpreter); stringTable[stringKey] = entriesTable; JSONObject entries = stringsJSON.GetField(stringKey); if (entries.type != JSONObject.Type.OBJECT) { UnityEngine.Debug.LogError("String table JSON format is not correct " + stringFile.name); continue; } foreach (string language in entries.keys) { string translation = entries.GetField(language).str; entriesTable[language] = translation; } } } stringSubstituter = new StringSubstituter(); stringSubstituter.CacheSubstitutionHandlers(); conversationManager = new ConversationManager(); conversationManager.PopulateCharacterCache(); if (fungusModule == FungusModuleOptions.UseGlobalVariables) { // Copy all items from the Fungus table to global variables foreach (TablePair p in fungusTable.Pairs) { if (interpreter.Globals.Keys.Contains(p.Key)) { UnityEngine.Debug.LogError("Lua globals already contains a variable " + p.Key); } else { interpreter.Globals[p.Key] = p.Value; } } interpreter.Globals["fungus"] = DynValue.Nil; // Note: We can't remove the fungus table itself because of dependencies between functions } }
protected void OnEnable() { // We need to update the cached list every time the Say Dialog is enabled // due to an initialization order issue after loading scenes. stringSubstituter.CacheSubstitutionHandlers(); }