ugui拖拽
整理了下以前写的
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems; [RequireComponent(typeof(CanvasGroup))]
public class ItemDrag : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler
{
private CanvasGroup canvasGroup;
private Transform originalGrid;//拖出的格子
public GameObject bagFrame;//拖出格子的父级
Vector2 originGridSize;//原始大小 // Start is called before the first frame update
void Start()
{
originGridSize = (transform as RectTransform).sizeDelta;
canvasGroup = GetComponent<CanvasGroup>();
} public void OnBeginDrag(PointerEventData eventData)
{
//让eventTrigger无法检测到物体,让他去检测物体后面的格子
canvasGroup.blocksRaycasts = false;
originalGrid = gameObject.transform.parent;
gameObject.transform.SetAsLastSibling();//设置ui最上层
} public void OnDrag(PointerEventData eventData)
{
//把物体设置到背景上的字节点上,是为了防止格子挡住物品
transform.SetParent(bagFrame.transform);
//跟随鼠标移动
transform.position = Input.mousePosition;
} public void OnEndDrag(PointerEventData eventData)
{
//结束拖拽时,鼠标所碰撞的点的物品
GameObject curEnter = eventData.pointerEnter;
//满足条件,正确地放入格子里面
if (curEnter != null && curEnter.tag == "bagGrid")
{
//设置格子的父节点
transform.SetParent(curEnter.transform, false);
//设置预制物的位置
(transform as RectTransform).localPosition = Vector3.zero;
(transform as RectTransform).localRotation = Quaternion.identity;
(transform as RectTransform).localScale = Vector3.one;
//设置成原来的大小
(transform as RectTransform).sizeDelta = originGridSize;
}
//没有满足条件
else
{
//设置格子的父节点
transform.SetParent(originalGrid.transform);
//设置预制物的位置
(transform as RectTransform).localPosition = Vector3.zero;
(transform as RectTransform).localRotation = Quaternion.identity;
(transform as RectTransform).localScale = Vector3.one;
//设置成原来的大小
(transform as RectTransform).sizeDelta = originGridSize;
print("没有满足条件");
} //eventTrigger可以检测到物体
canvasGroup.blocksRaycasts = true;
} //点击
public void OnPointerClick(PointerEventData eventData)
{
print("点击");
} //鼠标进入
public void OnPointerEnter(PointerEventData eventData)
{
print("进入");
} //鼠标退出
public void OnPointerExit(PointerEventData eventData)
{
print("退出");
}
}
ugui拖拽的更多相关文章
- 使用UGUI实现拖拽功能(拼图小游戏)
实现方式 1.引入UGUI自带的事件系统 UnityEngine.EventSystems 2.为我们的类添加接口 IBeginDragHandler, IDragHandler, IEndDragH ...
- Unity3D UGUI窗口拖拽
在开发UGUI时 我们时常需要做一个窗口拖拽的功能 先上代码 using UnityEngine; using UnityEngine.EventSystems; public class DragW ...
- Unity UGUI 实现简单拖拽功能
说到拖拽,那必然离不开坐标,UGUI 的坐标有点不一样,它有两种坐标,一种是屏幕坐标,还有一种就是 UI 在Canvas内的坐标(暂时叫做ugui坐标),这两个坐标是不一样的,所以拖拽就需要转换. 因 ...
- 使用NGUI实现拖拽功能(拼图小游戏)
上一次用UGUI实现了拼图小游戏,这次,我们来用NGUI来实现 实现原理 NGUI中提供了拖拽的基类UIDragDropItem,所以我们要做的就是在要拖拽的图片上加一个继承于该类的脚本,并实现其中的 ...
- Unity输出PC端(Windows) 拖拽文件到app中
需求:给策划们写一个PC端(Window)的Excel导表工具.本来用OpenFile打开FileExplorerDialog后让他们自己选择想要添加的Excel文件就行了,结果有个需求是希望能拖拽E ...
- 在屏幕拖拽3D物体移动
3D物体的拖拽不同于2D的.因为3D物体有x,y,z当然.实际拖拽还是在XZ平面.只是多了几个转换 using UnityEngine; using System.Collections; publi ...
- NGUI拖拽简介
挂上UIDragDropItem就可以实现拖拽,按钮监听Drop消息即可实现对拖放的监听. UIDragDropItem有一个Clone On Drag选项,勾上可以克隆,但会被ScrollView遮 ...
- Unity实现放大缩小以及相机位置平移实现拖拽效果
放大缩小功能是游戏开发中用到的功能,今天就来讲一下Unity中放大缩小怎么实现. 1.IDragHandler, IBeginDragHandler, IEndDragHandler这三个接口是Uni ...
- JavaScript动画-拖拽改变元素大小
▓▓▓▓▓▓ 大致介绍 拖拽改变元素大小是在模拟拖拽上增加了一些功能 效果:拖拽改变元素大小 ▓▓▓▓▓▓ 拖拽改变元素大小原理 首先这个方块得知道我们想要改变这个它的大小,所以我给它设定一个范围,当 ...
随机推荐
- Flume 实时获取日志内容插入MySQL
https://www.jianshu.com/p/22e6133649ca 采用链接的方法试了一下,好像不成功,问题出在 channel.take(); //获取出来的Event为空,不知道为啥
- LAMP 系统性能调优之内核调优措施
LAMP 系统性能调优之内核调优措施 2011-03-18 11:21 Sean A. Walberg 网络转载 字号:T | T 在对系统的 Apache.PHP 和 MySQL 组件进行调优之前, ...
- jmeter录制对于ip代理会失效
jmeter对于ip代理会失效,ip不能走代理,只有域名可以,因此如果需要用jmeter录制ip代理的请求,需要配置hosts访问,将ip转换成域名 如访问http://127.0.0.1:8080/ ...
- mysql分组排序加序号
参照https://www.cnblogs.com/CharlieLau/p/6737243.html 一.需求 新加一个Sort 字段,初始值为1,按照parentID分组添加sort值. 根据原数 ...
- python 使用嵌套函数报local variable xxx referenced before assignment或者 local variable XXX defined in enclosing scope
情况一: a 直接引用外部的,正常运行 def toplevel(): a = 5 def nested(): print(a + 2) # theres no local variable a so ...
- Linux 环境下 gzip 的加解密命令
1.加密 [root@127-0-0-1 nginx]# gzip -v access.log-20190328 access.log-20190328: 95.8% -- replaced with ...
- jeesite安装时Perhaps you are running on a JRE rather than a JDK
使用自己本地安装的maven,启动jeesite报错: No compiler is provided in this environment. Perhaps you are running on ...
- Ajax异步提交的步骤
1.创建XHR对象 ,XMLHttpRequest(该对象负责悄悄滴与服务器进行交互): 2.设置响应函数/回调函数(响应函数规定对返回自服务器的信息如何进行处理): 3.通过xmlhttp.open ...
- Win7,win10(部分机型) 安装appscan9.0.3.10(可升级)实操流程
Win10部分机型不能很好的兼容appscan,建议使用者用win7系统安装appscan 写于:2018.12.2 IBM Security AppScan Standard 可通过自动执行应用安全 ...
- es6的class关键字与es5的构造函数
1,构造函数 function Point(x, y) { this.x = x; this.y = y; } Point.prototype.toString = function () { ret ...