新学会了一个插件:dialogue,很方便,可以配合各种UI做对话

本文介绍了一款基于Unity平台的对话插件,通过使用节点编辑器和全局变量,开发者可以在不到一小时内实现对话系统的快速搭建与自动化处理。包括对话流程管理、分支选择、按钮绑定及销毁等功能,显著提高了脚本的易用性和开发效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

插件这种东西,最强大之处貌似就在于editor的编写

无论是customEditor,还是editorwindow,都是加快效率,使脚本更加易用的不二法宝啊

这是一个制作对话的插件

基于节点的编辑器

可以带有分支,有全局变量

缺点:编辑器不可以缩放,如果对话过长的话,编辑起来超级费力,需要拖啊拖啊拖啊


流程:执行Initialize初始化,执行continue继续,如果有分支,执行continue(index)的分支

比较重要的事件为onWait onWaitComplete 还有onMessageEvent

最重要的则是onPurchText,接收下一句的所有内容就在这个委托的方法里面



补充一下:Dialoguer.events.ClearAll();这个在每次开始新对话前执行一次很有必要.清空上次的


下面是我写的一个范例脚本,配合NGUI的,有一点补充下,就是continue在有分支的情况下如果不输入索引,则默认进行第一个分支,如果节点没有后记子节点,则重新开始



这个范例还是很高级的,插件的魅力啊,用了不到一个小时就写出来了,实现了自动显示下一句,如果有分支的话,自动生成分支按钮,将分支按钮与分支绑定,最多支持4个分支(你也可以无限加~~),点击分支自动跳转到分支的节点上,分支结束后,还可以自动销毁按钮~~~

如果没有这个插件的话,这个说不定要写多久......光是那个语句分支的模块大概就要好几天

unity果然还是要靠插件的啊!!!!

using UnityEngine;
using System.Collections;
using System.Collections.Generic;


public class TalkTest : MonoBehaviour {

    public UILabel textTitle;
    public UIButton Prefab;
    public UIGrid ButtonsContainer;

    private bool start = false;
    // Use this for initialization
    void Start () 
    {
        Dialoguer.Initialize();
        Dialoguer.events.ClearAll();
        addDialoguerEvents();
        Dialoguer.StartDialogue(3);
        start = true;
    }
    
    // Update is called once per frame
    void Update () {
    
    }

    public void ContinueDialogue()
    {
        if(!start)
        {
            Dialoguer.events.ClearAll();
            addDialoguerEvents();
            Dialoguer.StartDialogue(3);
            start = true;
        }

        Dialoguer.ContinueDialogue();
    }
    public void ClearChoiseButton()
    {
        foreach(Transform oldButton in ButtonsContainer.transform)
        {
            Destroy(oldButton.gameObject);
        }
            ButtonsContainer.Reposition();
        
    }
    
    public void DelegateBind(UIButton button,int index)
    {
        switch(index)
        {
        case 1:
            EventDelegate.Add(button.onClick,ChooseNoOne);
            break;
            
        case 2:
            EventDelegate.Add(button.onClick,ChooseNoTwo);
            
            break;
            
        case 3:
            EventDelegate.Add(button.onClick,ChooseNoThree);
            
            break;
            
        case 4:
            EventDelegate.Add(button.onClick,ChooseNoFour);
            
            break;
            
        default:
            break;
        }
    }
    public void ChooseNoOne()
    {
        Dialoguer.ContinueDialogue(0);
        Debug.Log("Choise One~~~~");
        ClearChoiseButton();
    }
    public void ChooseNoTwo()
    {
        Dialoguer.ContinueDialogue(1);
        Debug.Log("Choise 2222~~~~");
        
        ClearChoiseButton();
    }
    public void ChooseNoThree()
    {
        Dialoguer.ContinueDialogue(2);
        Debug.Log("Choise three~");
        
        ClearChoiseButton();
    }
    public void ChooseNoFour()
    {
        Dialoguer.ContinueDialogue(3);
        Debug.Log("Choise Four4444~~~~");
        
        ClearChoiseButton();
    }


    public void addDialoguerEvents()
    {

        Dialoguer.events.onStarted += onDialogueStartedHandler;

        Dialoguer.events.onEnded += onDialogueEndedHandler;

        Dialoguer.events.onTextPhase += onDialogueTextPhaseHandler;

        Dialoguer.events.onWindowClose += onDialogueWindowCloseHandler;

        Dialoguer.events.onInstantlyEnded += onDialogueInstantlyEndedHandler;

        Dialoguer.events.onMessageEvent += onDialoguerMessageEvent;

        Dialoguer.events.onWaitStart += onWaitStart;
        Dialoguer.events.onWaitComplete += onWaitComplete;
        
    }


    void onDialogueStartedHandler ()
    {
        Debug.Log(" Started ");
    }

    void onDialogueEndedHandler ()
    {
        start = false;
        Debug.Log("ended");
        
    }

    void onDialogueInstantlyEndedHandler ()
    {
        Debug.Log("Instantly");
        
    }

    void onDialogueTextPhaseHandler (DialoguerTextData data)
    {
        if( (data.choices != null )&&  data.choices.Length > 0 )
        {
            textTitle.text = data.text;

            for(int i = 0; i <  data.choices.Length ; i++)
            {
                UIButton button = GameObject.Instantiate(Prefab.GetComponent<UIButton>()) as UIButton;
                //button.gameObject.SetActive(true);
                button.transform.parent = ButtonsContainer.transform;
                button.transform.localScale = Vector3.one;
                DelegateBind(button,i +1);

                UILabel info =  button.GetComponentInChildren<UILabel>();
                info.text =  data.choices[i];


            }
            ButtonsContainer.Reposition();
            return;
        }


        textTitle.text = data.text;
        
    }

