286 lines
8.7 KiB
C#
286 lines
8.7 KiB
C#
![]() |
using System.Collections.Generic;
|
|||
|
using UnityEngine;
|
|||
|
using System.Text.RegularExpressions;
|
|||
|
using System.Text;
|
|||
|
|
|||
|
namespace GameLogic
|
|||
|
{
|
|||
|
public static class UITool
|
|||
|
{
|
|||
|
private static int hours;
|
|||
|
private static int minutes;
|
|||
|
private static int seconds;
|
|||
|
|
|||
|
public static string TextName(string value)
|
|||
|
{
|
|||
|
if (value.Length > 6)
|
|||
|
{
|
|||
|
value = value.Substring(0, 6);
|
|||
|
value += "...";
|
|||
|
}
|
|||
|
return value;
|
|||
|
}
|
|||
|
/// <summary>
|
|||
|
/// 数值转换
|
|||
|
/// </summary>
|
|||
|
/// <param name="number"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public static string ConvertToString(int number)
|
|||
|
{
|
|||
|
//如果数字小于10000,直接返回原始数字
|
|||
|
if (number < 10000)
|
|||
|
{
|
|||
|
return number.ToString();
|
|||
|
}
|
|||
|
else if (number < 100000000)
|
|||
|
{
|
|||
|
//将数字除以10000并将格式化为字符串
|
|||
|
float convertedValue = (float)number / 10000;
|
|||
|
return string.Format("{0}万", convertedValue.ToString("F1"));
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
//将数字除以100000000并将格式化为字符串
|
|||
|
float convertedValue = (float)number / 100000000;
|
|||
|
return string.Format("{0}亿", convertedValue.ToString("F1"));
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 数值转换
|
|||
|
/// </summary>
|
|||
|
/// <param name="number"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public static string ConvertToString(long number)
|
|||
|
{
|
|||
|
//应该没有负数要求转换的吧....
|
|||
|
if (number <= 0)
|
|||
|
{
|
|||
|
return "0";
|
|||
|
}
|
|||
|
//如果数字小于10000,直接返回原始数字
|
|||
|
if (number < 10000)
|
|||
|
{
|
|||
|
return number.ToString();
|
|||
|
}
|
|||
|
else if (number < 100000000)
|
|||
|
{
|
|||
|
//将数字除以10000并将格式化为字符串
|
|||
|
float convertedValue = (float)number / 10000;
|
|||
|
return string.Format("{0}万", convertedValue.ToString("F1"));
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
//将数字除以100000000并将格式化为字符串
|
|||
|
float convertedValue = (float)number / 100000000;
|
|||
|
return string.Format("{0}亿", convertedValue.ToString("F1"));
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public static Color ColorFromString(string colorStr)
|
|||
|
{
|
|||
|
if (ColorUtility.TryParseHtmlString(colorStr, out Color color))
|
|||
|
{
|
|||
|
return color;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
Debug.LogWarning("Invalid color string: " + colorStr);
|
|||
|
return Color.white; // 默认颜色
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 转换时间
|
|||
|
/// </summary>
|
|||
|
public static string ConvertNumToTime(long number)
|
|||
|
{
|
|||
|
hours = (int)(number / 3600);
|
|||
|
minutes = (int)((number % 3600) / 60);
|
|||
|
seconds = (int)(number % 60);
|
|||
|
return string.Format("{0:D2}:{1:D2}:{2:D2}", hours, minutes, seconds);
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 转换时间
|
|||
|
/// </summary>
|
|||
|
public static string ConvertNumToTime(int number)
|
|||
|
{
|
|||
|
hours = (int)(number / 3600);
|
|||
|
minutes = (int)((number % 3600) / 60);
|
|||
|
seconds = (int)(number % 60);
|
|||
|
return string.Format("{0:D2}:{1:D2}:{2:D2}", hours, minutes, seconds);
|
|||
|
}
|
|||
|
|
|||
|
public static string ConvertNumToMinute(int number)
|
|||
|
{
|
|||
|
minutes = (int)(number / 60);
|
|||
|
seconds = (int)(number % 60);
|
|||
|
return string.Format("{0:D2}:{1:D2}", minutes, seconds);
|
|||
|
}
|
|||
|
|
|||
|
public static Dictionary<string, string> conver_map = new Dictionary<string, string>(1000);
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 名字转化为最多显示六个字加。。。
|
|||
|
/// </summary>
|
|||
|
/// <param name="str"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public static string ConvertName(string str)
|
|||
|
{
|
|||
|
if (string.IsNullOrEmpty(str))
|
|||
|
{
|
|||
|
return str;
|
|||
|
}
|
|||
|
|
|||
|
// 使用正则表达式去除不可显示的字符
|
|||
|
str = Regex.Replace(str, @"\p{C}+", "");
|
|||
|
|
|||
|
if (str.Length > 6)
|
|||
|
{
|
|||
|
if (conver_map.TryGetValue(str, out var v))
|
|||
|
{
|
|||
|
return v;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
string currName = str.Substring(0, 6) + "...";
|
|||
|
conver_map[str] = currName;
|
|||
|
return currName;
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
return str;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
public static Dictionary<string, string> _ConvertName2_dct = new Dictionary<string, string>();
|
|||
|
/// <summary>
|
|||
|
/// 名字转化为竖排,并且最多显示4个字加。。。
|
|||
|
/// </summary>
|
|||
|
/// <param name="str"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public static string ConvertName2(string str)
|
|||
|
{
|
|||
|
if (string.IsNullOrEmpty(str))
|
|||
|
{
|
|||
|
return str;
|
|||
|
}
|
|||
|
if (_ConvertName2_dct.TryGetValue(str, out var v))
|
|||
|
{
|
|||
|
return v;
|
|||
|
}
|
|||
|
|
|||
|
string currName = string.Empty;
|
|||
|
if (str.Length > 4 && str != null)
|
|||
|
{
|
|||
|
currName = str.Substring(0, 4) + "...";
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
currName = str;
|
|||
|
}
|
|||
|
string s2 = string.Empty;
|
|||
|
for (int i = 0; i < currName.Length; i++)
|
|||
|
{
|
|||
|
if (i >= 4)
|
|||
|
{
|
|||
|
s2 += currName[i];
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
s2 += currName[i] + "\n";
|
|||
|
}
|
|||
|
}
|
|||
|
_ConvertName2_dct[str] = s2;
|
|||
|
return s2;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 检查并移除字体不支持的字符
|
|||
|
/// </summary>
|
|||
|
/// <param name="input">需要检查的字符串</param>
|
|||
|
/// <param name="font">使用的字体</param>
|
|||
|
/// <param name="replacementChar">替换字符,默认为空</param>
|
|||
|
/// <returns>处理后的字符串</returns>
|
|||
|
public static string RemoveUnsupportedChars(string input, TMPro.TMP_FontAsset font, char replacementChar = '\0')
|
|||
|
{
|
|||
|
if (string.IsNullOrEmpty(input) || font == null)
|
|||
|
{
|
|||
|
return input;
|
|||
|
}
|
|||
|
|
|||
|
StringBuilder result = new StringBuilder();
|
|||
|
foreach (char c in input)
|
|||
|
{
|
|||
|
if (font.HasCharacter(c))
|
|||
|
{
|
|||
|
result.Append(c);
|
|||
|
}
|
|||
|
else if (replacementChar != '\0')
|
|||
|
{
|
|||
|
result.Append(replacementChar);
|
|||
|
}
|
|||
|
}
|
|||
|
return result.ToString();
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 检查并移除字体不支持的字符
|
|||
|
/// </summary>
|
|||
|
/// <param name="input">需要检查的字符串</param>
|
|||
|
/// <param name="font">使用的字体</param>
|
|||
|
/// <param name="replacementChar">替换字符,默认为空</param>
|
|||
|
/// <returns>处理后的字符串</returns>
|
|||
|
public static string RemoveUnsupportedChars(string input, Font font, char replacementChar = '\0')
|
|||
|
{
|
|||
|
if (string.IsNullOrEmpty(input) || font == null)
|
|||
|
{
|
|||
|
return input;
|
|||
|
}
|
|||
|
|
|||
|
StringBuilder result = new StringBuilder();
|
|||
|
foreach (char c in input)
|
|||
|
{
|
|||
|
if (font.HasCharacter(c))
|
|||
|
{
|
|||
|
result.Append(c);
|
|||
|
}
|
|||
|
else if (replacementChar != '\0')
|
|||
|
{
|
|||
|
result.Append(replacementChar);
|
|||
|
}
|
|||
|
}
|
|||
|
return result.ToString();
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 检查字符串中是否包含字体不支持的字符
|
|||
|
/// </summary>
|
|||
|
/// <param name="input">需要检查的字符串</param>
|
|||
|
/// <param name="font">使用的字体</param>
|
|||
|
/// <returns>是否包含不支持的字符</returns>
|
|||
|
public static bool ContainsUnsupportedChars(string input, TMPro.TMP_FontAsset font)
|
|||
|
{
|
|||
|
if (string.IsNullOrEmpty(input) || font == null)
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
foreach (char c in input)
|
|||
|
{
|
|||
|
if (!font.HasCharacter(c))
|
|||
|
{
|
|||
|
return true;
|
|||
|
}
|
|||
|
}
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|