using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.UIElements;
#if ENABLE_INPUT_SYSTEM && UNITY_INPUT_SYSTEM_PACKAGE
using UnityEngine.InputSystem;
#endif
namespace MuHua {
///
/// UI工具
///
public static class UITool {
/// 获取鼠标位置
public static Vector3 GetMousePosition() {
#if ENABLE_INPUT_SYSTEM && UNITY_INPUT_SYSTEM_PACKAGE
return Mouse.current.position.ReadValue();
#else
return Input.mousePosition;
#endif
}
/// 获取鼠标位置(元素中的鼠标位置)
public static Vector3 GetMousePosition(VisualElement element) {
Vector3 mousePosition = GetMousePosition();
float offsetX = mousePosition.x / Screen.width;
float offsetY = mousePosition.y / Screen.height;
float x = element.resolvedStyle.width * offsetX;
float y = element.resolvedStyle.height * (1 - offsetY);
return new Vector3(x, y);
}
/// 富文本: 颜色
public static string RichTextColor(string value, Color color) {
string hexRGBA = ColorUtility.ToHtmlStringRGBA(color);
return $"{value}";
}
/// 富文本: 下划线
public static string RichTextUnderline(string value) {
return $"{value}";
}
/// 排除富文本
public static string RemoveRichText(string str) {
return Regex.Replace(str, "<.*?>", "");
}
/// 判断 localPosition 是否在 string 上
public static bool EnterString(this Label label, string str, Vector2 localPosition) {
string rawText = RemoveRichText(label.text);
int startIndex = rawText.IndexOf(str);
if (startIndex == -1) return false;
// 计算起始位置宽度
float startWidth = label.MeasureTextSize(
rawText.Substring(0, startIndex),
label.resolvedStyle.width,
VisualElement.MeasureMode.Undefined,
label.resolvedStyle.height,
VisualElement.MeasureMode.Undefined
).x;
// 计算目标字符串总宽度
float targetWidth = label.MeasureTextSize(
str,
label.resolvedStyle.width,
VisualElement.MeasureMode.Undefined,
label.resolvedStyle.height,
VisualElement.MeasureMode.Undefined
).x;
Rect rect = new Rect(startWidth, 0, targetWidth, label.resolvedStyle.height);
return rect.Contains(localPosition);
}
}
}