Home>

I want to display one character at a time, taking into account rich text + α in Unity.
Also, write a sentence. I'm trying to read a .txt file.
When executed, Unity will not respond without any error message.
I think the OutputChar function is causing the problem.
Thanks for your advice.

Test.txt
[text]<speed = 1.00>Change speed</speed>
[end]

code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using System.Text;
using System;
public class TextDisplay: MonoBehaviour {
    public string [] sentences;// Store the sentences
    public string file;// sentence before separation
    [SerializeField] Text uiText;// Reference to UiText
    [SerializeField] Text uiName;// Reference to UiName
    [SerializeField]
    [Range (0.001f, 0.3f)]
    float intervalForCharDisplay = 0.05f;// 1 Time to display characters

    private int currentSentenceNum = -1;// Current document number
    private string currentSentence = string.Empty;// current string
    private float timeTmpDisplay = 0.0f;// Temporary interval for character display
    private bool isTmpTime = false;
    private float timeBeganDisplay = 1;// Time when the display of the character string starts
    [SerializeField] public string filename;// = string.Empty;// File name to read
    private bool isTimerSet = false;
    int displayCharCount = 0;
    string [] endTagStack = {null, null, null, null, null};
    int tagNestNum = 0;
    // Use this for initialization
    void Start () {
        SetSentence ();
        SetNextSentence ();
    }
    // Update is called once per frame
    void Update () {
        // Sentence complete/incomplete
        if (IsDisplayComplete ())
        {
            // not last sentence&button pressed
            if ((currentSentenceNum + 1)&lt;sentences.Length&amp;&amp;Input.GetKeyUp (KeyCode.Space))
            {
                SetNextSentence ();
            }
        }
        else
        {
            // button was pressed
            if (Input.GetKeyUp (KeyCode.Space))
            {
                uiText.text = sentences [currentSentenceNum];
                displayCharCount = sentences [currentSentenceNum] .Length;
            }
            else if (isNextCharTime ())
            {
                OutputChar ();
            }
        }
    }
    // ------------------------------------------------ -------------------------------
    // output character
    void OutputChar ()
    {
        Debug.Log ("callOutputChar");
        displayCharCount ++;
        while (currentSentence.Substring (displayCharCount-1,1) == "&lt;")
        {
            int startIndex = displayCharCount-1;
            int endIndex = currentSentence.IndexOf ("&gt;", startIndex + 1);
            int tagLength = (endIndex-startIndex) + 1;
            bool isCanDChack = (currentSentence.Length-startIndex)&gt;7;
            bool isTagDelay = false;
            if (isCanDChack)
            {
                Debug.Log ("isCanDChack = true");
                isTagDelay = currentSentence.Substring (startIndex + 1, 6) == "/ speed" ||
                 currentSentence.Substring (startIndex + 1, 5) == "speed";
            }
            Debug.Log ("isTagDelay =" + isTagDelay);
            if (isTagDelay)
            {
                Debug.Log ("call delay");
                if (currentSentence.Substring (startIndex, tagLength) .Contains ("="))
                    timeTmpDisplay = float.Parse (currentSentence.Substring (startIndex + 7, 4));// converted to number
                currentSentence.Remove (startIndex, tagLength);
                isTmpTime = (isTmpTime == false);// Swap the switch
                continue;
            }
            else if (currentSentence.Substring (startIndex + 1,1) == "/")
            {
                displayCharCount + = tagLength;// Proceed display by the number of characters in the tag
                endTagStack [tagNestNum-1] = null;
                tagNestNum--;
                continue;
            }
            displayCharCount + = tagLength;// Proceed display by the number of characters in the tagstring endTag = "&lt;/" + currentSentence.Substring (startIndex + 1, tagLength-1);
            if (endTag.Contains ("="))
                endTag = endTag.Remove (endTag.IndexOf ("="), endTag.Length-endTag.IndexOf ("=")-1);
            endTagStack [tagNestNum] = endTag;
            tagNestNum ++;
        }
        uiText.text = currentSentence.Substring (0, displayCharCount);
        // for rich text
        if (endTagStack [0]! = null)
        {
            for (int i = 1;i&lt;tagNestNum + 1;i ++)
            {
                uiText.text + = endTagStack [tagNestNum-i];
            }
        }
    }
    // ------------------------------------------------ -------------------------------
    // Set next sentence
    void SetNextSentence ()
    {
        Debug.Log ("callSetSentence");
        currentSentenceNum ++;
        currentSentence = sentences [currentSentenceNum];
        displayCharCount = 0;
        for (int i = 0;i&lt;endTagStack.Length;i ++)
            endTagStack [i] = null;
        tagNestNum = 0;
    }
    // ------------------------------------------------ -------------------------------
    //
    bool IsDisplayComplete ()
    {
        return displayCharCount&gt;= sentences [currentSentenceNum] .Length;
    }
    // ------------------------------------------------ -----
    //
    void Readfile ()
    {
        // Read "FileName" .txt
        TextAsset t = Resources.Load ("text/Test", typeof (TextAsset)) as TextAsset;
        file = t.text;
    }
    // ------------------------------------------------ -------------------------------
    // Set the read file
    void SetSentence ()
    {
        string Type = "";
        int startIndex = 0, endIndex = 0;
        // read the file
        Readfile ();
        int textSize = (file.Length-file.Replace ("[text]", "") .Length)/6;
        sentences = new string [textSize];
        startIndex = file.IndexOf ('[');
        Debug.Log (startIndex);
        Debug.Log ("file.Length" + file.Length);
        int numSentence = 0;
        while (true)
        {
            endIndex = file.IndexOf (']', startIndex + 1);
            Type = file.Substring (startIndex + 1, (endIndex-startIndex-1));
            if (Type == "end") break;
            startIndex = file.IndexOf ('[', endIndex + 1);// Index to the next instruction
            if (Type == "text")
            {
                sentences [numSentence] = file.Substring (endIndex + 1, ((startIndex-1)-endIndex));
                numSentence ++;
                Type = "";
            }
            else if (Type == "name") {
                uiName.text = file.Substring (endIndex + 1, ((startIndex-1)-endIndex));
                Type = "";
            }
        }
    }
    // ------------------------------------------------ -------------------------------
    // Function that returns true when it is time to display the next character
    bool isNextCharTime ()
    {
        if (isTimerSet)
        {
            float T;
            if (isTmpTime)
                T = timeTmpDisplay;
            else
            {
                T = intervalForCharDisplay;
                timeTmpDisplay = 0.0f;
            }
            if (Time.time-timeBeganDisplay&gt;T)
            {
                isTimerSet = false;
                timeBeganDisplay = 0;
                return true;
            }
            return false;
        }
        isTimerSet = true;
        timeBeganDisplay = Time.time;
        return false;
    }
}
  • Answer # 1

    Did not assign! ! !
    I'm sorry. I want to delete a post

    Code
    // output character
        void OutputChar ()
        {
            Debug.Log ("callOutputChar");
            displayCharCount ++;
            while (currentSentence.Substring (displayCharCount-1,1) == "<")
            {
                int startIndex = displayCharCount-1;
                int endIndex = currentSentence.IndexOf (">", startIndex + 1);
                int tagLength = (endIndex-startIndex) + 1;
                bool isCanDChack = (currentSentence.Length-startIndex)>7;
                bool isTagDelay = false;
                if (isCanDChack)
                {
                    Debug.Log ("isCanDChack = true");
                    isTagDelay = currentSentence.Substring (startIndex + 1, 6) == "/ speed";
                    if (! isTagDelay)
                        isTagDelay = currentSentence.Substring (startIndex + 1, 5) == "speed";
                }
                Debug.Log ("isTagDelay =" + isTagDelay);
                if (isTagDelay)
                {
                    Debug.Log ("call delay");
                    if (currentSentence.Substring (startIndex, tagLength) .Contains ("="))
                        timeTmpDisplay = float.Parse (currentSentence.Substring (startIndex + 7, 4));// converted to number
               ====>
               ====>"currentSentence =" currentSentence.Remove (startIndex, tagLength);
               ====>
                    isTmpTime = (isTmpTime == false);// Swap the switch
                    continue;
                }
                else if (currentSentence.Substring (startIndex + 1,1) == "/")
                {
                    displayCharCount + = tagLength;// Proceed display by the number of characters in the tag
                    endTagStack [tagNestNum-1] = null;
                    tagNestNum--;
                    continue;
                }
                displayCharCount + = tagLength;// Proceed display by the number of characters in the tag
                string endTag = "</" + currentSentence.Substring (startIndex + 1, tagLength-1);
                if (endTag.Contains ("="))
                    endTag = endTag.Remove (endTag.IndexOf ("="), endTag.Length-endTag.IndexOf ("=")-1);
                endTagStack [tagNestNum] = endTag;
                tagNestNum ++;
            }
            uiText.text = currentSentence.Substring (0, displayCharCount);
            // for rich text
            if (endTagStack [0]! = null)
            {
                for (int i = 1;i<tagNestNum + 1;i ++)
                {
                    uiText.text + = endTagStack [tagNestNum-i];
                }
            }
        }