// Add a new message to the persistent data folder public static void SaveMessage(Message m) { string path = Application.persistentDataPath + "/Resources/ChatApp/Messages/" + m.chat.name; if (!System.IO.Directory.Exists(path)) { System.IO.Directory.CreateDirectory(path); } int count = System.IO.Directory.GetFiles(path).Length; string countName = ""; if (count < 10) { countName = "000" + count; } else if (count < 100) { countName = "00" + count; } else if (count < 1000) { countName = "0" + count; } SOSaver.Save(path + "/ " + m.chat.name + "_" + countName + ".dat", m); }
// Store new dialogue options public static void AddDialogueOption(MessageOption option) { string path = Application.persistentDataPath + "/Resources/ChatApp/Options/" + option.chat.name; if (!System.IO.Directory.Exists(path)) { System.IO.Directory.CreateDirectory(path); } int count = System.IO.Directory.GetFiles(path).Length; SOSaver.Save(path + "/ " + option.chat.name + "_" + count + ".dat", option); }
public Message LastMessage() { string path = Application.persistentDataPath + "/Resources/ChatApp/Messages/" + name; if (System.IO.Directory.Exists(path)) { Message[] messages = SOSaver.LoadAll <Message>(path); if (messages.Length > 0) { return(messages[messages.Length - 1]); } } return(null); }
// Clear the decisions and add new decisions public void ResetDecisions(Chat activeChat) { // Clear grid foreach (Transform child in decisionGrid.transform) { if (child != decisionGrid.transform) { Destroy(child.gameObject); } } decisionGrid.ClearGrid(); if (activeChat.queue.isEmpty) { // Add new decisions string path = Application.persistentDataPath + "/Resources/ChatApp/Options/" + activeChat.name; if (System.IO.Directory.Exists(path)) { MessageOption[] options = SOSaver.LoadAll <MessageOption>(path); foreach (MessageOption d in options) { if (d.chat == activeChat) { UIMessageOption newDecision = Instantiate(Resources.Load <UIMessageOption>("Apps/ChatApp/Prefabs/DialogueOption"), decisionGrid.transform); newDecision.Initialise(d); if (newDecision) { decisionGrid.cells.Add(newDecision); } } } decisionGrid.ResetGrid(); } } }
// Clear message screen and load new messages public IEnumerator WipeAndRedrawMessages(Chat activeChat) { // Clear grid List <GameObject> oldMessages = new List <GameObject>(); foreach (Transform child in messageGrid.transform) { if (child != messageGrid.transform) { oldMessages.Add(child.gameObject); } } messageGrid.ClearGrid(); // Set contact information messageScreenContactName.text = activeChat.name; avatar.sprite = activeChat.image; SetStatus(activeChat); // Add new messages string path = Application.persistentDataPath + "/Resources/ChatApp/Messages/" + activeChat.name; if (System.IO.Directory.Exists(path)) { Message[] messages = SOSaver.LoadAll <Message>(path); List <UIMessage> messageItems = new List <UIMessage>(); foreach (Message m in messages) { if (m.chat && m.chat == activeChat) { UIMessage newMessage = null; // Choose prefab based on sender if (m.contact == NarrativeHandler.instance.You) { newMessage = Instantiate(Resources.Load <UIMessage>("Apps/ChatApp/Prefabs/MessageByYou"), messageGrid.transform); } else { newMessage = Instantiate(Resources.Load <UIMessage>("Apps/ChatApp/Prefabs/MessageByContact"), messageGrid.transform); } if (newMessage) { newMessage.SetText(m.text); messageGrid.cells.Add(newMessage); messageItems.Add(newMessage); } } } yield return(null); // Remove old messages foreach (GameObject g in oldMessages) { Destroy(g); } // Scale new messages and push them forward foreach (UIMessage m in messageItems) { Vector3 position = m.transform.localPosition; position.z = 0; m.transform.localPosition = position; m.ScaleToFitText(); } // Check for incoming messages if (activeChat.HasIncommingMessage()) { Division upcomming = Instantiate(UpcommingMessage, messageGrid.transform); messageGrid.cells.Add(upcomming); } messageGrid.ResetGrid(); } sendBox.Reset(activeChat); }