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
? ? }
}文章來源地址http://www.zghlxwxcb.cn/news/detail-751830.html
到了這里,關(guān)于unity中 使用IDragHandler拖動交換子物體的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!