unity2d游戲開發——對話框實現


簡介

實現一個類似於pokemon的對話框

功能描述

  1. 對話按照次序依次顯示,而不是立刻顯示
  2. 點擊確認或取消鍵立刻顯示完整對話
  3. 顯示完整對話后,再次點擊確認或取消鍵,顯示下一行對話
  4. 全部顯示后,退出對話

設計思路

首先,因為再pokemon里,進入對話框后是沒法做其他操作的,而且全局只有一個,所以這里用單例模式。
為了設計的組件化,博主把對話界面設計成prefab,然后在對話時再實例化

為了按照次序顯示對話,用列表存儲對話內容

點擊查看代碼
public class DialogData
{
    public static DialogData instance = null;  //對話框單例

    List<string> allShowTexts = new List<string>();  //需要顯示的所有文本
    public delegate void OnCloseDialog();
    public OnCloseDialog onCloseDialog = null;

    public static DialogData GetInstance()
    {
        if (instance == null)
        {
            instance = new DialogData();
        }
        return instance;
    }

    public static List<string> GetTextList()
    {
        return GetInstance().allShowTexts;
    }

    public static void SetTextList(List<string> textList)
    {
        GetInstance().allShowTexts = textList;
    }

    public static void CloseDialog()
    {
        if (GetInstance().onCloseDialog != null)
        {
            GetInstance().onCloseDialog();
        }
        GetInstance().ClearData();
    }

    public void ClearData()
    {
        GetInstance().allShowTexts = new List<string>();
        onCloseDialog = null;
    }
}

然后在創建和關閉的時候,需要截獲和放開按鍵操作(這里的按鍵操作是博主自己實現的控制,因為比較簡單就不放出了)

private void OnDisable()
{
    SystemControl.SetBanMove(false);
    LineShowFinish();  //當腳本在失活的時候,將數據進行重置
    DialogData.CloseDialog();
}

private void OnEnable()
{
    SystemControl.SetBanMove(true);
}

按次序顯示用的是定時器,即在update中不斷刷新,計算是否需要顯示下一個字符。
為了方便判斷【是否完整顯示改行】,博主單獨用一個函數封裝了完整顯示的操作(雖然這樣會導致drawcall增加-_-||)

void OnStartWriter()
{
    if (isActive)
    {
        timer += Time.deltaTime;
        if (timer >= charsPerSecond)//判斷計時器時間是否到達
        {
            lineShowFinall = false;
            timer = 0;
            currentPos++;  //這里其實還可以做一個改良,可以檢測一個input用戶輸入,如果輸入了,則讓currentPos = words.Length,這樣可以實現按下按鍵,馬上就顯示完畢          
            myText.text = nowShowText.Substring(0, currentPos);  //刷新文本顯示內容

            if (currentPos >= nowShowText.Length)
            {
                lineShowFinall = true;
                LineShowFinish();
            }
        }
    }
}

void LineShowFinish()
{
    isActive = false;
    myText.text = nowShowText;
}

最后,就是顯示下一行。這里就很簡單了,將下一行的數據填充到prefab里,如果沒有下一行銷毀自身即可

光標實現

在pokemon中,右下角會顯示一個彈跳的光標。這里我們用正弦函數控制位置即可

void OnIconJump()
{
    nowPos.y = Mathf.Abs(Mathf.Sin(Time.fixedTime * Mathf.PI * HZ)) * zhenFu + startPos.y;
    nextIcon.transform.position = nowPos;
}

完整代碼

Tip.cs(綁定在上面的prefab)

點擊查看代碼
public class Dialog : MonoBehaviour
{
    float charsPerSecond = 0.05f;  //打字時間間隔
    string nowShowText;  //現在顯示的文字
    List<string> allShowTexts = new List<string>();  //需要顯示的所有文本
    int nowShowIdx = -1;  //當前顯示到行數
    bool showAllFinall = false;  // 所有文本顯示完畢

    bool isActive = false; //判斷是否開始輸出
    float timer;  //計時器
    Text myText;  //獲取身上的test腳本
    int currentPos = 0;  //當前打字位置
    bool lineShowFinall = false;  // 是否顯示完當前語句
    
    GameObject nextIcon;  // 下一條的箭頭
    Vector3 startPos;  //記錄原位置
    Vector2 nowPos;  //簡寫運動變化的位置
    float zhenFu = 0.2f;  //振幅
    float HZ = 1f;  //頻率

