Somewhere over the Lecture I lost node connections (just visual in editor there is no draw line) to all node, that has IsPlayerSpeaking true. I can’t find where the mistake is. Can you help, please.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
using System;
namespace RPG.Dialogue.Editor
{
public class DialogueEditor : EditorWindow
{
Dialogue selectedDialogue = null; //EditorWindow serilize all properties on top, we have to unserilized
[NonSerialized]
GUIStyle nodeStyle;
[NonSerialized]
GUIStyle playerNodeStyle;
[NonSerialized]
DialogueNode draggingNode = null;
[NonSerialized]
Vector2 draggingOffset;
[NonSerialized]
DialogueNode creatingNode = null;
[NonSerialized]
DialogueNode deletingNode = null;
[NonSerialized]
DialogueNode linkingParentNode = null;
Vector2 scrollPosition;
[NonSerialized]
bool draggingCanvas = false;
[NonSerialized]
Vector2 draggingCanvasOffset;
const float canvasSize = 4000;
const float backgroundSize = 50;
[MenuItem("Window/Dialogue Editor")]
public static void ShowEditorWindow()
{
GetWindow(typeof(DialogueEditor), false, "Dialogue Editor");
}
[OnOpenAsset(1)]
public static bool OnOpenAsset(int instanceID, int line)
{
Dialogue dialogue = EditorUtility.InstanceIDToObject(instanceID) as Dialogue;
if (dialogue != null)
{
ShowEditorWindow();
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(10,10, 10, 10);
playerNodeStyle = new GUIStyle();
playerNodeStyle.normal.background = EditorGUIUtility.Load("node1") as Texture2D;
playerNodeStyle.normal.textColor = Color.white;
playerNodeStyle.padding = new RectOffset(20, 20, 20, 20);
playerNodeStyle.border = new RectOffset(10, 10, 10, 10);
}
private void OnSelectionChanged()
{
Dialogue newdialogue = Selection.activeObject as Dialogue;
if (newdialogue != null)
{
selectedDialogue = newdialogue;
Repaint();
}
}
private void OnGUI()
{
if(selectedDialogue == null)
{
EditorGUILayout.LabelField("No dialogue selected. ");
}
else
{
ProcessEvents();
scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
Rect canvas = GUILayoutUtility.GetRect(canvasSize, canvasSize);
Texture2D backgroundTex = Resources.Load("background") as Texture2D;
Rect texCoords = new Rect(0, 0, canvasSize / backgroundSize, canvasSize / backgroundSize);
GUI.DrawTextureWithTexCoords(canvas, backgroundTex, texCoords);
foreach(DialogueNode node in selectedDialogue.GetAllNodes())
{
DrawConnections(node);
}
foreach (DialogueNode node in selectedDialogue.GetAllNodes())
{
DrawNode(node);
}
EditorGUILayout.EndScrollView();
if (creatingNode != null)
{
//Undo.RecordObject(selectedDialogue, "Added Dialogue Node");
selectedDialogue.CreateNode(creatingNode);
creatingNode = null;
}
if (deletingNode != null)
{
selectedDialogue.DeleteNode(deletingNode);
deletingNode = null;
}
}
}
private void ProcessEvents()
{
if (Event.current.type == EventType.MouseDown && draggingNode == null)
{
draggingNode = GetNodeAtPoint(Event.current.mousePosition + scrollPosition);
if (draggingNode != null )
{
draggingOffset = draggingNode.GetRect().position - Event.current.mousePosition;
Selection.activeObject = draggingNode;
}
else
{
draggingCanvas = true;
draggingCanvasOffset = Event.current.mousePosition + scrollPosition;
Selection.activeObject = selectedDialogue;
}
}
else if (Event.current.type == EventType.MouseDrag && draggingNode != null)
{
draggingNode.SetPosition(Event.current.mousePosition + draggingOffset);
GUI.changed = true;
}
else if (Event.current.type == EventType.MouseDrag && draggingCanvas)
{
scrollPosition = draggingCanvasOffset - Event.current.mousePosition;
GUI.changed = true;
}
else if (Event.current.type == EventType.MouseUp && draggingNode != null)
{
draggingNode = null;
}
else if (Event.current.type == EventType.MouseUp && draggingCanvas)
{
draggingCanvas = false;
}
}
private void DrawNode(DialogueNode node)
{
GUIStyle style = nodeStyle;
if (node.IsPlayerSpeaking())
{
style = playerNodeStyle;
}
GUILayout.BeginArea(node.GetRect(), style);
//EditorGUI.BeginChangeCheck();
node.SetText(EditorGUILayout.TextField(node.GetText()));
GUILayout.BeginHorizontal();
if (GUILayout.Button("x"))
{
deletingNode = node;
}
DrawLinkButtons(node);
if (GUILayout.Button("+"))
{
creatingNode = node;
}
GUILayout.EndHorizontal();
GUILayout.EndArea();
}
private void DrawLinkButtons(DialogueNode node)
{
if (linkingParentNode == null)
{
if (GUILayout.Button("link"))
{
linkingParentNode = node;
}
}
else if (linkingParentNode == node)
{
if (GUILayout.Button("cancel"))
{
linkingParentNode = null;
}
}
else if (linkingParentNode.GetChildren().Contains(node.name))
{
if (GUILayout.Button("unlink"))
{
linkingParentNode.RemoveChild(node.name);
linkingParentNode = null;
}
}
else
{
if (GUILayout.Button("child"))
{
linkingParentNode.AddChild(node.name);
linkingParentNode = null;
}
}
}
private void DrawConnections(DialogueNode node)
{
Vector3 startPosition = new Vector2(node.GetRect().xMax, node.GetRect().center.y);
foreach (DialogueNode childNode in selectedDialogue.GetAIChildern(node))
{
Vector3 endPosition = new Vector2 (childNode.GetRect().xMin, childNode.GetRect().center.y);
Vector3 controlPointOffset = endPosition - startPosition;
controlPointOffset.y = 0f;
controlPointOffset.x *= 0.8f;
Handles.DrawBezier(
startPosition, endPosition,
startPosition + controlPointOffset,
endPosition - controlPointOffset,
Color.white, null, 4f);
}
}
private DialogueNode GetNodeAtPoint(Vector2 point)
{
DialogueNode foundNode = null;
foreach (DialogueNode node in selectedDialogue.GetAllNodes())
{
if (node.GetRect().Contains(point))
{
foundNode = node;
}
}
return foundNode;
}
}
}