Created
January 5, 2020 15:49
-
-
Save AnsisMalins/2f2b8dc4c6d3cc9910be68192e472219 to your computer and use it in GitHub Desktop.
How to do a custom node graph editor in Unity 2018.4 or 2019.2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The commented out parts are for Unity 2018.4 | |
//using System.Reflection; | |
//using UnityEditor.Experimental.UIElements.GraphView; | |
//using UnityEngine.Experimental.UIElements; | |
//using UnityEngine.Experimental.UIElements.StyleEnums; | |
using System.Collections.Generic; | |
using System.Linq; | |
using UnityEditor; | |
using UnityEditor.Experimental.GraphView; | |
using UnityEngine; | |
using UnityEngine.UIElements; | |
public class DialogueEditorWindow : EditorWindow | |
{ | |
private bool initialized = false; | |
private DialogueGraphView view; | |
[MenuItem("Window/Dialogue Editor")] | |
public static void ShowWindow() | |
{ | |
var window = EditorWindow.GetWindow<DialogueEditorWindow>("Dialogue Editor"); | |
if (!window.initialized) | |
{ | |
window.Initialize(); | |
Debug.Log("Dialogue Editor initialized"); | |
window.initialized = true; | |
} | |
window.Show(); | |
} | |
private void Initialize() | |
{ | |
view = new DialogueGraphView(); | |
view.style.alignSelf = Align.Stretch; | |
view.style.flexGrow = 1; | |
view.AddManipulator(new ContentDragger()); | |
view.AddManipulator(new SelectionDragger()); | |
view.AddManipulator(new RectangleSelector()); | |
view.AddManipulator(new ClickSelector()); | |
view.SetupZoom(0.5f, 2f); | |
rootVisualElement.Add(view); | |
} | |
/*private VisualElement rootVisualElement | |
{ | |
get | |
{ | |
return GetType() | |
.GetProperty("rootVisualContainer", BindingFlags.Instance | BindingFlags.NonPublic) | |
.GetValue(this) as VisualElement; | |
} | |
}*/ | |
} | |
public sealed class DialogueGraphView : GraphView | |
{ | |
public override List<Port> GetCompatiblePorts(Port startPort, NodeAdapter nodeAdapter) | |
{ | |
return ports.ToList(); // All ports are compatible! | |
} | |
public override void BuildContextualMenu(ContextualMenuPopulateEvent evt) | |
{ | |
base.BuildContextualMenu(evt); | |
//evt.menu.AppendAction("Create", OnCreate, a => DropdownMenu.MenuAction.StatusFlags.Normal); | |
evt.menu.AppendAction("Create", OnCreate); | |
} | |
private void OnCreate(DropdownMenuAction action) | |
{ | |
var node = new Node(); | |
node.title = "NODE"; | |
var port1 = node.InstantiatePort(Orientation.Horizontal, Direction.Input, Port.Capacity.Single, typeof(string)); | |
port1.portName = "in"; | |
port1.source = ""; | |
node.inputContainer.Add(port1); | |
var port2 = node.InstantiatePort(Orientation.Horizontal, Direction.Output, Port.Capacity.Multi, typeof(string)); | |
port2.portName = "out"; | |
port2.source = ""; | |
node.outputContainer.Add(port2); | |
node.SetPosition(new Rect(action.eventInfo.localMousePosition, Vector2.zero)); | |
AddElement(node); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment