国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

unity中 使用IDragHandler拖動交換子物體

這篇具有很好參考價值的文章主要介紹了unity中 使用IDragHandler拖動交換子物體。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

using DG.Tweening;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using VRSightCheck.Scripts;

public class SwapPanel : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
? ? /// <summary>
? ? /// 是否已經(jīng)響應(yīng)拖拽
? ? /// </summary>
? ? public static bool isDrag = false;

? ? private List<RectTransform> items; ?// 子物體數(shù)組

? ? private int currentIndex = -1; ?// 當(dāng)前需要交換位置的子物體索引

? ? /// <summary>
? ? /// 開始點擊gameobject
? ? /// </summary>
? ? private GameObject mStartGo;

? ? /// <summary>
? ? /// 開始坐標(biāo)
? ? /// </summary>
? ? private Vector3 startPos = Vector3.zero;

? ? /// <summary>
? ? /// 結(jié)束點擊gameobject
? ? /// </summary>
? ? private GameObject mEndGo;

? ? /// <summary>
? ? /// 結(jié)束坐標(biāo)
? ? /// </summary>
? ? private Vector3 endPos = Vector3.zero;

? ? /// <summary>
? ? /// 是否正在移動
? ? /// </summary>
? ? private bool isMove = false;

? ? /// <summary>
? ? /// 移動時間
? ? /// </summary>
? ? private const float MOVE_TIME = 0.2f;

? ? /// <summary>
? ? /// 動畫
? ? /// </summary>
? ? private Tweener[] tws;

? ? /// <summary>
? ? /// 成功換位置事件
? ? /// </summary>
? ? private Action<GameObject, GameObject> ChangeSucAct;

? ? private void Awake()
? ? {
? ? ? ? items = new List<RectTransform>();
? ? ? ? currentIndex = -1;
? ? ? ? mStartGo = null;
? ? ? ? startPos = Vector3.zero;
? ? ? ? mEndGo = null;
? ? ? ? endPos = Vector3.zero;
? ? ? ? isMove = false;
? ? ? ? tws = new Tweener[3];
? ? ? ? ChangeSucAct = new Action<GameObject, GameObject>(ChangeSuc);
? ? }

? ? /// <summary>
? ? /// 清除子物體
? ? /// </summary>
? ? public void ClearItem()
? ? {
? ? ? ? items.Clear();
? ? }

? ? /// <summary>
? ? /// 添加子物體
? ? /// </summary>
? ? public void AddItem(RectTransform it)
? ? {
? ? ? ? items.Add(it);
? ? }

? ? /// <summary>
? ? /// 設(shè)置委托
? ? /// </summary>
? ? /// <param name="act"></param>
? ? public void SetChangeSucAct(Action<GameObject, GameObject> act)
? ? {
? ? ? ? ChangeSucAct = act;
? ? }

? ? public void OnBeginDrag(PointerEventData eventData)
? ? {
? ? ? ? //Debug.Log("OnBeginDrag=======================>");
? ? ? ? if (items.Count == 0 || isMove || isDrag) return;
? ? ? ? // 檢查觸摸點是否在另一個子物體上,并根據(jù)索引信息交換兩個子物體的位置
? ? ? ? List<RaycastResult> results = new List<RaycastResult>();
? ? ? ? EventSystem.current.RaycastAll(eventData, results);
? ? ? ? if (results.Count == 0) return;
? ? ? ? List<GameObject> gos = new List<GameObject>();
? ? ? ? for (int i = 0; i < results.Count; i++)
? ? ? ? {
? ? ? ? ? ? gos.Add(results[i].gameObject);
? ? ? ? }
? ? ? ? // 檢查觸摸點是否在其中一個子物體上
? ? ? ? for (int i = 0; i < items.Count; i++)
? ? ? ? {
? ? ? ? ? ? //if (RectTransformUtility.RectangleContainsScreenPoint(items[i], eventData.position, UICanvasManager.Instance.CameraCanvas))
? ? ? ? ? ? if(gos.Contains(items[i].gameObject))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? currentIndex = i;
? ? ? ? ? ? ? ? mStartGo = items[i].gameObject;
? ? ? ? ? ? ? ? startPos = items[i].position;

? ? ? ? ? ? ? ? isDrag = true;
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? }
? ? }

? ? public void OnDrag(PointerEventData eventData)
? ? {
? ? ? ? //Debug.Log("OnDrag=======================>");
? ? ? ? if (items.Count == 0 || isMove) return;
? ? ? ??
? ? ? ? if (currentIndex >= 0)
? ? ? ? {
? ? ? ? ? ? // 根據(jù)觸摸點的移動距離計算出當(dāng)前子物體應(yīng)該移動的位置
? ? ? ? ? ? Vector2 offset = eventData.delta / transform.localScale.x;
? ? ? ? ? ? if(items.Count > currentIndex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? items[currentIndex].anchoredPosition += offset;
? ? ? ? ? ? }
? ? ? ? }
? ? }

