Как отображать каждую строку текста (чернила) с временными интервалами в Unity? - PullRequest
0 голосов
/ 05 июля 2019

Я работаю над текстовой интерактивной игрой и использую плагин 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() функция извлекает текстовые строки из истории.С помощью этого фрагмента я всегда получаю результат, показывающий с интервалом времени весь проход, но не построчно.

Вот краткое изложение цели фрагмента;Я создаю экземпляр префаба "_line", который включает в себя текстовый дочерний элемент и дочерний элемент, включающий некоторые значки.Когда значение «президент» изменяется, всплывающее окно будет активным, и родительский текст будет изменен, после возврата значения по умолчанию всплывающее окно закрывается, и родительский текст снова становится префабом.Тот же процесс для других значений.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...