Example #1
0
    // 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
    }
Example #2
0
    // 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);
    }