138 lines
3.9 KiB
C#
138 lines
3.9 KiB
C#
using System;
|
|
using System.Linq;
|
|
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="mousePoint">鼠标界面位置</param>
|
|
/// <param name="transform">物体形状</param>
|
|
/// <returns>世界位置</returns>
|
|
public static Vector3 GetMouseWorldPos(Vector3 mousePoint, Transform transform)
|
|
{
|
|
if (!_mainCam) _mainCam = Camera.main;
|
|
|
|
mousePoint.z = _mainCam.WorldToScreenPoint(transform.position).z;
|
|
return _mainCam.ScreenToWorldPoint(mousePoint);
|
|
}
|
|
|
|
/// <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));
|
|
}
|
|
}
|
|
}
|
|
|
|
#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
|
|
}
|
|
} |