/// <summary> /// Loads all the functions using the given file name. /// </summary> /// <param name="fileName">The file name.</param> /// <param name="functionsName">The functions name.</param> private void LoadFunctions(string fileName, string functionsName) { string ext = Path.GetExtension(fileName); string folder = "LUA"; Functions.Type scriptType = Functions.Type.Lua; if (string.Compare(".cs", ext, true) == 0) { folder = "CSharp"; scriptType = Functions.Type.CSharp; } LoadTextFile( folder, fileName, (filePath) => { if (File.Exists(filePath)) { string text = File.ReadAllText(filePath); FunctionsManager.Get(functionsName).LoadScript(text, functionsName, scriptType); } else { UnityDebugger.Debugger.LogError(folder == "CSharp" ? "CSharp" : "LUA", "file " + filePath + " not found"); } }); }
/// <summary> /// Fire the event named actionName, resulting in all lua functions being called. /// This one reduces GC bloat. /// </summary> /// <param name="actionName">Name of the action being triggered.</param> /// <param name="target">Object, passed to LUA function as 1-argument.</param> /// <param name="parameters">Parameters in question. First one must be target instance. </param> public void Trigger(string actionName, params object[] parameters) { List <string> actions; if (actionsList.TryGetValue(actionName, out actions) && actions != null) { FunctionsManager.Get(parameters[0].GetType().Name).Call(actions, parameters); } }
/// <summary> /// Fire the event named actionName, resulting in all lua functions being called. /// This one reduces GC bloat. /// </summary> /// <param name="actionName">Name of the action being triggered.</param> /// <param name="target">Object, passed to LUA function as 1-argument.</param> /// <param name="parameters">Parameters in question. First one must be target instance. </param> public void Trigger(string actionName, params object[] parameters) { if (!actionsList.ContainsKey(actionName) || actionsList[actionName] == null) { } else { FunctionsManager.Get(parameters[0].GetType().Name).Call(actionsList[actionName], parameters); } }
/// <summary> /// Fire the event named actionName, resulting in all lua functions being called. /// </summary> /// <param name="actionName">Name of the action being triggered.</param> /// <param name="target">Object, passed to LUA function as 1-argument.</param> /// <param name="deltaTime">Time since last Trigger of this event.</param> public void Trigger <T>(string actionName, T target, params object[] parameters) { if (!actionsList.ContainsKey(actionName) || actionsList[actionName] == null) { } else { FunctionsManager.Get(target.GetType().Name).CallWithInstance(actionsList[actionName], target, parameters); } }
/// <summary> /// Loads all the functions using the given file name. /// </summary> /// <param name="fileName">The file name.</param> /// <param name="functionsName">The functions name.</param> private void LoadFunctions(string fileName, string functionsName) { LoadTextFile( "LUA", fileName, (filePath) => { string text = File.ReadAllText(filePath); FunctionsManager.Get(functionsName).LoadScript(text, functionsName); }); }
/// <summary> /// Loads the script file in the given location. /// </summary> /// <param name="file">The file name.</param> /// <param name="functionsName">The functions name.</param> public void LoadFunctionsInFile(FileInfo file, string functionsName) { LoadTextFile( file.DirectoryName, file.Name, (filePath) => { StreamReader reader = new StreamReader(file.OpenRead()); string text = reader.ReadToEnd(); FunctionsManager.Get(functionsName).LoadScript(text, functionsName, file.Extension == ".lua" ? Functions.Type.Lua : Functions.Type.CSharp); }); }
/// <summary> /// Loads the base and the mods scripts. /// </summary> /// <param name="mods">The mods directories.</param> /// <param name="fileName">The file name.</param> private void LoadFunctionScripts(string functionsName, string fileName) { string filePath = Path.Combine(Application.streamingAssetsPath, "LUA"); filePath = Path.Combine(filePath, fileName); if (File.Exists(filePath)) { string text = File.ReadAllText(filePath); FunctionsManager.Get(functionsName).LoadScript(text, functionsName); } foreach (DirectoryInfo mod in mods) { filePath = Path.Combine(mod.FullName, fileName); if (File.Exists(filePath)) { string text = File.ReadAllText(filePath); FunctionsManager.Get(functionsName).LoadScript(text, functionsName); } } }
public void SetClosedAction(string funcName) { Closed = () => FunctionsManager.Get("ModDialogBox").Call(funcName, Result); }
/// <summary> /// Loads the Mod Dialog Box from the XML file. /// </summary> /// <param name="file">The FileInfo object that references to the XML file.</param> public void LoadFromXML(FileInfo file) { // TODO: Find a better way to do this. Not user friendly/Expansible. // ModDialogBox -> Dialog Background // |-> Title // |-> Content Content = transform.GetChild(0).GetChild(1); controls = new Dictionary <string, DialogControl>(); XmlSerializer serializer = new XmlSerializer(typeof(ModDialogBoxInformation)); try { ModDialogBoxInformation dialogBoxInfo = (ModDialogBoxInformation)serializer.Deserialize(file.OpenRead()); Title = dialogBoxInfo.title; foreach (DialogComponent gameObjectInfo in dialogBoxInfo.content) { // Implement new DialogComponents in here. switch (gameObjectInfo.ObjectType) { case "Text": GameObject textObject = (GameObject)Instantiate(Resources.Load("Prefab/DialogBoxPrefabs/DialogText"), Content); textObject.GetComponent <Text>().text = (string)gameObjectInfo.data; textObject.GetComponent <RectTransform>().anchoredPosition = gameObjectInfo.position; break; case "Input": GameObject inputObject = (GameObject)Instantiate(Resources.Load("Prefab/DialogBoxPrefabs/DialogInputComponent"), Content); inputObject.GetComponent <RectTransform>().anchoredPosition = gameObjectInfo.position; inputObject.GetComponent <RectTransform>().sizeDelta = gameObjectInfo.size; controls[gameObjectInfo.name] = inputObject.GetComponent <DialogControl>(); break; case "Image": GameObject imageObject = (GameObject)Instantiate(Resources.Load("Prefab/DialogBoxPrefabs/DialogImage"), Content); Texture2D imageTexture = new Texture2D((int)gameObjectInfo.size.x, (int)gameObjectInfo.size.y); try { imageTexture.LoadImage(File.ReadAllBytes(Path.Combine(Application.streamingAssetsPath, (string)gameObjectInfo.data))); Sprite imageSprite = Sprite.Create(imageTexture, new Rect(0, 0, gameObjectInfo.size.x, gameObjectInfo.size.y), Vector2.zero); imageObject.GetComponent <Image>().sprite = imageSprite; imageObject.GetComponent <RectTransform>().anchoredPosition = gameObjectInfo.position; imageObject.GetComponent <RectTransform>().sizeDelta = gameObjectInfo.size; controls[gameObjectInfo.name] = imageObject.GetComponent <DialogControl>(); } catch (System.Exception error) { Debug.ULogErrorChannel("ModDialogBox", "Error converting image:" + error.Message); return; } break; case "Button": GameObject buttonObject = (GameObject)Instantiate(Resources.Load("Prefab/DialogBoxPrefabs/DialogButton")); buttonObject.GetComponent <RectTransform>().anchoredPosition = gameObjectInfo.position; buttonObject.GetComponent <RectTransform>().sizeDelta = gameObjectInfo.size; buttonObject.GetComponentInChildren <Text>().text = (string)gameObjectInfo.data; buttonObject.GetComponent <DialogButton>().buttonName = gameObjectInfo.name; controls[gameObjectInfo.name] = buttonObject.GetComponent <DialogButton>(); break; } } // Enable dialog buttons from the list of buttons. foreach (DialogBoxResult buttons in dialogBoxInfo.buttons) { switch (buttons) { case DialogBoxResult.Yes: gameObject.transform.GetChild(0).transform.Find("Buttons/btnYes").gameObject.SetActive(true); break; case DialogBoxResult.No: gameObject.transform.GetChild(0).transform.Find("Buttons/btnNo").gameObject.SetActive(true); break; case DialogBoxResult.Cancel: gameObject.transform.GetChild(0).transform.Find("Buttons/btnCancel").gameObject.SetActive(true); break; case DialogBoxResult.Okay: gameObject.transform.GetChild(0).transform.Find("Buttons/btnOK").gameObject.SetActive(true); break; } } EventActions eventActions = new EventActions(); foreach (KeyValuePair <string, string> pair in dialogBoxInfo.Actions) { eventActions.Register(pair.Key, pair.Value); } events = eventActions; FunctionsManager.Get("ModDialogBox").RegisterGlobal(typeof(ModDialogBox)); extraData = new List <object>(); } catch (System.Exception error) { Debug.ULogErrorChannel("ModDialogBox", "Error deserializing data:" + error.Message); } }