    void onDialogueWindowCloseHandler ()
    {
        Debug.Log("WIndow Close");
        
    }

    void onDialoguerMessageEvent (string message, string metadata)
    {
        Debug.Log("Message  ");
        
    }

    void onWaitStart ()
    {
        Debug.Log("onWaitStart  ");
    }

    void onWaitComplete ()
    {
        Debug.Log("onWaitComplete  ");
    }
}



Dialoguer.cs
Dialoguer.cs contains all the methods and events needed to use Dialoguer.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#region Initialization
// Call this in order to initialize the Dialoguer system.
void  Dialoguer.Initialize();
#endregion
 
#region Dialogues
// Start a Dialogue, with an ID or an Enum, with optional callback
void  Dialoguer.StartDialogue(DialoguerDialogues dialogue);
void  Dialoguer.StartDialogue(DialoguerDialogues dialogue, DialoguerCallback callback);
void  Dialoguer.StartDialogue( int  dialogueId);
void  Dialoguer.StartDialogue( int  dialogueId, DialoguerCallback callback);
         
// Continue the current dialogue after a Regular or Branched Text node
void  Dialoguer.ContinueDialogue( int  choice);
void  Dialoguer.ContinueDialogue();
 
// Ends the current dialogue
void  Dialoguer.EndDialogue();
#endregion
 
#region Global Variable Setters
// These methods set the current value of the specified Global Variable
void  Dialoguer.SetGlobalBoolean( int  booleanId,  bool  booleanValue);
void  Dialoguer.SetGlobalFloat( int  floatId,  float  floatValue);
void  Dialoguer.SetGlobalString( int  stringId,  string  stringValue);
#endregion
 
#region Global Variable Getters
// These methods get the current value from Dialoguer's Global Variable system
bool  Dialoguer.GetGlobalBoolean( int  booleanId);
float  Dialoguer.GetGlobalFloat( int  floatId);
string  Dialoguer.GetGlobalString( int  stringId);
#endregion
 
#region Global Variable Saving and Loading
// Gets Dialoguer's GlobalVariablesState, which can be saved as an XML string for future use.
string  Dialoguer.GetGlobalVariablesState();
     
// Sets Dialoguer's GlobalVariablesState. To be used in combination with the string previously saved with GetGlobalVariablesState
void  Dialoguer.SetGlobalVariablesState( string  globalVariablesXml);
#endregion
 
#region Events
// Dialoguer's events, dispatched by the Dialogue system
DialoguerEvents Dialoguer.events;
#endregion
DialoguerEvents : Events
DialoguerEvents is an object available at  Dialoguer.events which contains all the events Dialoguer has to listen to.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Clear all events currently registered with Dialoguer
void  Dialoguer.ClearAll();
 
// Occurs when Dialoguer starts a dialogue.
event  StartedHandler onStarted;
delegate  void  StartedHandler();
 
// Occurs when Dialoguer ends a dialogue.
event  EndedHandler onEnded;
delegate  void  EndedHandler();
 
// Occurs when Dialoguer suddenly ends a dialogue.
event  SuddenlyEndedHandler onInstantlyEnded;
delegate  void  SuddenlyEndedHandler();
 
// Occurs when Dialoguer enters a TextPhase in a dialogue.
event  TextPhaseHandler onTextPhase;
delegate  void  TextPhaseHandler(DialoguerTextData data);
 
// Occurs when Dialoguer calls for the text window to close.
event  WindowCloseHandler onWindowClose;
delegate  void  WindowCloseHandler();
 
// Occurs when Dialoguer calls for a wait with the Wait node.
event  WaitStartHandler onWaitStart;
delegate  void  WaitStartHandler();
 
// Occurs when Dialoguer finishes a wait with the Wait.
event  WaitCompleteHandler onWaitComplete;
delegate  void  WaitCompleteHandler();
 
// Occurs when Dialoguer sends a message with the SendMessage node.
event  MessageEventHandler onMessageEvent;
delegate  void  MessageEventHandler( string  message,  string  metadata);
DialoguerTextData : Object
DialoguerTextData is an object type that is passed to every Dialoguer.onTextPhase event listener. It contains vital information about the current TextPhase being served by Dialoguer.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// The formatted text, with in-line variables
string  text;
 
// The raw, unformatted text
string  rawText;
 
// The theme name
string  theme;
 
// Whether or not the newWindow field has been checked
bool  newWindow;
 
// The name field
string  name;
 
// The portrait field
string  portrait;
 
// The metadata field
string  metadata;
 
// The audio field
string  audio;
 
// The audio delay field   
float  audioDelay;
 
// The position rect field
Rect rect;
 
// Whether or not the rect field was used for this node
public  bool  usingPositionRect;
 
// The branched-text node's choices
string [] choices;
 
// The type of TextPhase belonging to the current node
public  DialoguerTextPhaseType windowType;
DialoguerCallback : Delegate
DialoguerCallback is a generic delegate used in the StartDialogue methods as a callback
1
public  delegate  void  DialoguerCallback();
DialoguerDialogues : Enum
DialoguerDialogues is an enum that is generated by selecting Dialoguer > Generate Dialogues Enum in the Unity menu bar. This will generate an enum named DialoguerDialogues containing all the different names of the Dialogues you have created.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值