This commit is contained in:
2025-09-17 18:56:28 +08:00
commit 54c72710a5
5244 changed files with 5717609 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
# UniFramework.Utility
一些工具类。

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: f055964b764f8cc41996f3c543307c85
TextScriptImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: c3799338015524b47a601024fd574a90
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,69 @@
using System;
namespace UniFramework.Utility
{
internal struct BitMask32
{
private int _mask;
public static implicit operator int(BitMask32 mask) { return mask._mask; }
public static implicit operator BitMask32(int mask) { return new BitMask32(mask); }
public BitMask32(int mask)
{
_mask = mask;
}
/// <summary>
/// 打开位
/// </summary>
public void Open(int bit)
{
if (bit < 0 || bit > 31)
throw new ArgumentOutOfRangeException();
else
_mask |= 1 << bit;
}
/// <summary>
/// 关闭位
/// </summary>
public void Close(int bit)
{
if (bit < 0 || bit > 31)
throw new ArgumentOutOfRangeException();
else
_mask &= ~(1 << bit);
}
/// <summary>
/// 位取反
/// </summary>
public void Reverse(int bit)
{
if (bit < 0 || bit > 31)
throw new ArgumentOutOfRangeException();
else
_mask ^= 1 << bit;
}
/// <summary>
/// 所有位取反
/// </summary>
public void Inverse()
{
_mask = ~_mask;
}
/// <summary>
/// 比对位值
/// </summary>
public bool Test(int bit)
{
if (bit < 0 || bit > 31)
throw new ArgumentOutOfRangeException();
else
return (_mask & (1 << bit)) != 0;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 60c7594328ef976408edadfdf2b9aa3d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,69 @@
using System;
namespace UniFramework.Utility
{
internal struct BitMask64
{
private long _mask;
public static implicit operator long(BitMask64 mask) { return mask._mask; }
public static implicit operator BitMask64(long mask) { return new BitMask64(mask); }
public BitMask64(long mask)
{
_mask = mask;
}
/// <summary>
/// 打开位
/// </summary>
public void Open(int bit)
{
if (bit < 0 || bit > 63)
throw new ArgumentOutOfRangeException();
else
_mask |= 1L << bit;
}
/// <summary>
/// 关闭位
/// </summary>
public void Close(int bit)
{
if (bit < 0 || bit > 63)
throw new ArgumentOutOfRangeException();
else
_mask &= ~(1L << bit);
}
/// <summary>
/// 位取反
/// </summary>
public void Reverse(int bit)
{
if (bit < 0 || bit > 63)
throw new ArgumentOutOfRangeException();
else
_mask ^= 1L << bit;
}
/// <summary>
/// 所有位取反
/// </summary>
public void Inverse()
{
_mask = ~_mask;
}
/// <summary>
/// 比对位值
/// </summary>
public bool Test(int bit)
{
if (bit < 0 || bit > 63)
throw new ArgumentOutOfRangeException();
else
return (_mask & (1L << bit)) != 0;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6b03684bc5163694ab3983243512b4cc
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,124 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace UniFramework.Utility
{
public static class StringConvert
{
/// <summary>
/// 正则表达式
/// </summary>
private static readonly Regex REGEX = new Regex(@"\{[-+]?[0-9]+\.?[0-9]*\}", RegexOptions.IgnoreCase);
/// <summary>
/// 字符串转换为BOOL
/// </summary>
public static bool StringToBool(string str)
{
int value = (int)Convert.ChangeType(str, typeof(int));
return value > 0;
}
/// <summary>
/// 字符串转换为数值
/// </summary>
public static T StringToValue<T>(string str)
{
return (T)Convert.ChangeType(str, typeof(T));
}
/// <summary>
/// 字符串转换为数值列表
/// </summary>
/// <param name="separator">分隔符</param>
public static List<T> StringToValueList<T>(string str, char separator)
{
List<T> result = new List<T>();
if (!String.IsNullOrEmpty(str))
{
string[] splits = str.Split(separator);
foreach (string split in splits)
{
if (!String.IsNullOrEmpty(split))
{
result.Add((T)Convert.ChangeType(split, typeof(T)));
}
}
}
return result;
}
/// <summary>
/// 字符串转为字符串列表
/// </summary>
public static List<string> StringToStringList(string str, char separator)
{
List<string> result = new List<string>();
if (!String.IsNullOrEmpty(str))
{
string[] splits = str.Split(separator);
foreach (string split in splits)
{
if (!String.IsNullOrEmpty(split))
{
result.Add(split);
}
}
}
return result;
}
/// <summary>
/// 转换为枚举
/// 枚举索引转换为枚举类型
/// </summary>
public static T IndexToEnum<T>(string index) where T : IConvertible
{
int enumIndex = (int)Convert.ChangeType(index, typeof(int));
return IndexToEnum<T>(enumIndex);
}
/// <summary>
/// 转换为枚举
/// 枚举索引转换为枚举类型
/// </summary>
public static T IndexToEnum<T>(int index) where T : IConvertible
{
if (Enum.IsDefined(typeof(T), index) == false)
{
throw new ArgumentException($"Enum {typeof(T)} is not defined index {index}");
}
return (T)Enum.ToObject(typeof(T), index);
}
/// <summary>
/// 转换为枚举
/// 枚举名称转换为枚举类型
/// </summary>
public static T NameToEnum<T>(string name)
{
if (Enum.IsDefined(typeof(T), name) == false)
{
throw new ArgumentException($"Enum {typeof(T)} is not defined name {name}");
}
return (T)Enum.Parse(typeof(T), name);
}
/// <summary>
/// 字符串转换为参数列表
/// </summary>
public static List<float> StringToParams(string str)
{
List<float> result = new List<float>();
MatchCollection matches = REGEX.Matches(str);
for (int i = 0; i < matches.Count; i++)
{
string value = matches[i].Value.Trim('{', '}');
result.Add(StringToValue<float>(value));
}
return result;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 889d01b55e38f7c49bbfb8908d9bf17f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,51 @@
using System;
using System.Text;
namespace UniFramework.Utility
{
public static class StringFormat
{
[ThreadStatic]
private static StringBuilder _cacheBuilder = new StringBuilder(1024);
public static string Format(string format, object arg0)
{
if (string.IsNullOrEmpty(format))
throw new ArgumentNullException();
_cacheBuilder.Length = 0;
_cacheBuilder.AppendFormat(format, arg0);
return _cacheBuilder.ToString();
}
public static string Format(string format, object arg0, object arg1)
{
if (string.IsNullOrEmpty(format))
throw new ArgumentNullException();
_cacheBuilder.Length = 0;
_cacheBuilder.AppendFormat(format, arg0, arg1);
return _cacheBuilder.ToString();
}
public static string Format(string format, object arg0, object arg1, object arg2)
{
if (string.IsNullOrEmpty(format))
throw new ArgumentNullException();
_cacheBuilder.Length = 0;
_cacheBuilder.AppendFormat(format, arg0, arg1, arg2);
return _cacheBuilder.ToString();
}
public static string Format(string format, params object[] args)
{
if (string.IsNullOrEmpty(format))
throw new ArgumentNullException();
if (args == null)
throw new ArgumentNullException();
_cacheBuilder.Length = 0;
_cacheBuilder.AppendFormat(format, args);
return _cacheBuilder.ToString();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 86ae09fcbdc5301499fb61e9bd4a5794
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,14 @@
{
"name": "UniFramework.Utility",
"rootNamespace": "",
"references": [],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 1ea6b676786131e4095182b742bb64ec
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,199 @@

namespace UniFramework.Utility
{
public sealed class UniTimer
{
/// <summary>
/// 延迟后,触发一次
/// </summary>
public static UniTimer CreateOnceTimer(float delay)
{
return new UniTimer(delay, -1, -1, 1);
}
/// <summary>
/// 延迟后,永久性的间隔触发
/// </summary>
/// <param name="delay">延迟时间</param>
/// <param name="interval">间隔时间</param>
public static UniTimer CreatePepeatTimer(float delay, float interval)
{
return new UniTimer(delay, interval, -1, -1);
}
/// <summary>
/// 延迟后,在一段时间内间隔触发
/// </summary>
/// <param name="delay">延迟时间</param>
/// <param name="interval">间隔时间</param>
/// <param name="duration">触发周期</param>
public static UniTimer CreatePepeatTimer(float delay, float interval, float duration)
{
return new UniTimer(delay, interval, duration, -1);
}
/// <summary>
/// 延迟后,间隔触发一定次数
/// </summary>
/// <param name="delay">延迟时间</param>
/// <param name="interval">间隔时间</param>
/// <param name="maxTriggerCount">最大触发次数</param>
public static UniTimer CreatePepeatTimer(float delay, float interval, long maxTriggerCount)
{
return new UniTimer(delay, interval, -1, maxTriggerCount);
}
/// <summary>
/// 延迟后,在一段时间内触发
/// </summary>
/// <param name="delay">延迟时间</param>
/// <param name="duration">触发周期</param>
public static UniTimer CreateDurationTimer(float delay, float duration)
{
return new UniTimer(delay, -1, duration, -1);
}
/// <summary>
/// 延迟后,永久触发
/// </summary>
public static UniTimer CreateForeverTimer(float delay)
{
return new UniTimer(delay, -1, -1, -1);
}
private readonly float _intervalTime;
private readonly float _durationTime;
private readonly long _maxTriggerCount;
// 需要重置的变量
private float _delayTimer = 0;
private float _durationTimer = 0;
private float _intervalTimer = 0;
private long _triggerCount = 0;
/// <summary>
/// 延迟时间
/// </summary>
public float DelayTime { private set; get; }
/// <summary>
/// 是否已经结束
/// </summary>
public bool IsOver { private set; get; }
/// <summary>
/// 是否已经暂停
/// </summary>
public bool IsPause { private set; get; }
/// <summary>
/// 延迟剩余时间
/// </summary>
public float Remaining
{
get
{
if (IsOver)
return 0f;
else
return System.Math.Max(0f, DelayTime - _delayTimer);
}
}
/// <summary>
/// 计时器
/// </summary>
/// <param name="delay">延迟时间</param>
/// <param name="interval">间隔时间</param>
/// <param name="duration">运行时间</param>
/// <param name="maxTriggerTimes">最大触发次数</param>
public UniTimer(float delay, float interval, float duration, long maxTriggerCount)
{
DelayTime = delay;
_intervalTime = interval;
_durationTime = duration;
_maxTriggerCount = maxTriggerCount;
}
/// <summary>
/// 暂停计时器
/// </summary>
public void Pause()
{
IsPause = true;
}
/// <summary>
/// 恢复计时器
/// </summary>
public void Resume()
{
IsPause = false;
}
/// <summary>
/// 结束计时器
/// </summary>
public void Kill()
{
IsOver = true;
}
/// <summary>
/// 重置计时器
/// </summary>
public void Reset()
{
_delayTimer = 0;
_durationTimer = 0;
_intervalTimer = 0;
_triggerCount = 0;
IsOver = false;
IsPause = false;
}
/// <summary>
/// 更新计时器
/// </summary>
public bool Update(float deltaTime)
{
if (IsOver || IsPause)
return false;
_delayTimer += deltaTime;
if (_delayTimer < DelayTime)
return false;
if(_intervalTime > 0)
_intervalTimer += deltaTime;
if (_durationTime > 0)
_durationTimer += deltaTime;
// 检测间隔执行
if (_intervalTime > 0)
{
if (_intervalTimer < _intervalTime)
return false;
_intervalTimer = 0;
}
// 检测结束条件
if (_durationTime > 0)
{
if (_durationTimer >= _durationTime)
Kill();
}
// 检测结束条件
if (_maxTriggerCount > 0)
{
_triggerCount++;
if (_triggerCount >= _maxTriggerCount)
Kill();
}
return true;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a9ba1e57e76e1c3498c1e678e2737ab2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: