UGUI事件之Drag拖拽事件

UI事件之Drag拖拽事件
========================================================
2.UGUI 事件命名空间
  当我们需要使用 UGUI 中的事件的时候,需要在脚本内引入专有命名空间:
  using UnityEngine.EventSystems;
----------------------------------
2.拖拽相关事件接口
----------------------------------
1.三个拖拽事件相关接口
  * IBeginDragHandler: 开始拖拽事件处理器;开始拖拽的一瞬间触发。
  * IDragHandler: 拖拽中事件处理器;拖拽过程中持续触发。
  * IEndDragHandler: 结束拖拽事件处理器;拖拽结束的一瞬间触发。
----------------------------------
扩展理解:
  这种“开始”“持续中”“结束”的模式,在 Unity 的交互中是非常常见的。
  我们之前的碰撞检测,触发检测,鼠标和键盘的按键检测,都有这三个状态。
----------------------------------
2.接口使用步骤
  ①当前脚本首先需要引入事件命名空间 EventSystems;
  ②在当前类继承的父类的后方,用逗号分隔,写需要使用到接口名;
  ③鼠标放到接口名上,右键-->实现接口-->实现接口 / 显示实现接口;
  ④编写相应事件的方法体,先简单输出调试。
----------------------------------
3.通过拖拽事件改变图片位置
  RectTransformUtility. / /RectTransform 工具类;
  ScreenPointToWorldPointInRectangle( //屏幕坐标点转化为世界坐标点;
  m_RectTransform, //游戏物体的 RectTransform ;
  eventData.position, //当前坐标位置点;
  eventData.enterEventCamera, //事件摄像机;
  out pos); //最终计算得到的世界坐标位置;
  PointerEventData:指针事件数据。
  上面的这个方法我们只需要写在“拖拽中事件”方法内,将最终的 pos 位置值
  持续赋值给当前游戏物体的 position 即可,就可以实现拖拽改变图片的位置。
========================================================
实例: 鼠标拖动游戏物体
//获取组件引用
m_RT = gameObject.GetComponent<RectTransform>();
//得到实时坐标位置转化成3D坐标,并返回一个位置变量
RectTransformUtility.ScreenPointToWorldPointInRectangle(m_RT,eventData.position,eventData.enterEventCamera,out pos);
//赋值给游戏物体
m_RT.position = pos;
----------------------------------
总结: 继承接口,实现接口,写入处理代码实现效果。

