259 lines
7.8 KiB
C#
259 lines
7.8 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Unity.VisualScripting;
|
|
using UnityEngine;
|
|
|
|
namespace AibisDream.Utility
|
|
{
|
|
public static class CommonUtil
|
|
{
|
|
private const float Epsilon = 1e-5f;
|
|
|
|
private static Camera _mainCam;
|
|
|
|
/// <summary>
|
|
/// 判断为0
|
|
/// </summary>
|
|
/// <param name="value">浮点数</param>
|
|
/// <returns>是否为0</returns>
|
|
public static bool IsZero(float value)
|
|
{
|
|
// 如果value小于epsilon,则认为它等于0
|
|
return Mathf.Abs(value) < Epsilon;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 判断浮点数是否相等
|
|
/// </summary>
|
|
/// <param name="value1">value1</param>
|
|
/// <param name="value2">value2</param>
|
|
/// <returns>是否相等</returns>
|
|
public static bool IsEqual(float value1, float value2)
|
|
{
|
|
return Mathf.Abs(value1 - value2) < Epsilon;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 简单延时实现
|
|
/// </summary>
|
|
/// <param name="delayTime">延时</param>
|
|
/// <param name="action">函数</param>
|
|
public static async Task Delay(int delayTime, Action action)
|
|
{
|
|
await Task.Delay(delayTime);
|
|
action?.Invoke();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 受限数值
|
|
/// </summary>
|
|
/// <param name="target">目标数</param>
|
|
/// <param name="max">上限</param>
|
|
/// <param name="min">下限</param>
|
|
/// <returns>结果</returns>
|
|
public static float Limit(float target, float max, float min)
|
|
{
|
|
if (target <= min)
|
|
{
|
|
return min;
|
|
}
|
|
|
|
if (target >= max)
|
|
{
|
|
return max;
|
|
}
|
|
|
|
return target;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 步进循环
|
|
/// </summary>
|
|
/// <param name="target">操作数</param>
|
|
/// <param name="min">最小值</param>
|
|
/// <param name="max">最大值</param>
|
|
/// <param name="step">步进</param>
|
|
/// <returns></returns>
|
|
/// <exception cref="ArgumentException">范围错误</exception>
|
|
public static int LoopStep(int target, int min, int max, int step = 1)
|
|
{
|
|
if (min > max)
|
|
{
|
|
throw new ArgumentException("min should be less than or equal to max.");
|
|
}
|
|
|
|
var range = max - min + 1;
|
|
|
|
// 计算新的目标值
|
|
var newTarget = target + step;
|
|
|
|
// 计算结果在范围内的值
|
|
if (newTarget > max)
|
|
{
|
|
newTarget = min + (newTarget - max - 1) % range;
|
|
}
|
|
else if (newTarget < min)
|
|
{
|
|
newTarget = max - (min - newTarget - 1) % range;
|
|
}
|
|
|
|
return newTarget;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 属性复制
|
|
/// </summary>
|
|
/// <param name="source">属性源</param>
|
|
/// <param name="target">属性目标</param>
|
|
public static void CopyFields(object source, object target)
|
|
{
|
|
var sourceFields = source.GetType().GetFields();
|
|
var targetFields = target.GetType().GetFields();
|
|
|
|
foreach (var sourceField in sourceFields)
|
|
{
|
|
if (sourceField.IsStatic) return;
|
|
|
|
var targetField = targetFields.FirstOrDefault(p =>
|
|
p.Name == sourceField.Name && p.FieldType == sourceField.FieldType);
|
|
if (targetField != null && !targetField.IsStatic())
|
|
{
|
|
targetField.SetValue(target, sourceField.GetValue(source));
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 角度转换
|
|
/// </summary>
|
|
/// <param name="angle">180度角</param>
|
|
/// <returns>360度角</returns>
|
|
public static float Angle360(float angle)
|
|
{
|
|
return angle < 0 ? angle + 360 : angle;
|
|
}
|
|
|
|
#region 链式扩展
|
|
|
|
public static T As<T>(this object selfObj) where T : class
|
|
{
|
|
return selfObj as T;
|
|
}
|
|
|
|
public static T Self<T>(this T self, Action<T> onDo)
|
|
{
|
|
onDo?.Invoke(self);
|
|
return self;
|
|
}
|
|
|
|
public static T Self<T>(this T self, Func<T, T> onDo)
|
|
{
|
|
return onDo.Invoke(self);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 静态扩展
|
|
|
|
public static void SetAlpha(this SpriteRenderer renderer, float targetAlpha)
|
|
{
|
|
Color color = renderer.color;
|
|
color.a = targetAlpha;
|
|
renderer.color = color;
|
|
}
|
|
|
|
#endregion
|
|
|
|
///<summary>
|
|
/// 去除原始对话中名称部分
|
|
/// </summary>
|
|
/// <param name="lineText">对话原文</param>
|
|
/// <returns>去除名称后结果</returns>
|
|
public static string RemoveName(string lineText)
|
|
{
|
|
var textArr = lineText.Split(":");
|
|
return textArr.Length > 1 ? textArr[1].Trim() : lineText;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 拆分字符串为若干行
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <param name="maxCharsPerLine"></param>
|
|
/// <returns></returns>
|
|
public static string SplitString(string input, int maxCharsPerLine)
|
|
{
|
|
if (string.IsNullOrEmpty(input) || maxCharsPerLine <= 0)
|
|
{
|
|
return input;
|
|
}
|
|
|
|
var result = new StringBuilder();
|
|
var lines = input.Replace("<br>", "\n").Split('\n');
|
|
|
|
foreach (var line in lines)
|
|
{
|
|
var currentIndex = 0;
|
|
|
|
while (currentIndex < line.Length)
|
|
{
|
|
var length = Math.Min(maxCharsPerLine, line.Length - currentIndex);
|
|
var substring = line.Substring(currentIndex, length);
|
|
|
|
// 检查是否需要调整长度以避免在单词中间断开
|
|
if (currentIndex + length < line.Length && !char.IsWhiteSpace(line[currentIndex + length]) && !char.IsPunctuation(line[currentIndex + length]))
|
|
{
|
|
var lastSpace = substring.LastIndexOf(' ');
|
|
if (lastSpace > 0)
|
|
{
|
|
length = lastSpace;
|
|
substring = line.Substring(currentIndex, length);
|
|
}
|
|
}
|
|
|
|
result.Append(substring.TrimStart());
|
|
currentIndex += length;
|
|
|
|
if (currentIndex < line.Length)
|
|
{
|
|
result.Append('\n');
|
|
}
|
|
}
|
|
|
|
result.Append('\n');
|
|
}
|
|
|
|
// Remove the last '\n' if needed
|
|
if (result.Length > 0)
|
|
{
|
|
result.Length--;
|
|
}
|
|
|
|
return result.ToString();
|
|
}
|
|
|
|
public static Vector2 GetPivotByType(PivotType pivotType)
|
|
{
|
|
return pivotType switch
|
|
{
|
|
PivotType.Top => new Vector2(0.5f, 1),
|
|
PivotType.Bottom => new Vector2(0.5f, 0),
|
|
PivotType.Left => new Vector2(0, 0.5f),
|
|
PivotType.Right => new Vector2(1, 0.5f),
|
|
PivotType.TopLeft => new Vector2(0, 1),
|
|
PivotType.TopRight => new Vector2(1, 1),
|
|
PivotType.BottomLeft => new Vector2(0, 0),
|
|
PivotType.BottomRight => new Vector2(1, 0),
|
|
PivotType.Center => new Vector2(0.5f, 0.5f),
|
|
_ => Vector2.zero
|
|
};
|
|
}
|
|
|
|
public static string Escape(string rawText)
|
|
{
|
|
return rawText.Replace("^", "#").Replace("[[", "{").Replace("]]", "}");
|
|
}
|
|
}
|
|
} |