Enum for Speakers

I saw the post on enum for speakers and I just wanted to share my approach to see if it was ideal or not.

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

namespace RPG.Dialogue
{
    public class DialogueNode : ScriptableObject
    {
        [SerializeField] Speaker speaker;
        [SerializeField] string text;
        [SerializeField] List<string> children = new List<string>();
        [SerializeField] Rect rect = new Rect(0, 0, 200, 100);

        public Rect GetRect()
        {
            return rect;
        }

        public string GetText()
        {
            return text;
        }

        public List<string> GetChildren()
        {
            return children;
        }

        public Speaker GetSpeaker()
        {
            return speaker;
        }

#if UNITY_EDITOR
        public void SetPosition(Vector2 newPosition)
        {   
            Undo.RecordObject(this, "Move Dialogue Node");
            rect.position = newPosition;
            EditorUtility.SetDirty(this);
        }

        public void SetText(string newText)
        {
            if (newText != text)
            {
                Undo.RecordObject(this, "Update Dialogue Text");
                text = newText;
                EditorUtility.SetDirty(this);
            }
        }

        public void AddChild(string childID)
        {
            Undo.RecordObject(this, "Add Dialogue Link");
            children.Add(childID);
            EditorUtility.SetDirty(this);
        }

        public void RemoveChild(string childID)
        {
            Undo.RecordObject(this, "Remove Dialogue Link");
            children.Remove(childID);
            EditorUtility.SetDirty(this);
        }

        public void SetSpeaker(Speaker newCurrentSpeaker)
        {
            Undo.RecordObject(this, "Change Dialogue Speaker");
            speaker = newCurrentSpeaker;
            EditorUtility.SetDirty(this);
        }
#endif
    }
}

And then if DialogueEditor.cs

 private void DrawNode(DialogueNode node)
        {
            GUIStyle style = nodeStyle;
            if (node.GetSpeaker() == Speaker.Player)
            {
                style = playerNodeStyle;
            }

            GUILayout.BeginArea(node.GetRect(), style);

            node.SetText(EditorGUILayout.TextField(node.GetText()));

            GUILayout.BeginHorizontal();
           
            if (GUILayout.Button("+"))
            {
                nodeToCreate = node;
            }
            DrawLinkButtons(node);
            if (GUILayout.Button("-"))
            {
                nodeToDelete = node;
            }
            GUILayout.EndHorizontal();

            GUILayout.EndArea();
        }

I currently only display 2 colors for nodes but am planning to add different colors for each enum. I then set the enum in the inspector when the node is created. Is this approach ideal or could it be done another way? I saw in another post that Brian said to have a list of enums and return the speaker through that but I didnt really understand that approach.

With two choices, this approach is fine. As you start to scale up with multiple enum values, you may wish to construct a Dictionary to lookup the values in OnEnable, and then assign the style from the Dictionary.

Privacy & Terms