Dotween 文本對話

遊戲中有對話情節,需求是:遊戲暫停 界面所有UI隱藏  npc與玩家對話

這裏複製上項目的涉及到此功能的一小段代碼。

    private List<string> content;//對話內容
    private int tempid = 0; //第幾句話
    public Text txt_Task;  //文本
    
    private void PlayTaskContent(List<string> _content) {
        //界面隱藏
       
        //停止活動
        Time.timeScale = 0;
        //播放對話
        txt_Task.text = "";
        tempid = -1;
        content = _content;
        PlayNextText();
        
    }
    private int timeCount =0;
    private void PlayNextText() {
        Debug.Log("播放一句話");
        tempid++;
        if (tempid< content.Count) {
            txt_Task.text = "";
            float _time = content[tempid].Length / 10;
            Tweener tweener = txt_Task.DOText(content[tempid], _time);
            tweener.SetUpdate(true);
            Tween t = DOTween.To(() => timeCount, a => timeCount = a, 1, 2).OnStepComplete(PlayNextText);
            t.SetUpdate(true);
            //tweener.OnComplete(PlayNextText);
        }
        else {
            Debug.Log("所有的話都播放完畢");
            Panel_Task.SetActive(false);
            Time.timeScale = 1;
            //更新任務
            GlobalDate.taskData.TaskID++;
        }
    }

調用 PlayTaskContent()參數傳入你的對話列表list信息

//在做遊戲暫停的時候通常會使用Time.Scale = 0 
//停止活動   
Time.timeScale = 0;

遊戲暫停 但是dotween 動畫還要播放,所以在DoTween中只需要設置tweener.SetUpdate(true); 即可。意思就是這個Tween是忽略TimeScale,如果不寫的話 tweener.SetUpdate 是 false。

txt_Task.text = "";//文本每次開始新說話的時候要清空 不然DOText打字機的形式會在舊文字的基礎上打字
            float _time = content[tempid].Length / 10;//這裏根據文字的長度設置打字機播放的速度
            Tweener tweener = txt_Task.DOText(content[tempid], _time);
            tweener.SetUpdate(true);//無視遊戲暫停

第一句對話說完。 我原本使用的是 tweener.OnComplete(PlayNextText); dotween播放完的回調事件,但是吧,沒有時間停頓,播放完這句話直接下一句話。字數少的時候一閃而過。

private void PlayNextText() {
        Debug.Log("播放下一句話");
        tempid++;
        if (tempid< content.Count) {
            txt_Task.text = "";
            float _time = content[tempid].Length / 10;
            Tweener tweener = txt_Task.DOText(content[tempid], _time);
            tweener.SetUpdate(true);

            tweener.OnComplete(PlayNextText);
        }
        else {
            Debug.Log("所有的話都播放完畢");
            Time.timeScale = 1;   
        }
    }

所以我想加了一個 延時。因爲遊戲暫停Time.timeScale = 0;Invoke和協程延時都不能使用了,有大佬有辦法的話可以在下面回覆一下我學習一下。所以這裏用dotween.to函數

 private int timeCount =0;
    private void PlayNextText() {
        Debug.Log("播放一句話");
        tempid++;
        if (tempid< content.Count) {
            txt_Task.text = "";
            float _time = content[tempid].Length / 10;
            Tweener tweener = txt_Task.DOText(content[tempid], _time);
            tweener.SetUpdate(true);
            Tween t = DOTween.To(() => timeCount, a => timeCount = a, 1, 2).OnStepComplete(PlayNextText);
            t.SetUpdate(true);
            //tweener.OnComplete(PlayNextText);
        }
        else {
            Debug.Log("所有的話都播放完畢"); 
            Time.timeScale = 1;         
        }
    }

//最後所有的對話完成   遊戲繼續 Time.timeScale = 1;

//細節補充

Time.timeScale變化後會影響誰?

1:“timeScale不會影響Update和LateUpdate的執行速度”

2:“FixedUpdate是根據時間來的,所以timeScale只會影響FixedUpdate的速度”

因爲:update跟當前平臺的幀數有關,而FixedUpdate是真實時間

ime.timeScale影響的是Unity的遊戲時間縮放比例。Unity裏面所有跟時間有關係的東西都是根據timeScale來演算的

具體關於Time.timeScale遊戲暫停的相關信息 可以去看看雨松大佬的:   http://www.xuanyusong.com/archives/2956

話說雨松陪伴了我整個unity的時光,剛開始入門的時候就經常看他的文章,現在依舊能夠有所收穫。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章