using System; using System.Threading.Tasks; using UnityEngine; namespace AibisDream.Utility { public static class CommonUtil { private const float Epsilon = 1e-5f; /// /// 判断为0 /// /// 浮点数 /// 是否为0 public static bool IsZero(float value) { // 如果value小于epsilon,则认为它等于0 return Mathf.Abs(value) < Epsilon; } /// /// 判断浮点数是否相等 /// /// value1 /// value2 /// 是否相等 public static bool IsEqual(float value1, float value2) { return Mathf.Abs(value1 - value2) < Epsilon; } /// /// 简单延时实现 /// /// 延时 /// 函数 public static async Task Delay(int delayTime, Action action) { await Task.Delay(delayTime); action?.Invoke(); } /// /// 受限加法 /// /// 目标数 /// 加数 /// 上限 /// 下限 /// 结果 public static float LimitedAdd(float target, float add, float max, float min) { var targetOil = target + add; return targetOil switch { <= 0 => min, >= 1 => max, _ => targetOil }; } } }