当前位置:   article > 正文

unity中UI组件跟随鼠标移动_unity ui跟随鼠标移动

unity ui跟随鼠标移动

给要移动的UI组件挂载

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. public class DragByInterface : MonoBehaviour,IDragHandler,IBeginDragHandler,IEndDragHandler//接口 鼠标拖拽中 开始拖拽 结束拖拽 用于UI界面
  6. {
  7. RectTransform rectTransform;//ui位置
  8. CanvasGroup canvasGroup;
  9. void Start()
  10. {
  11. rectTransform=GetComponent<RectTransform>();
  12. canvasGroup = GetComponent<CanvasGroup>();
  13. }
  14. public void OnBeginDrag(PointerEventData eventData)
  15. {
  16. canvasGroup.blocksRaycasts= false;//射线检测关闭
  17. }
  18. public void OnDrag(PointerEventData eventData)
  19. {
  20. //位置跟着鼠标动
  21. rectTransform.anchoredPosition+=eventData.delta;
  22. }
  23. public void OnEndDrag(PointerEventData eventData)
  24. {
  25. canvasGroup.blocksRaycasts= true;//射线检测开始
  26. }
  27. public void DebugLogObject()
  28. {
  29. Debug.Log("拖draw");
  30. }
  31. }

选中的UI显示在最上方

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. public class DragWindow : MonoBehaviour, IDragHandler,IBeginDragHandler
  6. {
  7. RectTransform rectTransform;//ui位置
  8. void Start()
  9. {
  10. rectTransform=GetComponent<RectTransform>();
  11. }
  12. public void OnDrag(PointerEventData eventData)
  13. {
  14. //位置跟着鼠标动
  15. rectTransform.anchoredPosition+=eventData.delta;//
  16. }
  17. public void OnBeginDrag(PointerEventData eventData)
  18. {
  19. rectTransform.SetAsLastSibling();//将渲染等级移动到最后面 显示在最前面
  20. }
  21. }

移动世界坐标系中的物体

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Draw2DSprite : MonoBehaviour
  5. {
  6. SpriteRenderer spriteRenderer;
  7. int startNmber;
  8. private void Awake()
  9. {
  10. spriteRenderer= GetComponent<SpriteRenderer>();
  11. startNmber= spriteRenderer.sortingOrder;
  12. }
  13. /// <summary>
  14. ///
  15. /// 鼠标事件要有Collier才行
  16. /// </summary>
  17. private void OnMouseDrag()//unity内置鼠标拖拽 用于2d,和3d场景
  18. {
  19. //缺少z轴,会移动位置不对
  20. // Vector2 cursorPor = Camera.main.ScreenToViewportPoint(Input.mousePosition);//将鼠标屏幕坐标转为世界坐标
  21. // transform.position=new Vector2(cursorPor.x, cursorPor.y);
  22. //获取需要移动物体的世界转屏幕坐标
  23. Vector3 screenPos = Camera.main.WorldToScreenPoint(this.transform.position);
  24. //获取鼠标位置
  25. Vector3 mousePos = Input.mousePosition;
  26. //因为鼠标只有X,Y轴,所以要赋予给鼠标Z轴
  27. mousePos.z = screenPos.z;
  28. //把鼠标的屏幕坐标转换成世界坐标
  29. Vector3 worldPos = Camera.main.ScreenToWorldPoint(mousePos);
  30. //控制物体移动
  31. transform.position = worldPos;
  32. }
  33. private void OnMouseEnter()//unity内置鼠标进入时
  34. {
  35. transform.localScale+= Vector3.one*0.07f;//放大物品缩放
  36. spriteRenderer.sortingOrder=10;//渲染顺序排到10级 比1级 显示在屏幕前方
  37. }
  38. void OnMouseExit()
  39. {
  40. transform.localScale-= Vector3.one*0.07f;//减小物品缩放
  41. spriteRenderer.sortingOrder=startNmber;
  42. }
  43. }

UI组件接受其他物体的信息

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using UnityEngine.UI;
  6. public class Slot : MonoBehaviour, IDropHandler//检测拖拽事件(OnDrag)放下的操作
  7. {
  8. public void OnDrop(PointerEventData eventData)
  9. {
  10. Debug.Log("获得物体信息");
  11. // eventData.pointerDrag. 获得拖拽物体信息
  12. eventData.pointerDrag.GetComponent<RectTransform>().anchoredPosition=GetComponent<RectTransform>().anchoredPosition;
  13. eventData.pointerDrag.GetComponent<DragByInterface>().DebugLogObject();
  14. }
  15. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/111378
推荐阅读
相关标签
  

闽ICP备14008679号