• 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. }

  • 相关阅读:
    【一步到位】Jenkins的安装、部署、启动(完整教程)
    蔚蓝资源包和数据分析
    Linux命令篇(二):文档编辑部分
    破除“数据孤岛”新策略:Data Fabric(数据编织)和逻辑数据平台
    交换机与路由器技术-04-远程管理交换机
    【CSS】问题:为什么我的z-index不起作用
    机械转码日记【23】模板进阶
    【数据结构】ArrayList与顺序表
    记一次 .NET 某传感器采集系统 线程爆高分析
    正则表达式
  • 原文地址:https://blog.csdn.net/Starry_error/article/details/127729856