鼠标移上3D物体的函数:

具体实现代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShuBiaoYiRuAndYiChu3D : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public GameObject leimu;
/// <summary>
/// 鼠标移入 物体消失
/// </summary>
public void OnMouseEnter()
{
leimu.SetActive(false);
}
/// <summary>
/// 鼠标移除 物体显示出来
/// </summary>
public void OnMouseExit()
{
leimu.SetActive(true);
}
}
鼠标移入UI物体的效果:

具体实现代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class ShuBiaoYiRuAndYiChuUI : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public GameObject leimu;
public void OnPointerEnter(PointerEventData eventData)
{
leimu.SetActive(true);
}
public void OnPointerExit(PointerEventData eventData)
{
leimu.SetActive(false);
}
}
本文详细介绍了在Unity游戏引擎中,如何使用C#脚本来实现鼠标与3D物体及UI元素的交互效果。包括当鼠标移入3D物体时使其消失,以及当鼠标移出时恢复显示;同时,也涵盖了鼠标与UI物体交互的实现,如鼠标进入UI区域时显示特定对象,鼠标离开时隐藏。这些技术对于创建动态和响应式的用户界面至关重要。
2万+

被折叠的 条评论
为什么被折叠?