? ? public void OnEndDrag(PointerEventData eventData)
? ? {
? ? ? ? //Debug.Log("OnEndDrag=======================>");
? ? ? ? if (items.Count == 0 || isMove || mStartGo == null) return;
? ? ? ??
? ? ? ? for (int i = 0; i < items.Count; i++)
? ? ? ? {
? ? ? ? ? ? if (i != currentIndex && RectTransformUtility.RectangleContainsScreenPoint(items[i], eventData.position , UICanvasManager.Instance.CameraCanvas))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? mEndGo = items[i].gameObject;
? ? ? ? ? ? ? ? endPos = items[i].position;
? ? ? ? ? ? ? ? Change();
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? if (mEndGo == null)
? ? ? ? {
? ? ? ? ? ? MoveBack();
? ? ? ? }
? ? ? ? currentIndex = -1;

? ? ? ? isDrag = false;
? ? }

? ? /// <summary>
? ? /// 清除開始點擊gameObject
? ? /// </summary>
? ? void ClearStartGo()
? ? {
? ? ? ? if (mStartGo == null) return;
? ? ? ? mStartGo.transform.localScale = Vector3.one;
? ? ? ? mStartGo = null;
? ? }


? ? /// <summary>
? ? /// 未成功換位置,返回
? ? /// </summary>
? ? void MoveBack()
? ? {
? ? ? ? isMove = true;

? ? ? ? tws[0] = mStartGo.transform.DOMove(startPos, MOVE_TIME);

? ? ? ? tws[0].SetEase(Ease.Linear);
? ? ? ? tws[0].OnComplete( () =>{
? ? ? ? ? ? ClearStartGo();
? ? ? ? ? ? isMove = false;
? ? ? ? });

? ? ? ? if (mEndGo != null)
? ? ? ? {
? ? ? ? ? ? mEndGo = null;
? ? ? ? }
? ? }

? ? /// <summary>
? ? /// 替換位置
? ? /// </summary>
? ? void Change()
? ? {
? ? ? ? isMove = true;
? ? ? ? int idx = mStartGo.transform.GetSiblingIndex();
? ? ? ? mStartGo.transform.SetSiblingIndex(mEndGo.transform.GetSiblingIndex());
? ? ? ? mEndGo.transform.SetSiblingIndex(idx);

? ? ? ? tws[1] = mStartGo.transform.DOMove(endPos, MOVE_TIME);
? ? ? ? tws[1].SetEase(Ease.Linear);
? ? ? ? tws[2] = mEndGo.transform.DOMove(startPos, MOVE_TIME);
? ? ? ? tws[2].SetEase(Ease.Linear);
? ? ? ? tws[2].OnComplete(() => {
? ? ? ? ? ? Debug.Log("替換位置================>" + ChangeSucAct);
? ? ? ? ? ? ChangeSucAct(mStartGo, mEndGo);
? ? ? ? ? ? ClearStartGo();
? ? ? ? ? ? isMove = false;
? ? ? ? ? ? mEndGo = null;
? ? ? ? });
? ? }


? ? private void OnDestroy()
? ? {
? ? ? ? //停止動畫
? ? ? ? for (int i = 0; i < tws.Length; i++)
? ? ? ? {
? ? ? ? ? ? if (tws[i] != null) tws[i].Kill();
? ? ? ? }
? ? }


? ? private void ChangeSuc(GameObject arg1, GameObject arg2)
? ? {

? ? }
}文章來源地址http://www.zghlxwxcb.cn/news/detail-751830.html

