• Unity 鼠标拖拽功能


    继承3个处理拖拽方法的接口IBeginDragHandler, IDragHandler, IEndDragHandler

    1. ///
    2. /// 实现接口的OnBeginDrag方法,处理开始拖拽时要做的事情
    3. ///
    4. ///
    5. public void OnBeginDrag(PointerEventData eventData)
    6. {
    7. startPosition = transform.position;//获取初始位置
    8. imageFather = transform.parent;//获取父物体的transform
    9. transform.SetParent(canvas.transform);//将物品放在canvas最下方,确保不会被遮挡
    10. canvasGroup.blocksRaycasts = false;//射线可以穿透物体
    11. }
    1. ///
    2. /// 实现接口的OnDrag方法,处理拖动中要做的事情
    3. ///
    4. ///
    5. public void OnDrag(PointerEventData eventData)
    6. {
    7. transform.position = screenPositionToWorldPositionBy_Z(Input.mousePosition, transform.position);
    8. }

    1. ///
    2. /// 通过基准点的Z点坐标,转化屏幕坐标为世界坐标
    3. ///
    4. /// 需要转化的屏幕坐标
    5. /// 基准点(需要它的z点坐标做转换)
    6. Vector3 screenPositionToWorldPositionBy_Z(Vector3 needToFixedScreenPosition, Vector3 baseWorldPosition)
    7. {
    8. Vector3 baseScreenPosition = Camera.main.WorldToScreenPoint(baseWorldPosition);
    9. needToFixedScreenPosition.z = baseScreenPosition.z;
    10. return Camera.main.ScreenToWorldPoint(needToFixedScreenPosition);
    11. }

     

    1. ///
    2. /// 实现接口的OnEndDrag方法,处理结束时的方法
    3. ///
    4. ///
    5. public void OnEndDrag(PointerEventData eventData )
    6. {
    7. //获取鼠标下的物体
    8. GameObject go = eventData.pointerCurrentRaycast.gameObject;
    9. //交换物品
    10. if (go.tag.Equals(Enum_Tags.InventoryItemGird)) ExchangeOfGoods(go);
    11. //复原
    12. else
    13. {
    14. transform.SetParent(imageFather);
    15. transform.position = startPosition;
    16. }
    17. canvasGroup.blocksRaycasts = true;//ui事件穿透:置为不能穿透
    18. }
    1. ///
    2. /// 将一个物体放在另一个物体下
    3. ///
    4. /// 作为子物体
    5. /// 作为父物体
    6. private void SetParentAndPosition(Transform child, Transform parent)
    7. {
    8. child.SetParent(parent);
    9. child.position = parent.position;
    10. }
    1. ///
    2. /// 格子内的物品进行交换
    3. ///
    4. /// 鼠标结束位置的格子
    5. private void ExchangeOfGoods(GameObject go)
    6. {
    7. Transform[] childrens = go.GetComponentsInChildren();
    8. bool isHaveChildren= childrens.Length > 1;
    9. if (isHaveChildren)
    10. {
    11. Transform child = go.transform.GetChild(0);
    12. SetParentAndPosition(child, imageFather);
    13. }
    14. SetParentAndPosition(gameObject.transform, go.transform);
    15. }
    1. Vector3 screenPositionToWorldPositionBy_Z(Vector3 needToFixedScreenPosition, Vector3 baseWorldPosition)
    2. {
    3. Vector3 baseScreenPosition = Camera.main.WorldToScreenPoint(baseWorldPosition);
    4. needToFixedScreenPosition.z = baseScreenPosition.z;
    5. return Camera.main.ScreenToWorldPoint(needToFixedScreenPosition);
    6. }

  • 相关阅读:
    【论文阅读】SPARK:针对视觉跟踪的空间感知在线增量攻击
    k8s-部署Redis-cluster(TLS)
    开关电源测试解决方案之浪涌电流测试 -纳米软件
    一篇文章讲清楚!数据库和数据仓库到底有什么区别和联系?
    【luckfox】添加压力传感器hx711
    【数学建模】MATLAB应用实战系列(八十八)-组合权重法应用案例(附MATLAB和Python代码)
    C语言从入门到精通- CLion安装配置Gitee
    【PyTorch深度学习项目实战100例】—— 基于MFCC对GTZAN音乐流派分类 | 第66例
    数据库系统概论简答题带答案
    秒杀官方实现,python界面库,去掉90%事件代码的nicegui
  • 原文地址:https://blog.csdn.net/Starry_error/article/details/127729856