1. 程式人生 > 其它 >unity2d開發——對話方塊實現

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;
}