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.pswp.cn/news/278395.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/278395.shtml
英文地址,請注明出處:http://en.pswp.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…