3D モデル上にマウスを置くと、Unity に導入パネルが表示されます。

1. 3D モデル上にマウスを置くと、導入パネルが表示されます。

1. 新しい Cube モデルを作成し、新しい画像パネルを作成します

2. スクリプト OnPointerEnterShow3D.cs を作成し、モデルにスクリプトをマウントします。
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. 次のように実行します

 

 

2. マウスを UI 上に置くと、導入パネルが表示されます。

1. 新しい UI 構造を作成する

2. スクリプト OnPointerEnterShowUI.cs を作成し、マウス ホバーが必要な UI にスクリプトをマウントします。
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. 次のように実行します

 

おすすめ

転載: blog.csdn.net/qq_2633600317/article/details/131853036