private void OnGUI() { // EditorGUILayout.LabelField("Color the selected obj", EditorStyles.boldLabel); if (GUILayout.Button("OpenFIle")) { string path = EditorUtility.OpenFilePanel("Load text", "", "txt"); string fileName = (path.Substring(path.LastIndexOf("/") + 1, path.LastIndexOf(".") - path.LastIndexOf("/") - 1)); if (path.Length != 0) { firstKey = null; var fileContent = File.ReadAllLines(path); for (int i = 0; i < fileContent.Length; i++) { fileContent[i] = fileContent[i].Trim(); } dialogues = new Dictionary <string, Dialogue>(); dialogues = ProcessTextToDialogue(fileContent); DialogueObj dialogueObj = (DialogueObj)ScriptableObject.CreateInstance(typeof(DialogueObj)); dialogueObj.dialogues = dialogues; dialogueObj.name = fileName; dialogueObj.firstKey = firstKey; dialogueObj.dialogues2 = dialoguesList; //AssetDatabase.CreateAsset(dialogueObj, Application.dataPath + "/" + "Dialogue/" + dialogueObj.name + ".asset"); AssetDatabase.CreateAsset(dialogueObj, "Assets/" + fileName + ".asset"); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } } //EditorGUILayout.ObjectField(source, typeof(UnityEngine.Object), true); // EditorGUILayout.ObjectField(source, typeof(DialogueObj), true); }
//This is where the display of the current dialogue items is located. void OnGUI() { DialogueObj current = diaHash[currentText] as DialogueObj; //Draw the dialogue box background (TextBoxBG) and scale it to the height and width designated by bgWidth and bgHeight GUI.DrawTexture(new Rect(upCornerx, upCornery, bgWidth, bgHeight), TextBoxBG); //The area where all text and options are displayed GUILayout.BeginArea(new Rect(margin, upCornery + margin, bgWidth - (margin * 2), bgHeight - (margin * 2))); GUILayout.BeginVertical(); //Display the current Dialogue Object's text GUILayout.Label(current.text); //For each option associated with the current object text, display a button foreach (DiaOptObj option in current.options) { if (GUILayout.Button(option.response)) { //When the button is selected: //Go to the designated dialogue option (go nowhere if no designated option) if (option.GoTo != null) { currentText = option.GoTo; } //Execute any commands associated with the object if (option.commands != null) { foreach (string[] command in option.commands) { GameObject receiver = GameObject.Find(command[0]) as GameObject; //If there is a parameter associated with this string, parse it as specified and call the function with it. //If not specified, assume it is a string and pass it on as-is. if (command.Length > 3) { if (command[3] == "i") { int par = int.Parse(command[2]); receiver.SendMessage(command[1], par); } else if (command[3] == "f") { float par = float.Parse(command[2]); receiver.SendMessage(command[1], par); } else if (command[3] == "b") { bool par = bool.Parse(command[2]); receiver.SendMessage(command[1], par); } } else if (command.Length == 3) { receiver.SendMessage(command[1], command[2]); } else { //No parameter given, call the function without any parameters receiver.SendMessage(command[1]); } } } } } //End areas and such GUILayout.EndVertical(); GUILayout.EndArea(); }
//End Get and Set methods //Parses a dialogue tree from an XML string. The resulting dialogues are stored as a hash map in diaHash (<string title>, //<DialogueObject>). These are displayed in the OnGUI function - edit there if you want to change their display. This //function just creates the hashMap and the Objects inside it. //XML tags: //<XML> - encloses the document // <DOb name = "name"> - encloses a piece of dialogue; name is how this dialogue piece is referenced // <DText> - encloses the text for this piece of dialogue, it is assummed that DOb will have exactly 1 of these // the text // </DText> // <DOp (GoTo="name")> - encloses a response to the dialogue which is selectable by the player; GoTo is a reference // to a DOb name, it is where the dialogue will go when this response is selected, it is optional // <DResp> - encloses the text for this response, it is assumed that DOp will have exactly 1 of these // the text // </DResp> // <Com Obj="name" Fn="name" (Par="par" (ParType = "type"))/> - represents a command to be executed // Obj - the name of the GameObject which has the script containing the function attached. This is *not* // the name of the script // Fn - the name of the function to be called (such as "setScreenWidth"). Do not include parentheses. // Par - Optional, a parameter to be passed to the fn. This must be in string form, and convertable to // the desired type from a string // ParType - Optional, what type to convert Par to. If not specified, it assumed that it should remain // a string. Possible values: // "i" - convert to int // "f" - convert to float // "b" - convert to bool // </DOp> // </DOb> //</XML> public void parseDialogueTree(string XML) { PT_XMLHashtable xht; int tempCount; int tempCount2; string[][] commands; DiaOptObj[] tempdoo; DialogueObj tempdob; reader.Parse(XML); xht = reader.xml["XML"][0]; foreach(PT_XMLHashtable DOBxht in xht["DOb"]){ tempdob = new DialogueObj(); tempdob.text = DOBxht["DText"][0].text; if(DOBxht.HasKey("DOp")){ tempdoo = new DiaOptObj[DOBxht["DOp"].Count]; tempCount = 0; foreach(PT_XMLHashtable DOPxht in DOBxht["DOp"]){ tempdoo[tempCount] = new DiaOptObj(); tempdoo[tempCount].response = DOPxht["DResp"][0].text; if(DOPxht.ContainsAtt("GoTo")){ tempdoo[tempCount].GoTo = DOPxht.att("GoTo"); } if(DOPxht.HasKey("Com")){ commands = new string[DOPxht["Com"].Count][]; tempCount2 = 0; foreach(PT_XMLHashtable option in DOPxht["Com"]){ if(option.HasAtt("ParType")){ commands[tempCount2] = new string[4]; commands[tempCount2][3] = option.att("ParType"); commands[tempCount2][2] = option.att ("Par"); }else if(option.HasAtt("Par")){ commands[tempCount2] = new string[3]; commands[tempCount2][2] = option.att("Par"); }else{ commands[tempCount2] = new string[2]; } commands[tempCount2][0] = option.att("Obj"); commands[tempCount2][1] = option.att("Fn"); tempCount2++; } tempdoo[tempCount].commands = commands; } tempCount++; } tempdob.options = tempdoo; } diaHash.Add(DOBxht.att("name"),tempdob); } }
//End Get and Set methods //Parses a dialogue tree from an XML string. The resulting dialogues are stored as a hash map in diaHash (<string title>, //<DialogueObject>). These are displayed in the OnGUI function - edit there if you want to change their display. This //function just creates the hashMap and the Objects inside it. //XML tags: //<XML> - encloses the document // <DOb name = "name"> - encloses a piece of dialogue; name is how this dialogue piece is referenced // <DText> - encloses the text for this piece of dialogue, it is assummed that DOb will have exactly 1 of these // the text // </DText> // <DOp (GoTo="name")> - encloses a response to the dialogue which is selectable by the player; GoTo is a reference // to a DOb name, it is where the dialogue will go when this response is selected, it is optional // <DResp> - encloses the text for this response, it is assumed that DOp will have exactly 1 of these // the text // </DResp> // <Com Obj="name" Fn="name" (Par="par" (ParType = "type"))/> - represents a command to be executed // Obj - the name of the GameObject which has the script containing the function attached. This is *not* // the name of the script // Fn - the name of the function to be called (such as "setScreenWidth"). Do not include parentheses. // Par - Optional, a parameter to be passed to the fn. This must be in string form, and convertable to // the desired type from a string // ParType - Optional, what type to convert Par to. If not specified, it assumed that it should remain // a string. Possible values: // "i" - convert to int // "f" - convert to float // "b" - convert to bool // </DOp> // </DOb> //</XML> public void parseDialogueTree(string XML) { PT_XMLHashtable xht; int tempCount; int tempCount2; string[][] commands; DiaOptObj[] tempdoo; DialogueObj tempdob; reader.Parse(XML); xht = reader.xml["XML"][0]; foreach (PT_XMLHashtable DOBxht in xht["DOb"]) { tempdob = new DialogueObj(); tempdob.text = DOBxht["DText"][0].text; if (DOBxht.HasKey("DOp")) { tempdoo = new DiaOptObj[DOBxht["DOp"].Count]; tempCount = 0; foreach (PT_XMLHashtable DOPxht in DOBxht["DOp"]) { tempdoo[tempCount] = new DiaOptObj(); tempdoo[tempCount].response = DOPxht["DResp"][0].text; if (DOPxht.ContainsAtt("GoTo")) { tempdoo[tempCount].GoTo = DOPxht.att("GoTo"); } if (DOPxht.HasKey("Com")) { commands = new string[DOPxht["Com"].Count][]; tempCount2 = 0; foreach (PT_XMLHashtable option in DOPxht["Com"]) { if (option.HasAtt("ParType")) { commands[tempCount2] = new string[4]; commands[tempCount2][3] = option.att("ParType"); commands[tempCount2][2] = option.att("Par"); } else if (option.HasAtt("Par")) { commands[tempCount2] = new string[3]; commands[tempCount2][2] = option.att("Par"); } else { commands[tempCount2] = new string[2]; } commands[tempCount2][0] = option.att("Obj"); commands[tempCount2][1] = option.att("Fn"); tempCount2++; } tempdoo[tempCount].commands = commands; } tempCount++; } tempdob.options = tempdoo; } diaHash.Add(DOBxht.att("name"), tempdob); } }