Unity displays the introduction panel when the mouse is hovering over the 3D model

1. Display the introduction panel when the mouse is hovering over the 3D model

1. Create a new Cube model and create a new Image panel

2. Write the script OnPointerEnterShow3D.cs and mount the script on the model
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class OnPointerEnterShow3D : MonoBehaviour
{
    //要显示的ui
    public RectTransform tip;

    void Update()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        bool raycast = Physics.Raycast(ray, out hit);
        if (raycast)
        {
            GameObject go = hit.collider.gameObject;
            if (go == this.gameObject)
            {
                tip.gameObject.SetActive(true);
                FollowMouse();
            }
            else
            {
                tip.gameObject.SetActive(false);
                Destory(go);
            }

        }
        else
        {
            tip.gameObject.SetActive(false);
        }

    }

    //ui位置跟随鼠标实时变化
    void FollowMouse()
    {
        tip.position = new Vector3(Input.mousePosition.x, Input.mousePosition.y + 80f, 0f);
    }

}
3. Run as follows

 

 

2. Display the introduction panel when the mouse is hovering over the UI

1. Create a new UI structure

2. Write the script OnPointerEnterShowUI.cs and mount the script to the UI that requires mouse hovering.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class OnPointerEnterShowUI : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{

    public void OnPointerEnter(PointerEventData eventData)
    {
        Debug.Log("移入");
        transform.GetChild(1).gameObject.SetActive(true);
    }
    public void OnPointerExit(PointerEventData eventData)
    {
        Debug.Log("移出");
        transform.GetChild(1).gameObject.SetActive(false);
    }
}
3. Run as follows

 

Guess you like

Origin blog.csdn.net/qq_2633600317/article/details/131853036