    private void OnDisable()
    {
        SystemControl.SetBanMove(false);
        LineShowFinish();  //當腳本在失活的時候,將數據進行重置
        DialogData.CloseDialog();
    }

    private void OnEnable()
    {
        SystemControl.SetBanMove(true);
    }

    void Start()
    {
        Debug.Log("start dialogs");
        allShowTexts = DialogData.GetTextList();
        if (allShowTexts.Count <= 0)
        {
            showAllFinall = true;
            GameObject.Destroy(gameObject, 0);
        }

        timer = 0;
        isActive = true;
        charsPerSecond = Mathf.Max(0.02f, charsPerSecond);  //將最小的出字速度限制為0.02,也可以自行調整
        myText = GameObject.Find("Dialog").GetComponent<Text>();
        NextLineShowStart();  //開始顯示第一行

        nextIcon = GameObject.Find("Next");
        startPos = nextIcon.transform.position;
        nowPos = startPos;
    }

    void Update()
    {
        //點擊確認或者取消鍵,馬上完整顯示
        if (SystemControl.Confirm() || SystemControl.Cancel())
        {
            if (!lineShowFinall)
            {
                lineShowFinall = true;
                LineShowFinish();
            }
            else
            {
                NextLineShowStart();
            }
        }
        OnStartWriter();
        OnIconJump();
    }

    void OnStartWriter()
    {
        if (isActive)
        {
            timer += Time.deltaTime;
            if (timer >= charsPerSecond)//判斷計時器時間是否到達
            {
                lineShowFinall = false;
                timer = 0;
                currentPos++;  //這里其實還可以做一個改良,可以檢測一個input用戶輸入,如果輸入了,則讓currentPos = words.Length,這樣可以實現按下按鍵,馬上就顯示完畢          
                myText.text = nowShowText.Substring(0, currentPos);  //刷新文本顯示內容

                if (currentPos >= nowShowText.Length)
                {
                    lineShowFinall = true;
                    LineShowFinish();
                }
            }
        }
    }

    void LineShowFinish()
    {
        isActive = false;
        myText.text = nowShowText;
    }
    
    void NextLineShowStart()
    {
        timer = 0;
        currentPos = 0;
        nowShowIdx++;
        if (nowShowIdx < allShowTexts.Count)
        {
            showAllFinall = false;
            lineShowFinall = false;
            nowShowText = allShowTexts[nowShowIdx];
            isActive = true;
            myText.text = "";
        }
        else
        {
            showAllFinall = true;
            Debug.Log("close dialogs");
            GameObject.Destroy(gameObject, 0);
            return;
        }
        //Debug.Log("nowShowIdx: " + nowShowIdx + ", showAllFinall: " + showAllFinall);
    }

    /// <summary>
    /// 箭頭跳動
    /// </summary>
    void OnIconJump()
    {
        nowPos.y = Mathf.Abs(Mathf.Sin(Time.fixedTime * Mathf.PI * HZ)) * zhenFu + startPos.y;
        nextIcon.transform.position = nowPos;
    }
}

public class DialogData
{
    public static DialogData instance = null;  //對話框單例

    List<string> allShowTexts = new List<string>();  //需要顯示的所有文本
    public delegate void OnCloseDialog();
    public OnCloseDialog onCloseDialog = null;

    public static DialogData GetInstance()
    {
        if (instance == null)
        {
            instance = new DialogData();
        }
        return instance;
    }

    public static List<string> GetTextList()
    {
        return GetInstance().allShowTexts;
    }

    public static void SetTextList(List<string> textList)
    {
        GetInstance().allShowTexts = textList;
    }

    public static void CloseDialog()
    {
        if (GetInstance().onCloseDialog != null)
        {
            GetInstance().onCloseDialog();
        }
        GetInstance().ClearData();
    }

    public void ClearData()
    {
        GetInstance().allShowTexts = new List<string>();
        onCloseDialog = null;
    }
}

如何調用

void CreateDeveloperTips()
{
    List<string> allShowTexts = new List<string>();
    allShowTexts.Add("您好!歡迎來到aquam的游戲開發世界。");
    allShowTexts.Add("下面介紹該游戲的操作方式。鍵盤的方向鍵控制人物的移動。");
    allShowTexts.Add("Z鍵確認, X鍵取消。");
    allShowTexts.Add("祝您游戲愉快!");
    DialogData.SetTextList(allShowTexts);
    DialogData.GetInstance().onCloseDialog = CloseDialogCB;
    Instantiate(prefab, dialogPos, Quaternion.Euler(0, 0, 0));
    isOpenTip = true;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM