Archived
分散文件结构,添加了枚举类型的一个辅助类
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
using Unity.VisualScripting.YamlDotNet.Core.Tokens;
|
||||
|
||||
using UnityEngine;
|
||||
|
||||
|
||||
namespace XericLibrary.Runtime.MacroLibrary
|
||||
{
|
||||
/// <summary>
|
||||
/// 枚举宏,用于完成枚举项遍历,位操作等
|
||||
/// </summary>
|
||||
public class MacroEnum<T>
|
||||
where T: struct, Enum
|
||||
{
|
||||
/// <summary>
|
||||
/// 反射组值
|
||||
/// </summary>
|
||||
private Array valueArray;
|
||||
|
||||
/// <summary>
|
||||
/// 反射组名称
|
||||
/// </summary>
|
||||
private string[] nameArray;
|
||||
|
||||
/// <summary>
|
||||
/// 构建一个枚举辅助器
|
||||
/// </summary>
|
||||
public MacroEnum()
|
||||
{
|
||||
valueArray = typeof(T).GetEnumValues();
|
||||
nameArray = typeof(T).GetEnumNames();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取列表
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public IEnumerable<T> GetValues()
|
||||
{
|
||||
foreach(var type in valueArray)
|
||||
yield return (T)type;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取列表
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public IEnumerable<string> GetNames()
|
||||
{
|
||||
foreach(var type in nameArray)
|
||||
yield return type;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取枚举项目
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public IEnumerable<(string, T)> GetEnums()
|
||||
{
|
||||
var output = nameArray
|
||||
.Zip(GetValues(), (name, value) => (name, value));
|
||||
return output;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 设置目标的枚举值
|
||||
/// </summary>
|
||||
/// <param name="target"></param>
|
||||
/// <param name="values"></param>
|
||||
public static void SetEnum(ref int target, params T[] values)
|
||||
{
|
||||
foreach(var value in values)
|
||||
{
|
||||
target |= Convert.ToInt32(value);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置目标的枚举值
|
||||
/// </summary>
|
||||
/// <param name="target"></param>
|
||||
/// <param name="values"></param>
|
||||
public static void SetEnum(ref int target, T value)
|
||||
{
|
||||
target |= Convert.ToInt32(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 复位目标的枚举值
|
||||
/// </summary>
|
||||
/// <param name="target"></param>
|
||||
/// <param name="values"></param>
|
||||
public static void ResetEnum(ref int target, params T[] values)
|
||||
{
|
||||
int temp = 0;
|
||||
foreach(var value in values)
|
||||
{
|
||||
temp |= Convert.ToInt32(value);
|
||||
}
|
||||
target &= ~temp;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 复位目标的枚举值
|
||||
/// </summary>
|
||||
/// <param name="target"></param>
|
||||
/// <param name="values"></param>
|
||||
public static void ResetEnum(ref int target, T value)
|
||||
{
|
||||
target &= ~Convert.ToInt32(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 翻转目标的枚举值
|
||||
/// </summary>
|
||||
/// <param name="target"></param>
|
||||
/// <param name="values"></param>
|
||||
public static void FlipEnum(ref int target, params T[] values)
|
||||
{
|
||||
int temp = 0;
|
||||
foreach(var value in values)
|
||||
{
|
||||
temp |= Convert.ToInt32(value);
|
||||
}
|
||||
target ^= temp;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 翻转目标的枚举值
|
||||
/// </summary>
|
||||
/// <param name="target"></param>
|
||||
/// <param name="values"></param>
|
||||
public static void FlipEnum(ref int target, T value)
|
||||
{
|
||||
target ^= Convert.ToInt32(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user