把下面的代码保存到一个代码文件,拖给一个游戏物体

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;public class ItemDrag : MonoBehaviour ,IBeginDragHandler,IDragHandler,IEndDragHandler
{private RectTransform m_RT;void IBeginDragHandler.OnBeginDrag(PointerEventData eventData){print("IBeginDragHandler.OnBeginDrag");gameObject.GetComponent<Transform>().position = Input.mousePosition;print("这是实现的拖拽开始接口");}void IDragHandler.OnDrag(PointerEventData eventData){print("IDragHandler.OnDrag");//虽然用Input.mousePosition可以得到一个2D坐标,不过我们现在需要的是3D坐标,看下面//gameObject.GetComponent<Transform>().position = Input.mousePosition;//3D坐标获取方法
        Vector3 pos;m_RT = gameObject.GetComponent<RectTransform>();//屏幕坐标到世界坐标RectTransformUtility.ScreenPointToWorldPointInRectangle(m_RT,eventData.position,eventData.enterEventCamera,out pos);m_RT.position = pos;print("拖拽中……");}void IEndDragHandler.OnEndDrag(PointerEventData eventData){print("IEndDragHandler.OnEndDrag");gameObject.GetComponent<Transform>().position = Input.mousePosition;print("实现的拖拽结束接口");}
}


如有错误,欢迎指出。

转载于:https://www.cnblogs.com/madinglin/p/8470961.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/278395.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java 通过cookie判断是否登陆

protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {// 判断cookie是否有登录信息Cookie[] cookies req.getCookies();boolean isLogin false;for(Cookie c : cookies){if("loginInfo".equals(c.getNa…

使用高级管理控制台获得对Windows Home Server的扩展访问

Windows Home Server is easy to setup and use so anyone with basic computer knowledge can operate their own server. But what if you’re an advanced user and want more control over various administrative functions? The Advanced Admin Console Addin gives you…

变动性算法源代码分析与使用示例(copy_backward、 transform、 replace_copy_if 等)

首先回顾前面的文章&#xff0c;我们把for_each 归类为非变动性算法&#xff0c;实际上它也可以算是变动性算法&#xff0c;取决于传入的第三个参数&#xff0c;即函数 指针。如果在函数内对容器元素做了修改&#xff0c;那么就属于变动性算法。 变动性算法源代码分析与使用示例…

[转]QDir类及其用法总结

直接给出原文链接&#xff1a;QDir类及其用法总结 转载于:https://www.cnblogs.com/rainbow70626/p/10330643.html

如何在Outlook中的电子邮件上显示快速操作按钮

There are probably actions you regularly perform in Outlook, such as deleting, archiving, and marking things as read. Here’s how to use Quick Action buttons to add one-click options that appear over every email to perform each action. 您可能会在Outlook中定…

c++读取和写入TXT文件的整理

c读取和写入TXT文件的整理 #include "stdafx.h" #include <iostream> //无论读写都要包含<fstream>头文件 #include <fstream> #include <iomanip> using namespace std;int main() {//ifstream从文件流向内存的ifstream表示文件输入流…

使用RestTemplate时报错java.lang.IllegalStateException: No instances available for 127.0.0.1

我在RestTemplate的配置类里使用了 LoadBalancedComponentpublic class RestTemplateConfig { Bean LoadBalanced public RestTemplate restTemplate(){ return new RestTemplate(); }}或者 再调用Autowiredprivate RestTemplate restTemplate;必须使用应用名作为代替ip:端口&a…

sh变量特性(3)默认特性

变量说明$0当前脚本的文件名$n传递给脚本或函数的参数&#xff0c;n是数字&#xff0c;第n个参数$#传递给脚本或函数的参数个数$*传递给脚本或函数的所有参数$传递给脚本或函数的所有参数。被””包含时&#xff0c;与$*稍有不同$?上个命令的退出状态&#xff0c;或函数返回值…

zune linux_更新您的Zune Player软件

zune linuxKeeping your computer and software up to date is very important in keeping everything running smooth and secure. It’s also important to keep your geeky gadgets updated as well. Here we take a look at updating a Zune HD. 保持计算机和软件的最新状态…

继承的几种方式

1.借助构造函数实现继承 function Parent() { this.name parent } Parent.prototype.say function () { // 不能被继承 this.say function() { console.log(hello this.name) } } function Child() { Parent.call(this) this.type child } console.log(new Child) // 没有参…

写一个简单的 django_post demo

1.新建一个django工程&#xff0c;其路由为下图 2.要做的是一个 简单的登录请求&#xff0c;以表单形式提交&#xff0c;html 部分代码如下 这里注意action指向的是路由的地址&#xff0c;index1后的views.login部分代码如下 这段代码指的是&#xff0c;如果login接收到的请求是…

日志收集

2019独角兽企业重金招聘Python工程师标准>>> ELK (ElasticSearch、Logstash、Kibana)&#xff1a; https://my.oschina.net/itblog/blog/547250 转载于:https://my.oschina.net/zfscofield/blog/1625703

autocopy2u_借助AutoCopy简化Firefox中的文本复制和粘贴

autocopy2uLooking for an easy way to speed up copying and pasting in Firefox? Now you can reduce the amount of work that you have to do by half with AutoCopy. 是否在寻找一种简便的方法来加快Firefox中的复制和粘贴&#xff1f; 现在&#xff0c;您可以使用自动复…

virtualenv模块使用

开发多个应用&#xff1a; 如A需要jinja2.7开发&#xff1b;如B需要jinja2.6开发。或者C需要Python2.7开发&#xff0c;D需要Python3.5开发 那么解决上述问题就需要使用virtualenv这个模块&#xff1a; 它的作用是&#xff1a;创建“隔离”环境&#xff0c;使项目拥有独立的Pyt…

僵尸进程处理方式

Linux服务器上&#xff0c;多少会出现一些僵尸进程&#xff0c;下面介绍如何快速寻找和消灭这些僵尸进程的方法 首先&#xff0c;我们可以用top命令来查看服务器当前是否有僵尸进程&#xff0c;在下图中可以看到僵尸进程数的提示&#xff0c;如果数字大于0&#xff0c;那么意味…

chromebook刷机_如何查看Chromebook的停产日期

chromebook刷机Google谷歌There comes a time in your Chromebook’s life when it no longer receives updates from Google. It’s inevitable and could be a lot sooner than you think. Here’s how to see your Chromebook’s scheduled end-of-life date. Chromebook一生…

C#将unix时间戳转换成.net的DateTime类型的代码

下面的内容是关于C#将unix时间戳转换成.net的DateTime类型的内容。 DateTime epoch new DateTime(1970,1,1,0,0,0,0, DateTimeKind.Utc);DateTime myDate epoch.AddSeconds(1258598728).toLocalTime(); 转载于:https://www.cnblogs.com/odsxe/p/10338494.html

【活动】AI人工智能技术沙龙 |杭州站

AI人工智能技术沙龙 |杭州站将于2018年3月3号在浙江杭州市文一西路1818-2号中国&#xff08;杭州&#xff09;人工智能小镇举办由袋鼠云、七牛云及“因特链”社区的老师为大家带来AI纯技术干货分享另有区块链和AI人工智能技术融合技术主题1活动安排时间&#xff1a;2018年3月3号…

如何在Google文档中的图片周围换行

If you want to insert an image or object into a document, it’s relatively simple. However, positioning and getting them to stay where you want can be frustrating. The wrap text feature in Google Docs makes all of this more manageable. 如果要将图像或对象插…

mysql的left函数

1、LEFT()函数是一个字符串函数&#xff0c;它返回具有指定长度的字符串的左边部分。 LEFT(Str,length); 接收两个参数&#xff1a; str&#xff1a;一个字符串&#xff1b; length&#xff1a;想要截取的长度&#xff0c;是一个正整数&#xff1b; 2、示例&#xff1a; SELECT…