After Lecture 2-12 [Styling Nodes],
I first received a null reference error for which I performed a check in Dialogue.cs.
Like:
using UnityEngine;
using System.Collections.Generic;
namespace RPG.Dialogue
{
[CreateAssetMenu(fileName = "New Dialogue", menuName = "Dialogue", order = 0)]
public class Dialogue : ScriptableObject
{
[SerializeField] List<DialogueNode> nodes;
#if UNITY_EDITOR
private void Awake()
{
if(nodes != null)
{
if (nodes.Count == 0)
{
nodes.Add(new DialogueNode());
}
}
}
#endif
public IEnumerable<DialogueNode> GetAllNodes()
{
return nodes;
}
}
}
I don’t have the error anymore, but the editor does not show, no matter where and how I summon it.
I even copied the scripts from repo:
DialogueNode.cs
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace RPG.Dialogue
{
[System.Serializable]
public class DialogueNode
{
public String ID;
public String text;
public Rect rect;
public string[] childrenNodeID;
}
}
and DialogueEditor.cs
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEngine;
namespace RPG.Dialogue.Editor
{
public class DialogueEditor : EditorWindow
{
Dialogue selectedDialogue = null;
GUIStyle nodeStyle;
[MenuItem("Window/Dialogue Editor")]
private static void ShowWindow()
{
GetWindow(typeof(DialogueEditor), false, "Dialogue Editor");
}
[OnOpenAsset(1)]
public static bool OnAssetOpen(int instanceID, int line)
{
Dialogue dialogue = EditorUtility.InstanceIDToObject(instanceID) as Dialogue;
if(dialogue != null)
{
ShowWindow();
return true;
}
return false;
}
private void OnEnable()
{
Selection.selectionChanged += OnSelectionChanged;
nodeStyle = new GUIStyle();
nodeStyle.normal.background = EditorGUIUtility.Load("node0") as Texture2D;
nodeStyle.normal.textColor = Color.white;
nodeStyle.padding = new RectOffset(20, 20, 20, 20);
nodeStyle.border = new RectOffset(12, 12, 12, 12);
}
private void OnSelectionChanged()
{
Dialogue dialogue = Selection.activeObject as Dialogue;
if (dialogue != null)
{
selectedDialogue = dialogue;
Repaint();
}
}
private void OnGUI()
{
if(selectedDialogue == null)
{
EditorGUILayout.LabelField("No Dialogue Selected !");
}
else
{
foreach(DialogueNode node in selectedDialogue.GetAllNodes())
{
OnGUINode(node);
}
}
}
private void OnGUINode(DialogueNode node)
{
GUILayout.BeginArea(node.rect, nodeStyle);
EditorGUI.BeginChangeCheck();
EditorGUILayout.LabelField("Node", EditorStyles.whiteLabel);
string newID = EditorGUILayout.TextField(node.ID);
string newText = EditorGUILayout.TextField(node.text);
if (EditorGUI.EndChangeCheck())
{
Undo.RecordObject(selectedDialogue, "Update Dialogue Data");
node.ID = newID;
node.text = newText;
}
GUILayout.EndArea();
}
}
}
I need some help please…