Unity编辑器开发分享

简单节点编辑器(二)

2017-09-25  本文已影响13人  小小小小小丶敏

上次的拖动窗口已经画出来了,这次我们看看怎么将两个窗口用连线连接起来吧。我们在上一篇的程序中,再增加一些代码,如下

using UnityEngine;  
using UnityEditor;  
public class NodeEditor : EditorWindow {  
    //窗口的矩形  
    Rect windowRect = new Rect(50, 50, 150, 100);  
    Rect windowRect2 = new Rect(100, 100, 150, 100);  
    //窗口的ID  
    int windownID = 0;  
    int windownID2 = 0;  
    static void ShowEditor() {  
        NodeEditor editor = EditorWindow.GetWindow<NodeEditor>();  
    }  
    void OnGUI() {  
        //绘画窗口  
        BeginWindows();  
        windowRect = GUI.Window(windownID, windowRect, DrawNodeWindow, "Demo Window");  
        windowRect2 = GUI.Window(windownID2,windowRect2,DrawNodeWindow,"Demo Window2");  
        EndWindows();  
        //连接窗口  
        DrawNodeCurve(windowRect,windowRect2);  
    }  
    //绘画窗口函数  
    void DrawNodeWindow(int id) {  
        //创建一个GUI Button  
        if (GUILayout.Button("Link")) {  
            Debug.Log("Clikc Link Button");  
        }  
        //设置改窗口可以拖动  
        GUI.DragWindow();  
    }  
      
    void DrawNodeCurve(Rect start, Rect end , Color color) {  
        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;  
        Handles.DrawBezier(startPos, endPos, startTan, endTan, color, null, 4);  
    }  
}  
运行Editor,出现这样的效果,是不是很帅气?当然,这还只是一些初步的代码,不过还是分享给大家学习,祝大家早日做好节点编辑器
上一篇下一篇

猜你喜欢

热点阅读