效果展示:
使用方法:
将以下代码保存到一个新的脚本里,然后将该脚本拖动到需要显示提示浮窗的UI元素上,之后在检查器中输入响应的文本即可。
本方法无需其他操作,所有步骤均在代码中实现,直接拖入脚本即可。
代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using UnityEngine.EventSystems;
//弹出一个说明性文字的显示浮窗
public class IndicativeWindow : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerMoveHandler
{
// Start is called before the first frame update
[SerializeField]
[Tooltip("将要显示的文字输入在这里")]
private string text;
[SerializeField]
[Tooltip("期望的最大宽度")]
private float maxWidth = 250f; // 期望的最大宽度
[SerializeField]
[Tooltip("背景不透明度")]
[Range(0, 1)]
private float 不透明度 = 0.5f;
[SerializeField]
private TextAnchor alignment = TextAnchor.MiddleCenter; //字体对齐
private GameObject tooltip;
private float Px, Py;
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void OnPointerEnter(PointerEventData eventData)
{
tooltip = new GameObject("说明性浮窗");
// 添加 Canvas 组件,并设置渲染模式为 Overlay
Canvas canvas = tooltip.AddComponent<Canvas>();
canvas.renderMode = RenderMode.ScreenSpaceOverlay;
// 获取或添加 GraphicRaycaster 组件,确保说明性浮窗可以响应鼠标事件
//tooltip.AddComponent<UnityEngine.UI.GraphicRaycaster>();
RectTransform rt = tooltip.GetComponent<RectTransform>();
if (rt != null)
{
rt.anchorMin = new Vector2(1, 1); // 左上角的锚点坐标为(0, 1)
rt.anchorMax = new Vector2(1, 1); // 左上角的锚点坐标为(0, 1)
rt.anchoredPosition = new Vector3(0, 0, 0);
//rt.pivot = new Vector2(0, 1); // 设置中心点位置,这一步可选
}
// 设置新GameObject的父对象为当前对象
tooltip.transform.SetParent(transform);
//tooltip.transform.position = new Vector3(Input.mousePosition.x + 250, Input.mousePosition.y - 250, 0); // 设置浮窗位置
//tooltip.layer = 0; //设置渲染层级
// 添加 Image 组件到新创建的 GameObject 上
Image newBackground = tooltip.AddComponent<Image>();
newBackground.sprite = Resources.Load<Sprite>("Backgronud");
//newBackground.rectTransform.sizeDelta = new Vector2(500f, 500f); // 设置大小为 100x100 像素(可根据需要修改)
newBackground.rectTransform.localPosition = Vector3.zero; // 设置位置为父物体中心(可根据需要修改)
// 设置透明度,alpha 值为 0.5,即半透明
Color color = newBackground.color;
color.a = 不透明度;
newBackground.color = color;
// 创建一个新的 GameObject 作为 Text 的容器
GameObject TextObject = new GameObject("Text");
// 将新创建的 GameObject 设为tooltip的子物体
TextObject.transform.parent = tooltip.transform;
// 在新GameObject上添加Text组件
Text newText = TextObject.AddComponent<Text>();
// 设置Text组件的内容
//newText.font = Resources.Load<Font>("Arial.ttf"); //这个好像没用
newText.font = Font.CreateDynamicFontFromOSFont("宋体", 24);
newText.text = text;
newText.color = Color.black;
newText.fontSize = 23;
newText.alignment = alignment;
// 设置框的大小(RectTransform)
RectTransform textRect = newText.GetComponent<RectTransform>();
//textRect.sizeDelta = new Vector2(500, 500); // 设置宽度和高度
// 计算文本的渲染长度
float textWidth = newText.preferredWidth;
// 调整 Text 组件的大小
Px = textWidth > maxWidth ? maxWidth : textWidth;
Py = textWidth / maxWidth * 33f;
newText.rectTransform.sizeDelta = new Vector2(Px, Py);
Py = newText.preferredHeight;
newText.rectTransform.sizeDelta = new Vector2(Px, Py);
newBackground.rectTransform.sizeDelta = new Vector2(Px * 1.1f, Py + 20f); //设置背景大小
textRect.localPosition = Vector3.zero; // 设置位置为父物体中心
// 设置 Canvas 的 overrideSorting 属性为 true,确保其可以覆盖其他 Canvas 的排序
canvas.overrideSorting = true;
// 设置 Canvas 的 sortingOrder 为一个较大的值,确保其始终在其他 Canvas 上方渲染
canvas.sortingOrder = 9999;
}
public void OnPointerMove(PointerEventData eventData)
{
tooltip.transform.position = new Vector3(Input.mousePosition.x + Px / 1.7f, Input.mousePosition.y - Py / 2, 0); // 设置浮窗位置
}
public void OnPointerExit(PointerEventData eventData)
{
if (tooltip != null)
{
Destroy(tooltip);
}
}
public void OnDisable()
{
// 在组件被设为非激活时执行的代码
if (tooltip != null)
{
Destroy(tooltip);
}
}
}