UGUI的中心拖拽和精準(zhǔn)拖拽

參考鏈接:https://www.cnblogs.com/mrmocha/p/8040649.html;



/*************************************************

* 項(xiàng)目名稱(chēng):UGUI通用

* 腳本創(chuàng)建人:魔卡

* 腳本創(chuàng)建時(shí)間:2017.12.14

* 腳本功能:UI圖片拖拽功能(將腳本掛載在需要拖放的圖片上)

* ***********************************************/

using UnityEngine;

using System.Collections;

using UnityEngine.EventSystems;

//UI圖片拖拽功能類(lèi)

public class UIDragByMocha : MonoBehaviour,IBeginDragHandler, IDragHandler, IEndDragHandler

{

? ? [Header( "是否精準(zhǔn)拖拽")]

? ? public bool m_isPrecision;

? ? //存儲(chǔ)圖片中心點(diǎn)與鼠標(biāo)點(diǎn)擊點(diǎn)的偏移量

? ? private Vector3 m_offset;

? ? //存儲(chǔ)當(dāng)前拖拽圖片的RectTransform組件

? ? private RectTransform m_rt;

? ? void Start()

? ? {

? ? ? ? //初始化

? ? ? ? m_rt= gameObject.GetComponent<RectTransform>();

? ? }

? ? //開(kāi)始拖拽觸發(fā)

? ? public void OnBeginDrag(PointerEventData eventData)

? ? {

? ? ? ? //如果精確拖拽則進(jìn)行計(jì)算偏移量操作

? ? ? ? if (m_isPrecision)

? ? ? ? {

? ? ? ? ? ? // 存儲(chǔ)點(diǎn)擊時(shí)的鼠標(biāo)坐標(biāo)

? ? ? ? ? ? Vector3 tWorldPos;

? ? ? ? ? ? //UI屏幕坐標(biāo)轉(zhuǎn)換為世界坐標(biāo)

? ? ? ? ? ? RectTransformUtility.ScreenPointToWorldPointInRectangle(m_rt, eventData.position, eventData.pressEventCamera, out tWorldPos);

? ? ? ? ? ? //計(jì)算偏移量?

? ? ? ? ? ? m_offset = transform.position - tWorldPos;

? ? ? ? }

? ? ? ? ? ? //否則,默認(rèn)偏移量為0

? ? ? ? else

? ? ? ? {

? ? ? ? ? ? m_offset = Vector3.zero;

? ? ? ? }

? ? ? ? SetDraggedPosition(eventData);

? ? }

? ? //拖拽過(guò)程中觸發(fā)

? ? public void OnDrag(PointerEventData eventData)

? ? {

? ? ? ? SetDraggedPosition(eventData);

? ? }

? ? //結(jié)束拖拽觸發(fā)

? ? public void OnEndDrag(PointerEventData eventData)

? ? {

? ? ? ? SetDraggedPosition(eventData);

? ? }

? ? /// <summary>

? ? /// 設(shè)置圖片位置方法

? ? /// </summary>

? ? /// <param name="eventData"></param>

? ? private void SetDraggedPosition(PointerEventData eventData)

? ? {

? ? ? ? //存儲(chǔ)當(dāng)前鼠標(biāo)所在位置

? ? ? ? Vector3 globalMousePos;

? ? ? ? //UI屏幕坐標(biāo)轉(zhuǎn)換為世界坐標(biāo)

? ? ? ? if (RectTransformUtility.ScreenPointToWorldPointInRectangle(m_rt, eventData.position, eventData.pressEventCamera, out globalMousePos))

? ? ? ? {

? ? ? ? ? ? //設(shè)置位置及偏移量

? ? ? ? ? ? m_rt.position = globalMousePos + m_offset;

? ? ? ? }

? ? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • // Copyright ? 2018, Meta Company. All rights reserved. ...
    LunarShade閱讀 290評(píng)論 0 0
  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom閱讀 3,170評(píng)論 0 3
  • 使用Unity完成簡(jiǎn)單的背包功能 用UGUI布局成一下這個(gè)樣子 設(shè)置BackGround的tag為Bag Lft,...
    Unity學(xué)習(xí)的路上閱讀 6,226評(píng)論 1 4
  • 城東一角,遇見(jiàn)自樂(lè)班鳴鑼開(kāi)場(chǎng),駐足聆聽(tīng)!昔年還為秦腔傳承擔(dān)心,隨著閱歷豐富感觸深刻,愈發(fā)覺(jué)得只要有秦人秦地,就有秦...
    符成方閱讀 743評(píng)論 0 1
  • 經(jīng)過(guò)多年的努力,A國(guó)和B國(guó)終于確立了正式的外交關(guān)系,為此,A國(guó)全國(guó)放假三天。為了表示對(duì)B國(guó)的誠(chéng)意,A國(guó)決定送B...
    風(fēng)中豆蔻閱讀 599評(píng)論 0 0

友情鏈接更多精彩內(nèi)容