背包系统中,交换两个物品的位置

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;//导包,控制UI
using UnityEngine.EventSystems;//导包,使用unity事件系统


/// <summary>
/// 物品拖动,注意要将储物格的tag设为【 BagGrid 】
/// </summary>
public class Bag : MonoBehaviour ,IBeginDragHandler,IDragHandler,IEndDragHandler
{
    //这个代码 一定要挂在拖动的对象身上 
    //保存初始位置
    Vector2 initPos;
    //保存拖动前的层级
    int hrc;
    RectTransform rtf;
    //保存拖动前的父对象
    Transform parent;
    void Start()
    {
        hrc = this.transform.GetSiblingIndex();
        rtf = this.transform as RectTransform;//转换
    }
    /// <summary>
    /// 鼠标刚点击开始拖动
    /// </summary>
    /// <param name="eventData"></param>
    public void OnBeginDrag(PointerEventData eventData)
    {
        this.setBlockRayCast(false);//当开始拖动的时候开启穿透
        initPos = rtf.position;
        //解决层级问题,
        this.transform.SetSiblingIndex(100);
    }
    /// <summary>
    /// 拖动中
    /// </summary>
    /// <param name="eventData"></param>
    public void OnDrag(PointerEventData eventData)
    {
        //在拖动过程中设置父对象为Canvas
        //this.transform.SetParent(GameObject.Find("Canvas").transform);
        Vector3 vect;
        //将屏幕空间转换为在给定的RectTransform平面上的世界空间中的位置。
        bool f = RectTransformUtility.ScreenPointToWorldPointInRectangle(rtf, eventData.position, eventData.pressEventCamera, out vect);
        if (f)
        {
            rtf.position = vect;
        }
    }
    /// <summary>
    /// 拖动结束,松开鼠标
    /// </summary>
    /// <param name="eventData"></param>
    public void OnEndDrag(PointerEventData eventData)
    {
        GameObject obj = eventData.pointerEnter;
        if (obj != null)
        {
            if (obj.tag == "BagGrid")
            {
                Debug.Log("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1");
                this.transform.SetParent(obj.transform);
                this.transform.localPosition = Vector3.zero;
            }
            else if (obj.tag == "middleGoods" && this.tag == "middleGoods")
            {
                Debug.Log("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!2");
                parent = this.transform.parent;
                this.transform.SetParent(obj.transform.parent);
                obj.transform.SetParent(parent);
                //设置拖动物体RectTransform的localPosition为零
                rtf.localPosition = Vector3.zero;
                //设置交换物体RectTransform的localPosition为零
                (obj.transform as RectTransform).localPosition = Vector3.zero;
            }
            else
            {
                Debug.Log("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!3");
                rtf.position = initPos;
            }
        }
        else
        {
            Debug.Log("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!4");
             rtf.position = initPos;
        }
        setBlockRayCast(true);
        //this.transform.SetSiblingIndex(hrc);
        Debug.Log(this.transform.GetSiblingIndex());
    }
    /// <summary>
    /// 设置开启穿透
    /// </summary>
    /// <param name="isRayCaset"></param>
    public void setBlockRayCast(bool isRayCaset)
    {
        CanvasGroup CG = null;
        if (this.gameObject.GetComponent<CanvasGroup>() == null)
        {
            CG = this.gameObject.AddComponent<CanvasGroup>();
        }
        else
        {
            CG = this.gameObject.GetComponent<CanvasGroup>();
        }
        //ture:不穿透,false:穿透
        CG.blocksRaycasts = isRayCaset;
    }
}

猜你喜欢

转载自blog.csdn.net/wk201403010114/article/details/78321987