到了這里,關(guān)于unity中 使用IDragHandler拖動交換子物體的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • unity拖拽背包物體,并交換兩個物體的信息

    unity拖拽背包物體,并交換兩個物體的信息

    前幾天看了一個教程,背包物品交換。由于教程中使用的是ngui,很多代碼需要使用UGUI重新寫,因此苦惱了幾天。 我遇到了以下問題,在此詳細(xì)羅列,以記錄錯誤的原因和修改的方法,供日后閱讀查找。 一、背包中的物品在拖拽之后放入指定的空格子(問題1:不知道該怎么

    2024年04月15日
    瀏覽(79)
  • 使用團(tuán)結(jié)引擎開發(fā)Unity 3D射擊游戲

    使用團(tuán)結(jié)引擎開發(fā)Unity 3D射擊游戲

    ? ? ? ?本案例是初級案例,意在引導(dǎo)想使用unity的初級開發(fā)者能較快的入門,體驗unity開發(fā)的方便性和簡易性能。 ? ? ? 本次我們將使用團(tuán)結(jié)引擎進(jìn)行開發(fā),幫助想體驗團(tuán)結(jié)引擎的入門開發(fā)者進(jìn)行較快的環(huán)境熟悉。 ? ? ?本游戲是一個俯視角度的射擊游戲。主角始終位于屏幕

    2024年01月19日
    瀏覽(110)
  • Unity 語法詳解之查找游戲物體的方法(含查找隱藏物體)

    Unity 語法詳解之查找游戲物體的方法(含查找隱藏物體)

    為了更好的看懂,有一個非?;A(chǔ)的知識,如果不知道可以移步去了解一下哦 unity | gameobject和transform的區(qū)別和關(guān)聯(lián)通俗解釋_gameobject transform_菌菌巧樂茲的博客-CSDN博客 一、前情提要 大寫的 GameObject是個類,里面寫滿了物體有關(guān)的代碼 小寫的 gameObject指的是物體本身 大寫的

    2024年02月12日
    瀏覽(22)
  • 【Unity3D】Unity 腳本 ③ ( C# 腳本的執(zhí)行入口函數(shù) | 獲取當(dāng)前游戲物體及物體名稱 | 獲取游戲物體的 Transform 組件數(shù)據(jù) | UnityEngine 命名空間簡介 )

    【Unity3D】Unity 腳本 ③ ( C# 腳本的執(zhí)行入口函數(shù) | 獲取當(dāng)前游戲物體及物體名稱 | 獲取游戲物體的 Transform 組件數(shù)據(jù) | UnityEngine 命名空間簡介 )

    在 C# 腳本中控制 游戲物體 GameObject 運(yùn)動 , 要先獲取該物體 , 然后 修改其 Transform 組件的屬性 ; 在 游戲開始運(yùn)行后 , 會自動執(zhí)行 游戲物體 GameObject 上的 C# 組件代碼 , 程序入口是 MonoBehaviour#Start() 函數(shù) ; 在 C# 腳本中 , 主要的內(nèi)容都在 Start() 函數(shù) 中實現(xiàn) ; 在 C# 腳本中 , 游戲物體

    2023年04月12日
    瀏覽(111)
  • Unity上接入手柄,手柄控制游戲物體移動

    Unity上接入手柄,手柄控制游戲物體移動

    1、unity軟件上安裝system input 組件。菜單欄【window】-【Packag Manager】打開如下界面,查找Input System,并且安裝。 2、安裝成功后插入手柄到windows上,打開菜單欄上【window】--【Analysis】--【Input Debuger】 進(jìn)入Input Debug界面,可以看到手柄設(shè)備能被Unity識別。 3、雙擊【XinputControllerW

    2024年04月15日
    瀏覽(27)
  • 【Unity】Transform—游戲物體的角度和旋轉(zhuǎn)
  • 解決Unity中UI的中心錨點無法拖動修改問題(筆記)

    解決Unity中UI的中心錨點無法拖動修改問題(筆記)

    提示:文章寫完后,目錄可以自動生成,如何生成可參考右邊的幫助文檔 記錄一下UI中容易忘記的細(xì)節(jié)。

    2024年02月08日
    瀏覽(20)
  • Unity3D實現(xiàn)UI的單擊、雙擊、拖動狀態(tài)判斷

    Unity3D實現(xiàn)UI的單擊、雙擊、拖動狀態(tài)判斷

    這篇文章就來實現(xiàn)UI的單擊、雙擊、按壓、拖動的不同狀態(tài)判斷。不定時更新Unity開發(fā)技巧,覺得有用記得一鍵三連哦。 示例、 判斷單擊和雙擊,主要是判斷點擊的次數(shù)。 UI的點擊事件,需要繼承UI的點擊事件接口,重寫點擊事件即可。 UI點擊事件接口: 3-1-1 所引用的命名空

    2024年01月20日
    瀏覽(95)
  • unity UI 跟隨3D物體移動

    unity UI 跟隨3D物體移動

    ?

    2024年02月11日
    瀏覽(31)
  • Unity UI不被3D物體遮擋

    Unity UI不被3D物體遮擋

    UI Shader: 用該Shader創(chuàng)建一個材質(zhì),將該材質(zhì)掛到不被模型遮擋的UI上。 TextMeshPro 設(shè)置: 修改TextMesh Pro/Shaders/TMP_SDF.shader 修改如下:

    2024年02月07日
    瀏覽(36)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包