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

插件这种东西,最强大之处貌似就在于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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,让我来为您介绍如何使用Lua和Urho3D编辑器的UI系统来实现简单的对话功能。 首先,我们需要创建一个UI元素,用于显示对话框。在Urho3D编辑器中,可以通过在场景中添加一个UI元素节点来实现这一点。在节点属性面板中,可以将该节点的“UI元素”属性设置为“Window”,并将其命名为“dialogueWindow”。 接下来,我们需要编写Lua脚本来处理对话逻辑和更UI元素。在Urho3D编辑器中,可以通过在场景中添加一个节点并将其脚本属性设置为我们刚刚创建的脚本来实现这一点。让我们将其命名为“dialogueScript”。 以下是一个示例脚本,用于实现简单的对话功能: ```lua -- 获取UI元素 local dialogueWindow = ui.root:GetChild("dialogueWindow", true) -- 对话内容 local dialogue = { {speaker = "NPC", text = "你好,欢迎来到游戏!"}, {speaker = "玩家", text = "谢谢!"}, {speaker = "NPC", text = "你想开始游戏吗?"}, {speaker = "玩家", text = "是的,请告诉我如何开始。"}, {speaker = "NPC", text = "你需要按下空格键开始游戏。"} } -- 对话索引 local index = 1 -- 更UI元素 local function UpdateDialogue() -- 获取对话框文本和说话者标签 local speakerLabel = dialogueWindow:GetChild("speakerLabel", true) local textLabel = dialogueWindow:GetChild("textLabel", true) -- 获取当前对话 local currentDialogue = dialogue[index] -- 更标签内容 speakerLabel.text = currentDialogue.speaker textLabel.text = currentDialogue.text end -- 处理空格键按下事件 function HandleKeyDown(eventType, eventData) -- 获取按键代码 local key = eventData:GetInt("Key") -- 如果按下的是空格键 if key == KEY_SPACE then -- 更对话索引 index = index + 1 -- 如果对话已经结束,关闭对话框 if index > #dialogue then dialogueWindow.visible = false return end -- 更UI元素 UpdateDialogue() end end -- 注册空格键按下事件处理函数 SubscribeToEvent("KeyDown", "HandleKeyDown") -- 显示对话dialogueWindow.visible = true -- 更UI元素 UpdateDialogue() ``` 在上面的代码中,我们首先获取了名为“dialogueWindow”的UI元素。然后,我们定义了一个包含对话内容的表,以及一个表示当前对话索引的变量。我们还定义了一个名为“UpdateDialogue”的函数,用于更UI元素中的标签内容。最后,我们注册了一个名为“HandleKeyDown”的函数,用于处理空格键按下事件,并在脚本启动时显示对话

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值