51 lines
1.2 KiB
C#
51 lines
1.2 KiB
C#
|
|
using System.Collections;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using TMPro;
|
||
|
|
using UnityEngine;
|
||
|
|
using UnityEngine.UI;
|
||
|
|
|
||
|
|
public class ModelHover : MonoBehaviour
|
||
|
|
{
|
||
|
|
private Outline outline; // 轮廓组件
|
||
|
|
private bool isOutlineEnabled = false; // 轮廓是否启用
|
||
|
|
public TextMeshPro displayText; // UI 文字组件
|
||
|
|
|
||
|
|
// Start is called before the first frame update
|
||
|
|
void Start()
|
||
|
|
{
|
||
|
|
outline = GetComponent<Outline>();
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
void OnMouseEnter()
|
||
|
|
{
|
||
|
|
isOutlineEnabled = outline.enabled;
|
||
|
|
print("Mouse Enter");
|
||
|
|
outline.enabled = true;
|
||
|
|
if (displayText != null)
|
||
|
|
{
|
||
|
|
displayText.gameObject.SetActive(true);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void OnMouseExit()
|
||
|
|
{
|
||
|
|
if (!isOutlineEnabled)
|
||
|
|
{
|
||
|
|
outline.enabled = false;
|
||
|
|
}
|
||
|
|
if (displayText != null)
|
||
|
|
{
|
||
|
|
displayText.gameObject.SetActive(false);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Update is called once per frame
|
||
|
|
void Update()
|
||
|
|
{
|
||
|
|
// 将 UI 文字跟随鼠标移动
|
||
|
|
/*Vector3 mousePosition = Input.mousePosition;
|
||
|
|
displayText.transform.position = new Vector3(mousePosition.x, mousePosition.y + 20, 0);*/ // 在鼠标上方显示,稍微偏移以避免遮挡
|
||
|
|
}
|
||
|
|
}
|