Unity编辑器 - DragAndDrop拖拽控件

Unity编辑器的拖拽(DragAndDrop)在网上能找到的资料少,自己稍微研究了一下,写了个相对完整的案例,效果如下

代码:

object dragData = "dragData";
Vector2 offset;
Color col = new Color(1, 0, 0, 0.6f);
Rect rect1 = new Rect(20, 10, 100, 20);
Rect rect2 = new Rect(20, 60, 100, 20);
Rect drect;
bool isDraging;
int cid; private void OnGUI() {
GUI.Box(rect1, "rect1");
GUI.Box(rect2, "rect2"); Event e = Event.current;
cid = GUIUtility.GetControlID(FocusType.Passive);
switch (e.GetTypeForControl(cid)) {
case EventType.MouseDown:
if (rect1.Contains(e.mousePosition))
GUIUtility.hotControl = cid;
break;
case EventType.MouseUp:
if (GUIUtility.hotControl == cid)
GUIUtility.hotControl = 0;
break;
case EventType.MouseDrag:
Debug.Log("MouseDrag");
if (GUIUtility.hotControl == cid && rect1.Contains(e.mousePosition)) {
DragAndDrop.PrepareStartDrag();
//DragAndDrop.objectReferences = new Object[] { };
DragAndDrop.SetGenericData("dragflag", dragData);
DragAndDrop.StartDrag("dragtitle");
offset = e.mousePosition - rect1.position;
drect = rect1;
isDraging = true;
e.Use();
}
break;
case EventType.DragUpdated:
Debug.Log("DragUpdated");
drect.position = e.mousePosition - offset;
if (rect2.Contains(e.mousePosition)) {
DragAndDrop.visualMode = DragAndDropVisualMode.Generic;
drect = rect2;
}
e.Use();
break;
case EventType.DragPerform:
Debug.Log("DragPerform");
DragAndDrop.AcceptDrag();
Debug.Log("DragPerform : " + DragAndDrop.GetGenericData("dragflag"));
e.Use();
break;
case EventType.DragExited:
Debug.Log("DragExited");
isDraging = false;
if (GUIUtility.hotControl == cid)
GUIUtility.hotControl = 0;
e.Use();
break;
} if (isDraging) {
EditorGUI.DrawRect(drect, col);
}
}

事件调用顺序

Unity编辑器 - DragAndDrop拖拽控件的更多相关文章

  1. 【C#/WPF】GridSplitter 分割布局,拖拽控件分隔栏以改变控件尺寸

    需求:界面由多部分控件组成,想要拖拽控件之间的分隔栏以改变尺寸. MainWindow.xaml: <Grid> <Grid.ColumnDefinitions> <Co ...

  2. Unity编辑器 - 鼠标悬停在控件上时改变鼠标样式

    Unity编辑器 - 鼠标悬停在控件上时改变鼠标样式 摘自Unity文档 EditorGUIUtility.AddCursorRect public static void AddCursorRect ...

  3. Unity编辑器 - 使用GL绘制控件

    Unity编辑器 - 使用GL绘制控件 控件较为复杂时,可能造成界面卡顿,在EditorGUI中也可以灵活使用GL绘制来提升性能. 以绘制线段为例: using UnityEngine; using ...

  4. flutter Draggable Widget拖拽控件

    Draggable Widget Draggable控件负责就是拖拽,父层使用了Draggable,它的子元素就是可以拖动的,子元素可以实容器,可以是图片.用起来非常的灵活. 参数说明: data: ...

  5. Flutter 拖拽控件Draggable

    Flutter提供了强大的拖拽控件,可以灵活定制,并且非常简单.下面作一个拖拽的案例. Draggable Widget Draggable控件负责就是拖拽,父层使用了Draggable,它的子元素就 ...

  6. ios-将代码创建的视图控件放入拖拽控件的下面

    如图所示 图片是拖拽上去的imageView,橘黄色控件是在代码中创建的添加上去的,此时黄色view在imageView 上方 调用方法bringSubviewToFront:试图将imageView ...

  7. H5实现多图片预览上传,可点击可拖拽控件介绍

    版权声明:欢迎转载,请注明出处:http://blog.csdn.net/weixin_36380516 在做图片上传时发现一个蛮好用的控件,支持多张图片同时上传,可以点击选择图片,也可以将图片拖拽到 ...

  8. Flutter 拖拽控件Draggable看这一篇就够了

    注意:无特殊说明,Flutter版本及Dart版本如下: Flutter版本: 1.12.13+hotfix.5 Dart版本: 2.7.0 Draggable系列组件可以让我们拖动组件. Dragg ...

  9. 拖拽控件java版

    Button vv = new Button("vvvv");  DragSource.getDefaultDragSource().createDefaultDragGestur ...

随机推荐

  1. 面试准备——(三)Selenium面试题总结

    一.Selenium基本知识 1. 什么是Selenium? Selenium是浏览器自动化工具,主要用来Web的自动化测试,以及基于Web的任务管理自动化.它支持的语言有:python.Java.r ...

  2. ASP.NET WebApi 中使用swagger 构建在线帮助文档

    1 在Visual Studio 中创建一个Asp.NET  WebApi 项目,项目名:Com.App.SysApi(本例创建的是 .net 4.5 框架程序) 2  打开Nuget 包管理软件,查 ...

  3. mybatis if 语句嵌套

    在使用mybatis的时候,可以在 if 标签下面加上if标签. 比如要对这个sql语句进行改进. select a.* from emp a inner join dept b on a.deptn ...

  4. c#随便聊聊数据库操作

    最近在学习web后台以及Python,到了程序员的转折年纪了,哎.估计很久不会写博文了.言归正传. 在原理的数据库连接池HiKari项目上.我扩展了独立的3个库,说是3个库,其实原本该是一个库.先聊聊 ...

  5. 算法基础-dfs

    最近在学dfs(深度优先搜索),dfs与树的遍历差不多,就是先从一个点开始一直搜索,直到走不动为止.现在推荐一个简单的dfs题, 百炼的2815, ########################## ...

  6. RAC初体验(环境搭建)

      实施阶段: 1.主机配置 2.安装Clusterware 3.安装Oracle Database 4.配置Listener 5.创建ASM 6.创建Database 一.主机配置 1.网络设置 I ...

  7. multiprocessing中进程池,线程池的使用

    multiprocessing 多进程基本使用 示例代码1 import time import random from multiprocessing import Process def run( ...

  8. 史上更全的 MySQL 高性能优化实战总结!

    1 前言 2 优化的哲学 3 优化思路 3.1 优化什么 3.2 优化的范围有哪些 3.3 优化维度 4 优化工具有啥? 4.1 数据库层面 4.2 数据库层面问题解决思路 4.3 系统层面 4.4 ...

  9. Python-爬虫小计

    # -*-coding:utf8-*-import requestsfrom bs4 import BeautifulSoupimport timeimport osimport urllibimpo ...

  10. Debian中CodeIgniter+nginx+MariaDB+phpMyAdmin配置

    本文不讲述软件安装过程,记述本人在Debia中配置CodeIgniter时遇到的问题及解决方法,希望能够为有需要的人提供帮助. 一.Debian版本及所需的软件 Debian 9.8 stretch ...