当前位置:网站首页>Unity Editor Extension - drag and drop
Unity Editor Extension - drag and drop
2022-07-03 08:27:00 【T.D.C】
Drag and drop
- 2 Methods
- Use ChildWindow
- Custom control processing mouse drag event processing completes the change of control position
design sketch
- Click on add Create subwindow
- After selecting the window, link the currently selected window with the previously selected window with a line
This paper introduces the use of ChildWindow The drag and drop
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace DC.DCIMGUIBox
{
public class WindowNodeData
{
public Rect rect;
public bool selected;
}
public class DragWindow : EditorWindow
{
[MenuItem("DC/IMGUI/DragWindow")]
public static void Open()
{
var window = GetWindow<DragWindow>();
window.minSize = new Vector2(800, 600);
}
public List<WindowNodeData> nodeDataList = new List<WindowNodeData>();
List<Tuple<WindowNodeData, WindowNodeData>> lines = new List<Tuple<WindowNodeData, WindowNodeData>>();
private WindowNodeData lastSelectedWindow = null;
public void OnGUI()
{
GUILayout.BeginHorizontal();
if (GUILayout.Button("add"))
{
nodeDataList.Add(new WindowNodeData() {rect = new Rect(100, 100, 200, 100)});
}
GUILayout.EndHorizontal();
var col = new Color(0.8f, 0.3f, 0.3f);
for (int i = 0; i < lines.Count; i++)
{
var line = lines[i];
var startPos = line.Item1.rect.center;
var endPos = line.Item2.rect.center;
DrawConnectLine(new Vector3(startPos.x, startPos.y), new Vector3(endPos.x, endPos.y), col);
}
BeginWindows();
for (int i = 0; i < nodeDataList.Count; i++)
{
nodeDataList[i].rect = GUILayout.Window(i, nodeDataList[i].rect, WindowNode, i.ToString());
}
EndWindows();
}
public static void DrawConnectLine(Vector3 startPos, Vector3 endPos, Color color)
{
var startTangent = new Vector3(startPos.x + Mathf.Sign(endPos.x - startPos.x) * 10, startPos.y, startPos.z);
var endTangent = new Vector3(endPos.x + Mathf.Sign(endPos.y - startPos.y) * 10, endPos.y, endPos.z);
Handles.DrawBezier(startPos, endPos, startTangent, endTangent, color, CustomControls.texture2d, 1);
}
public void OnNodeSelected(WindowNodeData data)
{
for (int i = 0; i < nodeDataList.Count; i++)
{
if (nodeDataList[i] == data)
{
continue;
}
nodeDataList[i].selected = false;
}
Repaint();
if (lastSelectedWindow != null)
{
lines.Add(new Tuple<WindowNodeData, WindowNodeData>(lastSelectedWindow, data));
}
lastSelectedWindow = data;
}
public void WindowNode(int windowId)
{
var data = nodeDataList[windowId];
GUI.DragWindow(new Rect(0, 0, data.rect.width, 16));
var curEvt = Event.current;
if (curEvt.type == EventType.MouseDown && curEvt.button == 0 &&
new Rect(0, 16, data.rect.width, data.rect.height - 16).Contains(curEvt.mousePosition))
{
data.selected = true;
OnNodeSelected(data);
}
if (data.selected)
{
EditorGUI.DrawRect(new Rect(0, 0, data.rect.width, data.rect.height), new Color(0.6f, 0, 0.4f, 0.3f));
}
GUILayout.Label(windowId.ToString());
}
}
}
边栏推荐
猜你喜欢
Haproxy+kept build 01
How to establish rectangular coordinate system in space
UE4 source code reading_ Bone model and animation system_ Animation node
Haproxy+kept cluster setup 02
基于SSM的校园失物招领平台,源码,数据库脚本,项目导入运行视频教程,论文撰写教程
Unity learning notes
MXone Pro自适应2.0影视模板西瓜视频主题苹果cmsV10模板
Three characteristics
Youyou1 of xlua knapsack system
Basic operation and process control
随机推荐
UE4 source code reading_ Bone model and animation system_ Animation compression
Delete the last character of the string in golang
C#课程设计之员工信息管理系统
redis集群系列四
Multi traveling salesman problem -- overview of formula and solution process
Golang中删除字符串的最后一个字符
Cesium for unreal quick start - simple scenario configuration
Go resolve ID card
Osgearth north arrow display
Classes and objects
VIM learning notes from introduction to silk skating
Golang 时间格式整理
MySQL 8
Initial unity
Three characteristics
Base64编码简介
Haproxy+kept cluster setup 02
十六进制编码简介
LinkList
Yolo series --- xml2txt script