Skip to content

Instantly share code, notes, and snippets.

@float1251
Last active December 10, 2015 09:14
Show Gist options
  • Save float1251/fd8e2ace6c2b99e0f6bf to your computer and use it in GitHub Desktop.
Save float1251/fd8e2ace6c2b99e0f6bf to your computer and use it in GitHub Desktop.
UnityEditorでNodeベースのエディターを作る。
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
public class CustomEditorWindow : EditorWindow
{
class NodeData
{
public Rect window;
public string title;
public string name;
public float test;
public string style;
public List<NodeData> children = new List<NodeData>();
}
List<NodeData> list = new List<NodeData>();
[MenuItem("Window/UnityPlayer")]
static void Open()
{
var editor = EditorWindow.GetWindow<CustomEditorWindow>();
editor.Init();
}
void Init()
{
var w1 = new NodeData();
w1.window = new Rect(10, 10, 300, 100);
w1.title = "Title";
w1.name = "name1";
var w2 = new NodeData();
w2.title = "Title2";
w2.name = "name2";
w2.style = "flow node 1";
w2.window = new Rect(210, 210, 300, 100);
w1.children.Add(w2);
list.Add(w1);
list.Add(w2);
}
public void OnGUI()
{
// Nodeをつなげる
list.Where(e => e.children.Count > 0).ToList().ForEach(_ =>
{
_.children.ForEach(v => DrawNodeCurve(_.window, v.window));
});
OnRightClick();
BeginWindows();
for (var i = 0; i < list.Count; i++)
{
var n = list[i];
if (string.IsNullOrEmpty(n.style))
{
n.window = GUI.Window(i, n.window, DrawNodeWindow, n.title);//, "flow node 5");
}
else
{
n.window = GUI.Window(i, n.window, DrawNodeWindow, n.title, n.style);//, "flow node 5");
}
}
EndWindows();
}
void OnRightClick()
{
var evt = Event.current;
if (evt.type == EventType.ContextClick)
{
var pos = evt.mousePosition;
GenericMenu menu = new GenericMenu();
menu.AddItem(new GUIContent("Create Node"), false, (obj) =>
{
CreateNode((Vector2)obj, "Title", "test");
}, pos);
menu.ShowAsContext();
evt.Use();
}
if (evt.type == EventType.MouseDown && evt.button == 1)
{
var pos = evt.mousePosition;
GenericMenu menu = new GenericMenu();
var index = list.FindIndex(_ => _.window.Contains(pos));
if (index >= 0)
{
menu.AddItem(new GUIContent("Delete Node"), false, (obj) =>
{
list.RemoveAt(index);
}, pos);
}
menu.ShowAsContext();
evt.Use();
}
}
void DrawNodeWindow(int id)
{
EditorGUILayout.LabelField(list[id].name);
list[id].test = EditorGUILayout.Slider("test", list[id].test, 0, 100);
GUI.DragWindow();
}
void DrawNodeCurve(Rect start, Rect end)
{
Vector3 startPos = new Vector3(start.x + start.width, start.y + start.height / 2, 0);
Vector3 endPos = new Vector3(end.x, end.y + end.height / 2, 0);
Vector3 startTan = startPos + Vector3.right * 50;
Vector3 endTan = endPos + Vector3.left * 50;
Color shadowCol = new Color(0, 0, 0, 0.06f);
for (int i = 0; i < 3; i++) // Draw a shadow
Handles.DrawBezier(startPos, endPos, startTan, endTan, shadowCol, null, (i + 1) * 5);
Handles.DrawBezier(startPos, endPos, startTan, endTan, Color.black, null, 1);
}
void CreateNode(Vector2 pos, string title, string name)
{
var w = new NodeData();
w.title = title;
w.name = name;
w.window = new Rect(pos.x, pos.y, 300, 100);
list.Add(w);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment