// Add a text object // Optional parameter: CutsceneTextData - used to preload values in to the text object public void AddText(CutsceneTextData ctd) { // instantiate a new text object from our prefab GameObject textGO = Instantiate(cutsceneTextObjectPrefab); textGO.name = "CutsceneText" + textCount; textGO.transform.SetParent(transform, false); // grab the rect transform and the text components for editing RectTransform rt = textGO.GetComponent <RectTransform>(); Text text = textGO.GetComponent <Text>(); // make an object for our struct TextAndRectTransform tart = new TextAndRectTransform(); // set up a new text with default information if (ctd == null) { tart = ParseDataToText(textDefaults, text, rt); } // if given text, set up the text from that data else { tart = ParseDataToText(ctd, text, rt); } // set the text and the rect transform we're using text = tart.text; rt = tart.rt; textCount++; // add to our count of active texts allActiveTexts.Add(text); // add the text to our list }
// load any existing preset defaults void LoadFromJson() { if (File.Exists(filePath)) { // Read the json from the file into a string string dataAsJson = File.ReadAllText(filePath); // Pass the json to JsonUtility, and tell it to create a GameData object from it CutsceneTextData loadedData = JsonUtility.FromJson <CutsceneTextData>(dataAsJson); // Set parameters based on data from loadedData // check for default font if (!loadedData.font.Equals("Arial")) { // attempt to find the font in Assets/Resources/Fonts try { font = Resources.Load <Font>("Fonts/" + loadedData.font); } catch (Exception e) { Debug.LogError("Could not find font. Escaping CutsceneTextObject Init.\n" + e.Message); return; } } else { font = Resources.GetBuiltinResource(typeof(Font), "Arial.ttf") as Font; } // attempt to parse the enum for the TextAnchor try { anchor = (TextAnchor)Enum.Parse(typeof(TextAnchor), loadedData.textAnchor); } catch (Exception e) { Debug.LogError("TextAnchor string to enum parse failed. Escaping CutsceneTextObject Init.\n" + e.Message); return; } // set up font size, hold time, and font color fontSize = loadedData.fontSize; holdTime = loadedData.holdTime; color = new Color(loadedData.fontColor[0], loadedData.fontColor[1], loadedData.fontColor[2], loadedData.fontColor[3]); // set up the rect transform's default position and sizeDelta pos = new Vector3(loadedData.position[0], loadedData.position[1], loadedData.position[2]); sizeDelta = new Vector2(loadedData.sizeDelta[0], loadedData.sizeDelta[1]); } // These are the defaults set by Unity with the exception of holdTime, which we'll default to the default value of a float else { fontSize = 14; color = Color.black; anchor = TextAnchor.UpperLeft; font = Resources.GetBuiltinResource(typeof(Font), "Arial.ttf") as Font; sizeDelta = new Vector2(160, 30); holdTime = 0; } }
// grab the text defaults if they exist when CutsceneCreator is awoken void Awake() { if (File.Exists(textDefaultFilePath)) { // Read the json from the file into a string string dataAsJson = File.ReadAllText(textDefaultFilePath); // Pass the json to JsonUtility, and tell it to create a GameData object from it textDefaults = JsonUtility.FromJson <CutsceneTextData>(dataAsJson); } }
// Initialize this object given specific CutsceneTextData and the amount of time to delay typing each letter (the higher this value, the slower it types public void Init(CutsceneTextData ctd, float typeDelaySpeed) { // check for default font if (!ctd.font.Equals("Arial")) { // attempt to find the font in Assets/Resources/Fonts try { text.font = Resources.Load <Font>("Fonts/" + ctd.font); } catch (Exception e) { Debug.LogError("Could not find font. Escaping CutsceneTextObject Init.\n" + e.Message); return; } } else { text.font = Resources.GetBuiltinResource(typeof(Font), "Arial.ttf") as Font; } text.text = ""; // set the text to empty for now text.fontSize = ctd.fontSize; // set the font size // attemp to parse the enum for the TextAnchor try { text.alignment = (TextAnchor)Enum.Parse(typeof(TextAnchor), ctd.textAnchor); } catch (Exception e) { Debug.LogError("TextAnchor string to enum parse failed. Escaping CutsceneTextObject Init.\n" + e.Message); return; } text.color = new Color(ctd.fontColor[0], ctd.fontColor[1], ctd.fontColor[2], 0); // set the text color - set alpha to 0 so we can fade the text in maxAlpha = ctd.fontColor[3]; textToShow = ctd.textToShow; // save the full text we're going to display in the end holdTime = ctd.holdTime; // save the hold time we'll wait before displaying the next text RectTransform rectTransform = GetComponent <RectTransform>(); // grab the rect transform rectTransform.localPosition = new Vector3(ctd.position[0], ctd.position[1], ctd.position[2]); // set the position of the text in the scene rectTransform.sizeDelta = new Vector2(ctd.sizeDelta[0], ctd.sizeDelta[1]); // set the size of the text field in the scene if (cutsceneManager.typeText) { TypeSentence(typeDelaySpeed); // begin typing the sentence with the set typeDelaySpeed } else { DisplayAndFade(); // display the entire sentence, but fade in the text } }
// Parses CutsceneTextData into the components for a Text object and a RectTransform, then returns them as a TextAndRectTransform struct object public TextAndRectTransform ParseDataToText(CutsceneTextData ctd, Text text, RectTransform rt) { TextAndRectTransform tart = new TextAndRectTransform(); text.text = ctd.textToShow; // fill in the text field // try to find and set the font // check for default font if (!ctd.font.Equals("Arial")) { try { // search from Assets/Resouces/Fonts/ text.font = Resources.Load <Font>("Fonts/" + ctd.font); } catch (System.Exception e) { Debug.LogError("Could not find font named " + ctd.font + ". Escaping CutsceneTextObject Init.\n" + e.Message); return(tart); } } else { text.font = Resources.GetBuiltinResource(typeof(Font), "Arial.ttf") as Font; } text.fontSize = ctd.fontSize; // set font size // attempt to set the text alignment try { text.alignment = (TextAnchor)System.Enum.Parse(typeof(TextAnchor), ctd.textAnchor); } catch (System.Exception e) { Debug.LogError("TextAnchor string to enum parse failed. Cannot parse " + ctd.textAnchor + ". Escaping CutsceneTextObject Init.\n" + e.Message); return(tart); } // set the color of the text text.color = new Color(ctd.fontColor[0], ctd.fontColor[1], ctd.fontColor[2], ctd.fontColor[3]); // set the position of the text and the size of the rectangle rt.localPosition = new Vector3(ctd.position[0], ctd.position[1], ctd.position[2]); rt.sizeDelta = new Vector2(ctd.sizeDelta[0], ctd.sizeDelta[1]); // set the components of the TextAndRectTransform tart.text = text; tart.rt = rt; return(tart); }
// Called by CutsceneCreatorTextEditorWindow // updates text data in our list or adds the data if it did not previously exist public void UpdateTextData(int index, CutsceneTextData ctd) { // check if there is already data at the target index in the list if (currentTextDataList.Count > index && currentTextDataList[index] != null) { currentTextDataList[index] = ctd; Debug.Log("Text " + index + "overridden"); } else { currentTextDataList.Add(ctd); Debug.Log("Text " + index + " saved"); } }
CutsceneCreator cutsceneCreator; // the CutsceneCreator we are using // Initialize the window // requires a text we will edit and a CutsceneCreator we will manipulate the values of public void Init(Text textToEdit, CutsceneCreator cc) { // set up and show the window CutsceneCreatorTextEditorWindow window = (CutsceneCreatorTextEditorWindow)GetWindow(typeof(CutsceneCreatorTextEditorWindow), true); window.Show(); text = textToEdit; rt = text.gameObject.GetComponent <RectTransform>(); cutsceneCreator = cc; targetIndex = text.transform.GetSiblingIndex(); // the index we target will be whatever the text's transform's index is in the hierarchy // check if there is text data in the CutsceneCreator already and if we're editing something existing rather than something new if (cutsceneCreator.currentTextDataList.Count > targetIndex && cutsceneCreator.currentTextDataList[targetIndex] != null) { try { ctd = cutsceneCreator.currentTextDataList[targetIndex]; holdTime = ctd.holdTime; // set the hold time } catch (Exception e) { Debug.LogError("Could not find textData of index " + targetIndex + ". Further details: " + e.Message); } } else // otherwise, set up the defaults for a new text object and make a new data and add the window to the list { // Call upon the CutsceneCreator's struct and method to set up the default parameters CutsceneCreator.TextAndRectTransform tart = new CutsceneCreator.TextAndRectTransform(); tart = cc.ParseDataToText(cc.textDefaults, text, rt); // set the parameters for the objects this class is using via the struct in CutsceneCreator text = tart.text; rt = tart.rt; // set up holdTime separately as this is not included in the CutsceneCreator's method holdTime = cc.textDefaults.holdTime; // add the new cutscene text data to the window ctd = new CutsceneTextData(); cutsceneCreator.activeWindows.Add(targetIndex, this); } }
void OnGUI() { // set up fields to manipulate based on the defaults we want to set for new texts GUILayout.Label("Default Text Options", EditorStyles.boldLabel); font = (Font)EditorGUILayout.ObjectField("Font: ", font, typeof(Font), false); fontSize = EditorGUILayout.IntField("Size: ", fontSize); color = EditorGUILayout.ColorField("Color: ", color); anchor = (TextAnchor)EditorGUILayout.EnumPopup("Alignment: ", anchor); pos = EditorGUILayout.Vector3Field("Position: ", pos); sizeDelta = EditorGUILayout.Vector2Field("Width and Height: ", sizeDelta); holdTime = EditorGUILayout.FloatField("HoldTime: ", holdTime); EditorGUILayout.Space(); // save the data if (GUILayout.Button("Save")) { defaultTextOptions = new CutsceneTextData(); defaultTextOptions.textToShow = "Enter your text here."; defaultTextOptions.fontSize = fontSize; defaultTextOptions.holdTime = holdTime; defaultTextOptions.font = font.name; defaultTextOptions.textAnchor = anchor.ToString(); defaultTextOptions.fontColor = new float[] { color.r, color.g, color.b, color.a }; defaultTextOptions.position = new float[] { pos.x, pos.y, pos.z }; defaultTextOptions.sizeDelta = new float[] { sizeDelta.x, sizeDelta.y }; Save(); cutsceneCreator.textDefaults = defaultTextOptions; // set the text defaults in the cutsceneCreator Close(); // close the window when we save } }