The dialogue system of the Unity Fungus plugin is easy to use

Fungus is a free open source plug-in for Unity. It can realize the dialogue between players and NPCs without code. For the plug-in Fungus, today I will talk about my views on it and some simple applications and simple code functions. realization.

Here you need to import the Fungus plug-in, and Tools/Fungus will appear after the plug-in is imported

 

The SayDialog template is used, the Menu menu option is used, the Character player NPC is created, and the Flowchart small mushroom is used.

 

Here I use a capsule as the player, and two cubes as NPCs. Here we need to start the dialogue by approaching the white Cube.

 Here I gave the white Cube a spherical collider, and the player can only have a dialogue if he enters the area. I named it NPC1 and the other NPC2.

 

Click with the left mouse button to proceed with the dialogue. The border and font style here use SayDialog to give the template used. I will tell you how to change the format later.

 End the dialogue here, go find NPC2 to have a dialogue, I made a judgment here, if you don't talk to the white Cube, you can't have a dialogue with it. In the process of interacting with this NPC2, we will choose options, which involves the use of Menu,

 

 The task is obtained here, and each option selection will have different results. If you choose option one, you will receive the task. It will prompt that the receiving task is successful. The second option is to reject the task, and the task will not be accepted.

Choose to accept the task: I put the code part on Update and it will keep cyclically choosing

 Declined task:

 Here we start to talk about the principle.

We start by creating Flowchart, which is used to manage conversations. A small mushroom will appear after creation.

 

After clicking the little mushroom, don’t worry about other things, click Open Flowchart Window to enter the dialog management

 

 

 Click on GameStart, edit dialogue

 

 

 The default option here is GameStarted, which starts the dialogue when the game is running. Here we choose None, how to judge the dialogue through code.

Double-click the + sign, and a lot of dialog boxes will pop up. Here we briefly talk about the things we need to use.

 

 Here we have to choose to use the SetSayDialog template

 

 Create a template in Tools/Fungus/Create/SayDialog.

 Create NPC characters in Tools/Fungus/Create/Character, create two, one NPC1, one NPC2.

 

 

 Rename to distinguish between NPC1 and 2

 

 

 

 Create a Plane, and create 2 Cubes on it, and a capsule

 Double-click to open the SayDialog edit dialog template

 

Panel: You can change the chat background

 Image: It is a picture of a big mushroom

Continue: is the picture in the lower right corner

 

 Here we will change the background image of a panel. After editing, we can enter Flowchart, click on SayDialog and entrust it with the just edited SayDialog, so that we have edited the template. The operations below the template will use this template, unless you are editing a template and using it below, then this template will be replaced

 Select the say dialogue, and select the NPC for the dialogue. Here I give both sentences to NPC1

 

 After that, we right-click to create a new dialogue, repeat the previous operation, and give the dialogue to NPC2

 

Then we add judgment conditions to it, click Variables, click the "+" sign, and then select Boolean to modify the name canTalk. Then select Variable/SetVariable

 

 

 Add an if judgment in Second

 

Note: The position of the dialogue NPC cannot be mistaken, because the NPC that uses the Second to talk to cannot perform the dialogue without going through the NPC that uses the Start to talk to! ! ! !

Then we give the NPC code, two NPC (Cube) use the same script, what needs to be modified is the name of ChatName.

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

/*
*创建者:
*创建时间:
*描述:
*版本:
*/
public class NPC : MonoBehaviour
{
    public string ChatName;//外面传入要进行的对话名称
    private bool canTalk=false; //判断当前是否可以判断读取对话
    private Flowchart f;

    void Start()
    {
        f = GameObject.Find("Flowchart").GetComponent<Flowchart>();//获取小蘑菇,对话系统
    }
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.E))//按下键盘E开始对话
        {
            Say();
        }
    }
    //鼠标点击事件,如果鼠标点击NPC也允许读取动画
    private void OnMouseDown()
    {
        Say();
    }
    void Say()
    {
        if (canTalk) 
        {
            if (f.HasBlock(ChatName))//判断对话是否存在
            {
                f.ExecuteBlock(ChatName);//执行对话
            }
        }
    }
    private void OnTriggerEnter(Collider other)//触发器允许对话
    {
            canTalk = true;        
    }
    private void OnTriggerExit(Collider other)
    {
            canTalk = false;        
    }


}

All here to change the name of the conversation, click on the conversation and then modify the BlockName

 

Note: The Cube must add a collider, the player must also add it, and add a rigid body

 

 Give the player a movement code and let him move

 void Update()
    {
        Vector3 v = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
        transform.position += v * 3 * Time.deltaTime;
        Talk();
    }

 

 Here we add two Menu selection branches to the Second dialogue of NPC2, and two selection boxes will pop up.

 

 How do we judge which selection box we have selected here? The operation here is the same as the previous Boolean.

Here I choose a Variable of type Integer and make it public.

 Create two dialogs. Click the Menu to select TargetBlack, select say1 for the first Menu, and select say2 for the second Menu

 

In this way, it has branches, and choosing different options will result in different results.

 

 

 How to judge which button we have selected, the Change of Variables/Integer is used here.

code show as below:

using System.Collections.Generic;
using UnityEngine;
using Fungus;
using UnityEngine.UI;
/*
*创建者:
*创建时间:
*描述:
*版本:
*/
public class Player1 : MonoBehaviour
{
    public Flowchart fc;
    public Menu b;
    

    public void Talk()
    {
        //获取IntegerVariable的值
        if (fc.GetIntegerVariable("Change")==1)
        {
            print("去钓鱼");
        }
        else if(fc.GetIntegerVariable("Change") == 2)
        {
            print("不去钓鱼");
        }
       
    }
 
    void Update()
    {
        Vector3 v = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
        transform.position += v * 3 * Time.deltaTime;
        Talk();
    }

}

 Note: You need to drag the Flowchart object to the code, otherwise an error will be reported.

 select option 1

 select option 2

 

Guess you like

Origin blog.csdn.net/m0_71624363/article/details/129696541