如何在Unity中以时间间隔显示每个文本行(墨水)?

时间:2019-07-05 06:57:20

标签: c# unity3d

我正在开发基于文本的交互式游戏,并使用Ink插件。基本上,我想按时间间隔显示每个故事情节,例如游戏LIFELINE或ZARYA。如何创建循环?

我尝试了Invoke方法,如下所示。

using UnityEngine;
using System.Collections;
using Ink.Runtime;
using UnityEngine.UI;

public class Main : MonoBehaviour
{
    public TextAsset inkAsset;
    public Story _inkStory;
    public bool storyNeeded;
    public ScrollRect _scrollRect;

    // Pop-ups
    public GameObject _baskanPopup;
    public GameObject _baskanMesaj;
    public GameObject _missionPopup;
    public GameObject _missionMessage;

    // Chat Prefabs
    [SerializeField]
    public GameObject _chatLine;
    public GameObject _content;

    // AutoScroll
    [SerializeField]
    private GameObject _line;
    [SerializeField]
    private GameObject _choices;

    /* UI Prefabs */
    [SerializeField]
    private UnityEngine.UI.Button button;

    void Awake ()
    {
        _inkStory = new Story (inkAsset.text);
        storyNeeded = true;
    }

    void Start(){ }

    void Update ()
    {
        if (storyNeeded == true)
        {
            while (_inkStory.canContinue)
            {
                Invoke("Spawn", 3f);

                // Ink story position
                Text storyText = _line.GetComponentInChildren<Text>();
                storyText.transform.localPosition = new Vector2(422, -171);

                // Ink story keeps continue
                storyText.text = _inkStory.Continue();

                // Variables
                int president = (int)_inkStory.variablesState["PRESIDENT"];
                int mission = (int)_inkStory.variablesState["TATI_MISSION"];
                int systemName = (int)_inkStory.variablesState["name_SYSTEM"];
                int timeAgent = (int)_inkStory.variablesState["TIMEAGENT000001"];
                int timeAgent2 = (int)_inkStory.variablesState["TIMEAGENT110432"];

                // System name
                if (systemName == 1)
                {
                    GameObject chatLine = _line.transform.Find("chatLine").gameObject;
                    Text agentNameText = chatLine.GetComponentInChildren<Text>();
                    agentNameText.text = "Sistem";
                }

                //President pop-up
                if (president == 1)
                {
                    _baskanPopup.SetActive(true);
                    Text presidentMessage = Instantiate(storyText);
                    presidentMessage.transform.SetParent(_baskanMesaj.transform);
                }
                else
                {
                    _baskanPopup.SetActive(false);
                    RemovePresidentMessage();
                }

                // Mission pop-up
                if (mission == 1)
                {
                    _missionPopup.SetActive(true);
                    storyText.transform.SetParent(_missionMessage.transform);
                    storyText.transform.localPosition = new Vector2(0, 0);
                }
                else
                {
                    _missionPopup.SetActive(false);
                    RemoveMissionMessage();
                }

                if (timeAgent == 1)
                {
                    Transform agentName = _line.transform.Find("chatLine/agentNumber");
                    Text agentNameText = agentName.GetComponent<Text>();
                    agentNameText.text = "00 0001";
                }

                if (timeAgent2 == 1)
                {
                    Transform agentName = _line.transform.Find("chatLine/agentNumber");
                    Text agentNameText = agentName.GetComponent<Text>();
                    agentNameText.text = "110432";
                }
            }

            if (_inkStory.currentChoices.Count > 0)
            {
                //President variable
                int president = (int)_inkStory.variablesState["PRESIDENT"];

                RemoveButtons();
                for (int ii = 0; ii < _inkStory.currentChoices.Count; ++ii)
                {
                    UnityEngine.UI.Button choice = Instantiate (button) as UnityEngine.UI.Button;
                    choice.transform.SetParent (_choices.transform, false);
                    choice.transform.Translate (new Vector2 (0, 0));

                    UnityEngine.UI.Text choiceText = choice.GetComponentInChildren<UnityEngine.UI.Text> ();
                    choiceText.text = _inkStory.currentChoices [ii].text;

                    UnityEngine.UI.HorizontalLayoutGroup layoutGroup = choice.GetComponent <UnityEngine.UI.HorizontalLayoutGroup> ();

                    int choiceId = ii;
                    choice.onClick.AddListener(delegate{ChoiceSelected(choiceId);});

                    if (president == 1)
                    {

                        choice.transform.localPosition = new Vector2(0, -991);
                    }
                }
            }

            storyNeeded = false;
        }
    }

    void Spawn()
    {
        //Main Screen Text
        GameObject message = Instantiate(_line) as GameObject;
        message.transform.SetParent(_content.transform);
    }


    void RemoveText ()
    {
        int childCount = _line.transform.childCount;
        for (int i = childCount - 1; i >= 0; --i)
        {
            GameObject.Destroy(_line.transform.GetChild (i).gameObject);
        }
    }

    void RemovePresidentMessage()
    {
        int childCount = _baskanMesaj.transform.childCount;
        for (int i = childCount - 1; i >= 0; --i)
        {
            GameObject.Destroy(_baskanMesaj.transform.GetChild (i).gameObject);
        }
    }

    void RemoveMissionMessage()
    {
        int childCount = _missionMessage.transform.childCount;
        for (int i = childCount - 1; i >= 0; --i)
        {
            GameObject.Destroy(_missionMessage.transform.GetChild (i).gameObject);
        }
    }

    void RemoveButtons ()
    {
        int childCount = _choices.transform.childCount;
        for (int i = childCount - 1; i >= 0; --i)
        {
            GameObject.Destroy(_choices.transform.GetChild (i).gameObject);
        }
    }

    public void ChoiceSelected (int id)
    {
        _inkStory.ChooseChoiceIndex (id);
        storyNeeded = true;
    }
}

_inkStory.Continue()函数从故事中提取文本行。 有了这个代码段,我总是得到以时间间隔显示整个段落而不是逐行显示的结果。

以下是摘要目标的摘要; 我实例化了包含文本child和包含某些图标内容的child的“ _line”预制件。 当“ president”值更改时,弹出窗口将处于活动状态,而文本父级将被更改,将值返回默认值后,弹出窗口将关闭,文本的父级将再次成为预制件。 其他值的处理相同。

0 个答案:

没有答案