이제 룸 노드 그래프에서 룸 노드들을 생성했을때 룸 노드끼리 연결선이 연결되서 실제로 우리가 원하는 그래프의 형태를 띠도록 스크립트를 작성해보자.
노드를 생성하고 우클릭으로 드래그해서 다른 노드와 연결시킨다. 연결된 노드끼리는 부모 자식 관계가 형성되도록 한다. 부모 자식 관계는 화살표로 표현해서 부모로부터 자식으로 화살표가 향하도록 한다. 실제로 인 게임에서는 해당 방들이 실제로 연결될 것이다.
저번에 했던 스크립트 코딩 과정과 동일하게 이벤트 처리에서 우클릭 이벤트 처리 부분을 구현해서 해당 부분에 선이 연결되는 로직을 추가하자.
이때 선을 그릴떄 화살표 방향으로 부모-자식 관계를 표현하기로 했는데 화살표는 다음과 같은 방법으로 그렸다.
위와 같이 startPosition과 endPosition의 좌표만 알고 있다면 적절한 상수를 곱해서 화살표를 그릴 수 있다. (화살표 사이즈 : conntingLineArrowSize)
코드가 길어서 코드는 후첨하고 실제로 연결선이 구현이 어떻게 되었는지 살펴보자.
기존과 동일하게 노드 생성은 정상적으로 수행된다. 이 노드를 우클릭을 한 상태로 드래그 하면 마우스를 따라서 선이 연결된다.
이 때 노드가 아닌 즉, rect가 아닌 곳에서 마우스 우클릭을 해제하면 선이 삭제된다. 하지만 노드가 있는 곳에서 우클릭을 때면 선이 연결되면서 화살표가 그려진다. 이 때 출발한 노드를 부모 노드, 마우스를 땐 노드를 자식 노드로 저장하고 화살표 방향은 부모 노드에서 자식 노드로 향한다.
현재는 여러개의 노드가 여러개의 부모-자식 관계를 가지고 있지만 추후에 유효성 검사를 추가해서 한 부모만을 가지도록 설정할 예정이다. (여러 개의 자식은 상관이 없다.)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "RoomNodeGraph", menuName = "Scriptable Objects/Dungeon/Room Node Graph")]
public class RoomNodeGraphSO : ScriptableObject
{
[HideInInspector] public RoomNodeTypeListSO roomNodeTypeList;
[HideInInspector] public List<RoomNodeSO> roomNodeList = new List<RoomNodeSO>();
[HideInInspector] public Dictionary<string, RoomNodeSO> roomNodeDictionary = new Dictionary<string, RoomNodeSO>();
private void Awake()
{
LoadRoomNodeDictionary();
}
/// <summary>
/// Load the room node dictionary from the room node list
/// </summary>
private void LoadRoomNodeDictionary()
{
roomNodeDictionary.Clear();
// Populate dictionary
foreach (RoomNodeSO node in roomNodeList)
{
roomNodeDictionary[node.id] = node;
}
}
#region Editor Code
//The following code should only run in the Unity Editor
#if UNITY_EDITOR
[HideInInspector] public RoomNodeSO roomNodeToDrawLineFrom = null;
[HideInInspector] public Vector2 linePosition;
// Repopulate node dictionary every time a change is made in the editor
public void OnValidate()
{
LoadRoomNodeDictionary();
}
public void SetNodeToDrawConnectionLineFrom(RoomNodeSO node, Vector2 position)
{
roomNodeToDrawLineFrom = node;
linePosition = position;
}
#endif
#endregion
}
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices.WindowsRuntime;
using UnityEditor;
using UnityEngine;
public class RoomNodeSO : ScriptableObject
{
[HideInInspector] public string id;
[HideInInspector] public List<string> parentRoomNodeIDList = new List<string>();
[HideInInspector] public List<string> childRoomNodeIDList = new List<string>();
[HideInInspector] public RoomNodeGraphSO roomNodeGraph;
public RoomNodeTypeSO roomNodeType;
[HideInInspector] public RoomNodeTypeListSO roomNodeTypeList;
#region Editor Code
// the following code should only be run in the Unity Editor
#if UNITY_EDITOR
[HideInInspector] public Rect rect;
[HideInInspector] public bool isLeftClickDragging = false;
[HideInInspector] public bool isSelected = false;
/// <summary>
/// Initialise node
/// </summary>
public void Initialise(Rect rect, RoomNodeGraphSO nodeGraph, RoomNodeTypeSO roomNodeType)
{
this.rect = rect;
this.id = Guid.NewGuid().ToString();
this.name = "RoomNode";
this.roomNodeGraph = nodeGraph;
this.roomNodeType = roomNodeType;
// Load room node type list
roomNodeTypeList = GameResources.Instance.roomNodeTypeList;
}
public void Draw(GUIStyle nodeStyle)
{
// Draw Node Box Using Begin Area
GUILayout.BeginArea(rect, nodeStyle);
// Start Region To Detect Popup Selection Changes
EditorGUI.BeginChangeCheck();
// Display a popup using the RoomNodeType name values that can be selected from (default to the currently set roomNodeType)
int selected = roomNodeTypeList.list.FindIndex(x => x == roomNodeType);
int selection = EditorGUILayout.Popup("", selected, GetRoomNodeTypeToDisplay());
roomNodeType = roomNodeTypeList.list[selection];
if (EditorGUI.EndChangeCheck())
EditorUtility.SetDirty(this);
GUILayout.EndArea();
}
/// <summary>
/// Populate a string array with the room node types to display that can be selected
/// </summary>
public string[] GetRoomNodeTypeToDisplay()
{
string[] roomArray = new string[roomNodeTypeList.list.Count];
for(int i = 0; i < roomNodeTypeList.list.Count; i++)
{
if (roomNodeTypeList.list[i].displayInNodeGraphEditor)
{
roomArray[i] = roomNodeTypeList.list[i].roomNodeTypeName;
}
}
return roomArray;
}
/// <summary>
/// Process events for the node
/// </summary>
public void ProcessEvents(Event currentEvent)
{
switch(currentEvent.type)
{
// Process Mouse Down Events
case EventType.MouseDown:
ProcessMouseDownEvent(currentEvent);
break;
// Process Mouse Up Events
case EventType.MouseUp:
ProcessMouseUpEvent(currentEvent);
break;
// Process Mouse Drag Events
case EventType.MouseDrag:
ProcessMouseDragEvent(currentEvent);
break;
default:
break;
}
}
/// <summary>
/// Process mouse down events
/// </summary>
private void ProcessMouseDownEvent(Event currentEvent)
{
// If left click down
if (currentEvent.button == 0)
{
ProcessLeftClickDownEvent();
}
// If Right click down
if (currentEvent.button == 1)
{
ProcessRightClickDownEvent(currentEvent);
}
}
/// <summary>
/// Process right click down
/// </summary>
private void ProcessRightClickDownEvent(Event currentEvent)
{
roomNodeGraph.SetNodeToDrawConnectionLineFrom(this, currentEvent.mousePosition);
}
/// <summary>
/// Process left click down event
/// </summary>
private void ProcessLeftClickDownEvent()
{
Selection.activeObject = this;
// Toggle node selection
if(isSelected == true)
{
isSelected = false;
}
else
{
isSelected = true;
}
}
/// <summary>
/// Process mouse up events
/// </summary>
private void ProcessMouseUpEvent(Event currentEvent)
{
// If left click up
if (currentEvent.button == 0)
{
ProcessLeftClickUpEvent();
}
}
/// <summary>
/// Process left click up event
/// </summary>
private void ProcessLeftClickUpEvent()
{
if (isLeftClickDragging)
{
isLeftClickDragging = false;
}
}
/// <summary>
/// Process mouse drag event
/// </summary>
private void ProcessMouseDragEvent(Event currentEvent)
{
// process left click drag event
if (currentEvent.button == 0)
{
ProcessLeftMouseDragEvent(currentEvent);
}
}
/// <summary>
/// Process left mouse drag event
/// </summary>
private void ProcessLeftMouseDragEvent(Event currentEvent)
{
isLeftClickDragging = true;
DragNode(currentEvent.delta);
GUI.changed = true;
}
/// <summary>
/// Drag Node
/// </summary>
public void DragNode(Vector2 delta)
{
rect.position += delta;
EditorUtility.SetDirty(this);
}
/// <summary>
/// Add childID to the node (returns true if the node has been added, false otehrwise)
/// </summary>
public bool AddChildRoomNodeIDToRoomNode(string childID)
{
childRoomNodeIDList.Add(childID);
return true;
}
/// <summary>
/// Add parentID to the node (returns true if the node has been added, false otehrwise)
/// </summary>
public bool AddParentRoomNodeIDToRoomNode(string parentID)
{
parentRoomNodeIDList.Add(parentID);
return true;
}
#endif
#endregion Editor COde
}
using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
using Unity.VisualScripting;
using UnityEditor.MPE;
using System;
using PlasticPipe.PlasticProtocol.Messages;
public class RoomNodeGraphEditor : EditorWindow //편집기
{
private GUIStyle roomNodeStyle;
private static RoomNodeGraphSO currentRoomNodeGraph;
private RoomNodeSO currentRoomNode = null;
private RoomNodeTypeListSO roomNodeTypeList;
//Node layout Values
private const float nodeWidth = 160f;
private const float nodeHeight = 75f;
private const int nodePadding = 25;
private const int nodeBorder = 12;
// Connecting line values
private const float connectingLineWidth = 3f;
private const float connectingLineArrowSize = 6f;
[MenuItem("Room Node Graph Editor", menuItem = "Window/Dungeon Editor/Room Node Graph Editor")]
private static void OpenWindow()
{
GetWindow<RoomNodeGraphEditor>("Room Node Graph Editor");
}
private void OnEnable()
{
// Define node layout style
roomNodeStyle = new GUIStyle();
roomNodeStyle.normal.background = EditorGUIUtility.Load("node1") as Texture2D;
roomNodeStyle.normal.textColor = Color.white;
roomNodeStyle.padding = new RectOffset(nodePadding, nodePadding, nodePadding, nodePadding);
roomNodeStyle.border = new RectOffset(nodeBorder, nodeBorder, nodeBorder, nodeBorder);
// Load Room Node types
roomNodeTypeList = GameResources.Instance.roomNodeTypeList;
}
/// <summary>
/// Open the room node graph editor window if a room node graph scriptable object asset is double clicked in the inspector
/// </summary>
[OnOpenAsset(0)] // Need the namespace UnityEditor.Callbacks
public static bool OnDoubleClickAsset(int instanceID, int line)
{
RoomNodeGraphSO roomNodeGraph = EditorUtility.InstanceIDToObject(instanceID) as RoomNodeGraphSO;
if (roomNodeGraph != null)
{
OpenWindow();
currentRoomNodeGraph = roomNodeGraph;
return true;
}
return false;
}
/// Draw Editor GUI
private void OnGUI()
{
// If a scriptable object of type RoomNodeGraphSo has been selected then process
if (currentRoomNodeGraph != null)
{
// Draw line if being dragged
DrawDraggedLine();
// Process Events
ProcessEvents(Event.current);
// Draw Connections Between Room Nodes
DrawRoomConnections();
// Draw Room Nodes
DrawRoomNodes();
}
if (GUI.changed)
Repaint();
}
private void DrawDraggedLine()
{
if (currentRoomNodeGraph.linePosition != Vector2.zero)
{
//Draw line from node to line position
Handles.DrawBezier(currentRoomNodeGraph.roomNodeToDrawLineFrom.rect.center, currentRoomNodeGraph.linePosition,
currentRoomNodeGraph.roomNodeToDrawLineFrom.rect.center, currentRoomNodeGraph.linePosition, Color.white, null, connectingLineWidth);
}
}
private void ProcessEvents(Event currentEvent)
{
// Get room node that mouse is over if it's null or not currently being dragged
if (currentRoomNode == null || currentRoomNode.isLeftClickDragging == false)
{
currentRoomNode = IsMouseOverRoomNode(currentEvent);
}
// if mouse isn't over a room node or we are currently dragging a line from the room node thyen process graph events
if (currentRoomNode == null || currentRoomNodeGraph.roomNodeToDrawLineFrom != null)
{
ProcessRoomNodeGraphEvents(currentEvent);
}
// else process room node events
else
{
// process room node events
currentRoomNode.ProcessEvents(currentEvent);
}
}
/// <summary>
/// Check to see to mouse is over a room node - if so then return the room node else return null
/// </summary>
private RoomNodeSO IsMouseOverRoomNode(Event currentEvent)
{
for (int i = currentRoomNodeGraph.roomNodeList.Count - 1; i >= 0; i--)
{
if (currentRoomNodeGraph.roomNodeList[i].rect.Contains(currentEvent.mousePosition))
{
return currentRoomNodeGraph.roomNodeList[i];
}
}
return null;
}
/// <summary>
/// Process Room Node Graph Events
/// </summary>
private void ProcessRoomNodeGraphEvents(Event currentEvent)
{
switch(currentEvent.type)
{
// Process Mouse Down Events
case EventType.MouseDown:
ProcessMouseDownEvent(currentEvent);
break;
// Process Mouse Drag Events
case EventType.MouseDrag:
ProcessMouseDragEvent(currentEvent);
break;
// Process Mouse Up Events
case EventType.MouseUp:
ProcessMouseUpEvent(currentEvent);
break;
default:
break;
}
}
/// <summary>
/// Process mouse down events on the room node graph (not over a node)
/// </summary>
private void ProcessMouseDownEvent(Event currentEvent)
{
// Process right click mouse down on graph event (show context menu)
if(currentEvent.button == 1)
{
ShowContextMenu(currentEvent.mousePosition);
}
}
/// <summary>
/// Show the context menu
/// </summary>
private void ShowContextMenu(Vector2 mousePosition)
{
GenericMenu menu = new GenericMenu();
menu.AddItem(new GUIContent("Create Room Node "), false, CreateRoomNode, mousePosition);
menu.ShowAsContext();
}
/// <summary>
/// Create a room node at the mouse position
/// </summary>
private void CreateRoomNode(object mousePositionObject)
{
CreateRoomNode(mousePositionObject, roomNodeTypeList.list.Find(x => x.isNone));
}
/// <summary>
/// Create a room node at the mouse position - overloaded to also pass in RoomNodeType
/// </summary>
private void CreateRoomNode(object mousePositionObject, RoomNodeTypeSO roomNodeType)
{
Vector2 mousePosition = (Vector2)mousePositionObject;
// create room node scriptable object asset
RoomNodeSO roomNode = ScriptableObject.CreateInstance<RoomNodeSO>();
// add room node to current room node graph room node list
currentRoomNodeGraph.roomNodeList.Add(roomNode);
// set room node values
roomNode.Initialise(new Rect(mousePosition, new Vector2(nodeWidth, nodeHeight)), currentRoomNodeGraph, roomNodeType);
// add room node to room node graph scriptable object assets database
AssetDatabase.AddObjectToAsset(roomNode, currentRoomNodeGraph);
AssetDatabase.SaveAssets();
// Refresh graph node dictionary
currentRoomNodeGraph.OnValidate();
}
/// <summary>
/// Process mouse up event
/// </summary>
private void ProcessMouseUpEvent(Event currentEvent)
{
// if releasing the right mouse button and currently draggin a line
if (currentEvent.button == 1 && currentRoomNodeGraph.roomNodeToDrawLineFrom != null)
{
// Check if over a room node
RoomNodeSO roomNode = IsMouseOverRoomNode(currentEvent);
if (roomNode != null)
{
// if so set ift as a child of the parent room node if it can be added
if (currentRoomNodeGraph.roomNodeToDrawLineFrom.AddChildRoomNodeIDToRoomNode(roomNode.id))
{
// Set parent ID in child room node
roomNode.AddParentRoomNodeIDToRoomNode(currentRoomNodeGraph.roomNodeToDrawLineFrom.id);
}
}
ClearLineDrag();
}
}
/// <summary>
/// Process mouse drag event
/// </summary>
private void ProcessMouseDragEvent(Event currentEvent)
{
// process right click drag event - draw line
if (currentEvent.button == 1)
{
ProcessRightMouseDragEvent(currentEvent);
}
}
/// <summary>
/// Process right mouse drag event - draw line
/// </summary>
private void ProcessRightMouseDragEvent(Event currentEvent)
{
if (currentRoomNodeGraph.roomNodeToDrawLineFrom != null)
{
DragConnectingLine(currentEvent.delta);
GUI.changed = true;
}
}
/// <summary>
/// Drag connecting line from room node
/// </summary>
public void DragConnectingLine(Vector2 delta)
{
currentRoomNodeGraph.linePosition += delta;
}
/// <summary>
/// Clear line drag from a room node
/// </summary>
private void ClearLineDrag()
{
currentRoomNodeGraph.roomNodeToDrawLineFrom = null;
currentRoomNodeGraph.linePosition = Vector2.zero;
GUI.changed = true;
}
/// <summary>
/// Draw connections in the graph window between room nodes
/// </summary>
private void DrawRoomConnections()
{
// Loop through all room nodes
foreach (RoomNodeSO roomNode in currentRoomNodeGraph.roomNodeList)
{
if (roomNode.childRoomNodeIDList.Count > 0)
{
// Loop Through child room nodes
foreach (string childRoomNodeID in roomNode.childRoomNodeIDList)
{
//get child room node from dictionary
if (currentRoomNodeGraph.roomNodeDictionary.ContainsKey(childRoomNodeID))
{
DrawConnectionLine(roomNode, currentRoomNodeGraph.roomNodeDictionary[childRoomNodeID]);
GUI.changed = true;
}
}
}
}
}
/// <summary>
/// Draw connection line between the parent room node and child room node
/// </summary>
private void DrawConnectionLine(RoomNodeSO parentRoomNode, RoomNodeSO childRoomNode)
{
//get line start and end position
Vector2 startPosition = parentRoomNode.rect.center;
Vector2 endPosition = childRoomNode.rect.center;
//calculate midway point
Vector2 midPosition = (startPosition + endPosition) / 2f;
// Vector from start to end position of line
Vector2 direction = endPosition - startPosition;
// Calculate normalised perpendicular positions from the mid point
Vector2 arrowTailPoint1 = midPosition - new Vector2(-direction.y, direction.x).normalized * connectingLineArrowSize;
Vector2 arrowTailPoint2 = midPosition + new Vector2(-direction.y, direction.x).normalized * connectingLineArrowSize;
// Calculate mid point offset position for arrow head
Vector2 arrowHeadPoint = midPosition + direction.normalized * connectingLineArrowSize;
// Draw Arrow
Handles.DrawBezier(arrowHeadPoint, arrowTailPoint1, arrowHeadPoint, arrowTailPoint1, Color.white, null, connectingLineWidth);
Handles.DrawBezier(arrowHeadPoint, arrowTailPoint2, arrowHeadPoint, arrowTailPoint2, Color.white, null, connectingLineWidth);
// Draw line
Handles.DrawBezier(startPosition, endPosition, startPosition, endPosition, Color.white, null, connectingLineWidth);
GUI.changed = true;
}
/// <summary>
/// Draw room nodes in the graph window
/// </summary>
private void DrawRoomNodes()
{
// Loop through all room nodes and draw them
foreach (RoomNodeSO roomNode in currentRoomNodeGraph.roomNodeList)
{
roomNode.Draw(roomNodeStyle);
}
GUI.changed = true;
}
}