using System; using System.Collections; using System.Collections.Generic; using System.Linq; using Unity.VisualScripting.YamlDotNet.Core.Tokens; using UnityEngine; namespace XericLibrary.Runtime.MacroLibrary { /// /// 枚举宏,用于完成枚举项遍历,位操作等 /// public class MacroEnum where T: struct, Enum { /// /// 反射组值 /// private Array valueArray; /// /// 反射组名称 /// private string[] nameArray; /// /// 构建一个枚举辅助器 /// public MacroEnum() { valueArray = typeof(T).GetEnumValues(); nameArray = typeof(T).GetEnumNames(); } /// /// 获取列表 /// /// public IEnumerable GetValues() { foreach(var type in valueArray) yield return (T)type; } /// /// 获取列表 /// /// public IEnumerable GetNames() { foreach(var type in nameArray) yield return type; } /// /// 获取枚举项目 /// /// public IEnumerable<(string, T)> GetEnums() { var output = nameArray .Zip(GetValues(), (name, value) => (name, value)); return output; } /// /// 设置目标的枚举值 /// /// /// public static void SetEnum(ref int target, params T[] values) { foreach(var value in values) { target |= Convert.ToInt32(value); } } /// /// 设置目标的枚举值 /// /// /// public static void SetEnum(ref int target, T value) { target |= Convert.ToInt32(value); } /// /// 复位目标的枚举值 /// /// /// public static void ResetEnum(ref int target, params T[] values) { int temp = 0; foreach(var value in values) { temp |= Convert.ToInt32(value); } target &= ~temp; } /// /// 复位目标的枚举值 /// /// /// public static void ResetEnum(ref int target, T value) { target &= ~Convert.ToInt32(value); } /// /// 翻转目标的枚举值 /// /// /// public static void FlipEnum(ref int target, params T[] values) { int temp = 0; foreach(var value in values) { temp |= Convert.ToInt32(value); } target ^= temp; } /// /// 翻转目标的枚举值 /// /// /// public static void FlipEnum(ref int target, T value) { target ^= Convert.ToInt32(value); } } }