当前位置:   article > 正文

unity鼠标悬停在3D模型上时显示介绍面板_unity 鼠标悬停

unity 鼠标悬停

一、鼠标悬停在3D模型上时显示介绍面板

1.新建Cube模型,新建Image面板

2.编写脚本OnPointerEnterShow3D.cs,脚本挂载到模型上
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class OnPointerEnterShow3D : MonoBehaviour
  6. {
  7. //要显示的ui
  8. public RectTransform tip;
  9. void Update()
  10. {
  11. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  12. RaycastHit hit;
  13. bool raycast = Physics.Raycast(ray, out hit);
  14. if (raycast)
  15. {
  16. GameObject go = hit.collider.gameObject;
  17. if (go == this.gameObject)
  18. {
  19. tip.gameObject.SetActive(true);
  20. FollowMouse();
  21. }
  22. else
  23. {
  24. tip.gameObject.SetActive(false);
  25. Destory(go);
  26. }
  27. }
  28. else
  29. {
  30. tip.gameObject.SetActive(false);
  31. }
  32. }
  33. //ui位置跟随鼠标实时变化
  34. void FollowMouse()
  35. {
  36. tip.position = new Vector3(Input.mousePosition.x, Input.mousePosition.y + 80f, 0f);
  37. }
  38. }
3.运行如下

 

 

二、鼠标悬停在UI上时显示介绍面板

1.新建UI结构

2.编写脚本OnPointerEnterShowUI.cs,将脚本挂载到需要鼠标悬停的UI上
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. public class OnPointerEnterShowUI : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
  6. {
  7. public void OnPointerEnter(PointerEventData eventData)
  8. {
  9. Debug.Log("移入");
  10. transform.GetChild(1).gameObject.SetActive(true);
  11. }
  12. public void OnPointerExit(PointerEventData eventData)
  13. {
  14. Debug.Log("移出");
  15. transform.GetChild(1).gameObject.SetActive(false);
  16. }
  17. }
3.运行如下

 

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

闽ICP备14008679号