Unity Graph View creates a graphical dialogue editing system (2)

Unity Graph View to create a graphical dialogue editing system (2) - data node abstraction

Table of contents

Video Effect Demonstration

Graph View creates a graphical dialogue editing system effect display

The final source code is attached here

Click here to download the source code


We need two data types, one is the dialog tree (DialogTree), the other is the dialog node (DialogNode), and the former is the container of the latter.

DialogTree

The definition of Tree is relatively simple, only a few simple methods need to be implemented

[CreateAssetMenu(menuName = "Dialogue Tree Graph")]
public class DialogTree : ScriptableObject
{
    
    
    [HideInInspector]
    public StartNode startNode = null;

	// .............
}

DialogNode

In order to make the whole system simpler, we only make three kinds of nodes first, one is the node for the player to speak, the other is the node for the NPC to speak, and then add a node for the start of the dialogue. The start node has no preceding node, which is The start of the entire dialogue event is the root of the entire dialogue tree and the basis for triggering the NPC dialogue component.
All three nodes need to have attributes such as location, title, and output options. Among them, there can be multiple output options, which have different meanings for different nodes. For example, for the start node, the output option is the type of event. For example, when you approach an NPC, the NPC will trigger a "player approach" event, and then start the "player approach" branch in the start node in the Tree; for NPC nodes , a single output means what the NPC wants to say, but if you define multiple sentences, it means that the NPC will randomly say one of them; and for the player node, a single sentence means to show ordinary dialogue, and multiple sentences means that at this time The UI should display a menu for the player to make dialogue choices.

public abstract class DialogNodeBase : ScriptableObject
{
    
    
    [HideInInspector]
    public Vector2 position = Vector2.zero;
    [HideInInspector]
    public string id = null;
    public abstract string nodeTitle {
    
     get; }
    public abstract string [] outputItems {
    
     get; set; }

    [Serializable]
    public class LinkChild
    {
    
    
        public string key;
        public DialogNodeBase child;
    };

    [HideInInspector,SerializeField]
    private List<LinkChild> m_children = new List<LinkChild>();
}

The start node, NPC node, and player node all inherit from the above basic nodes.
Among them, the key attribute is the m_children attribute, which stores all the successor nodes of the node and the location of the output interface of the subsequent nodes.
Taking the NPC node as an example, the derivation is as follows:

public class NPCRandomNode: DialogNodeBase
{
    
    
    [SerializeField]
    private string[] randomItems = null;

    public override string nodeTitle
    {
    
    
        get
        {
    
    
            if (outputItems == null || outputItems.Length <= 1)
                return "NPC说";
            else
                return "NPC随机说下列之一";
        }
    }
    public override string[] outputItems {
    
     get {
    
     return randomItems; } set {
    
     randomItems = value; } }
}

Node data is actually very simple. In the future, other nodes can be easily expanded on this basis, such as logical judgment nodes, which can judge that if a task is completed, enter a dialogue branch, and if it is not completed, enter another branch.

Guess you like

Origin blog.csdn.net/sdhexu/article/details/123482388