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. spring+jdbc+template+transaction实现

    使用spring和jdbc模板事务实现 1.创建实体类: Role package com.wbg.sjt.entity; public class Role { private int id; pr ...

  2. PAT——1002. 写出这个数

    读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式:每个测试输入包含1个测试用例,即给出自然数n的值.这里保证n小于10100. 输出格式:在一行内输出n的各位数字之和的每 ...

  3. 【题解】洛谷P1941 [NOIP2014TG] 飞扬的小鸟(背包DP)

    次元传送门:洛谷P1941 思路 从题意可知 在每个单位时间内 可以无限地向上飞 但是只能向下掉一次 所以我们可以考虑运用背包解决这道题 上升时 用完全背包 下降时 用01背包 设f[x][y]为在坐 ...

  4. HDU 1248 寒冰王座 (完全背包)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1248 寒冰王座 Time Limit: 2000/1000 MS (Java/Others)    M ...

  5. Java基础随笔3

    一. 键盘录入数据概述 我们目前在写程序的时候,数据值都是固定的,但是实际开发中,数据值肯定是变化的,所以,把数据改进为键盘录入,提高程序的灵活性. 键盘录入数据的步骤: A:导包(位置放到class ...

  6. 一点一点看JDK源码(五)java.util.ArrayList 后篇之Spliterator多线程遍历

    一点一点看JDK源码(五)java.util.ArrayList 后篇之Spliterator多线程遍历 liuyuhang原创,未经允许禁止转载 本文举例使用的是JDK8的API 目录:一点一点看J ...

  7. 初学JavaScript从入门到放弃(一)JavaScript介绍、变量、数据类型

    一.JavaScript介绍 1.JavaScript:轻量级的客户端脚本语音 2.目前js已经不仅仅是客户语音,基于NODE可以做服务器端程序,所以Javascript是全栈编程语音 3.js及部分 ...

  8. Linux下抓取登陆用户密码神器mimipenguin

    windows下有Mimikatz,现在linux下有了mimipenguin,国外安全研究员huntergregal发布了工具mimipenguin,一款Linux下的密码抓取神器,弥补了Linux ...

  9. lock free

    #include <thread> #include <iostream> #include <mutex> #include <atomic> #in ...

  10. Eclipse切换字体颜色

    打开window-preferences