物体拖拽效果OnDrag

[csharp]  view plain  copy
 print ?
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using UnityEngine.UI;  
  4. using UnityEngine.EventSystems;  
  5.   
  6. public class PetItemMove : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, ICanvasRaycastFilter  
  7. {  
  8.     private static Transform canvasTra;  
  9.     private Transform nowParent;//物品是格子的子物体,nowParent记录的是当前物品属于哪个格子(格子1)  
  10.     private bool isRaycastLocationValid = true;//默认射线不能穿透物品  
  11.     private Camera canvasCamera = null;  
  12.     void Start()  
  13.     {  
  14.   
  15.     }  
  16.   
  17.     public void OnBeginDrag(PointerEventData eventData)//开始拖拽  
  18.     {  
  19.         if (canvasTra == null) canvasTra = GameObject.Find("Canvas").transform;  
  20.         canvasCamera = canvasTra.GetComponentInChildren<Camera>();  //UICamera
  21.         //Debug.Log(Input.mousePosition);  
  22.         nowParent = transform.parent;//nowparent为被拖拽物体的原始父物体  
  23.         transform.SetParent(canvasTra);//将当前拖拽的物品放在canvas下  
  24.         isRaycastLocationValid = false;//ui穿透:置为可以穿透  【拖拽物体移动的时候鼠标下是有物体一直跟随遮挡的,如果不穿透就获取不到放置位置(OnEndDrag中判断是空格子,物体,还是无效位置)】  
  25.   
  26.     }  
  27.   
  28.     public void OnDrag(PointerEventData eventData)//拖拽过程中  
  29.     {  
  30.        transform.position = canvasCamera.ScreenToWorldPoint(Input.mousePosition);//鼠标左键按住拖拽的时候,物体跟着鼠标移动  
  31.         //transform.position = Input.mousePosition;//鼠标左键按住拖拽物体的时候不显示物体  
  32.   
  33.     }  
  34.   
  35.     public void OnEndDrag(PointerEventData eventData)//  
  36.     {  
  37.         GameObject go = eventData.pointerCurrentRaycast.gameObject;//获取到鼠标终点位置下 可能的物体  
  38.   
  39.           
  40.         if (go != null)  
  41.         {  
  42.             Debug.Log(go.name);  
  43.             if (go.tag.Equals("Grid"))//鼠标终点位置下是: 空格子(所以直接放进去)  
  44.             {  
  45.                 SetParentAndPosition(transform, go.transform);  
  46.             }  
  47.             else if (go.tag.Equals("PlayerPetItem"))//鼠标终点位置下是: 也是一个物体(所以需要交换位置)  
  48.             {//交换位置要注意可能需要把物品下的子物体的Raycast Target关掉(不去掉可能无法交换)  
  49.                 SetParentAndPosition(transform, go.transform.parent);//将被拖拽的物体1放到鼠标终点下的格子2里面  
  50.                 SetParentAndPosition(go.transform, nowParent);//将鼠标终点格子2里面物体2 放到 原来物体1的格子1里面  
  51.                 if (transform.position == go.transform.position)  
  52.                 {  
  53.                     Debug.Log("error");  
  54.                 }  
  55.             }  
  56.             else//鼠标终点是:无效位置(所以物体放回原来的位置)  
  57.             {  
  58.                 SetParentAndPosition(transform, nowParent);  
  59.             }  
  60.         }  
  61.         else//  
  62.         {  
  63.             SetParentAndPosition(transform, nowParent);  
  64.         }  
  65.         isRaycastLocationValid = true;//ui事件穿透:置为不能穿透  
  66.     }  
  67.   
  68.     private void SetParentAndPosition(Transform child, Transform parent)//将child放到parent下作为子物体  
  69.     {  
  70.         child.SetParent(parent);  
  71.         child.position = parent.position;  
  72.     }  
  73.   
  74.     public bool IsRaycastLocationValid(Vector2 sp, Camera eventCamera)//UI事件穿透:如置为false即可以穿透,被图片覆盖的按钮可以被点击到  
  75.     {  
  76.         return isRaycastLocationValid;  
  77.     }  
  78. }  
 
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值