/******************************************************************** 文件: CommonTools.cs 作者: 梦语 邮箱: 1982614048@qq.com 创建时间: 2024/03/29 17:28:19 最后修改: 梦语 最后修改时间: 2024/04/03 17:15:36 功能: 通用工具类 *********************************************************************/ using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Reflection; using UnityEngine; namespace Ether { public static class CommonTools { /// /// 获取所有子类 /// /// 基类类型 /// public static Type[] GetChildTypes(Type parentType) { List listType = new List(); // 获取所有程序集 Assembly[] asms = AppDomain.CurrentDomain.GetAssemblies(); foreach (Assembly assembly in asms) { // 遍历程序集下的每一个类型 Type[] types = assembly.GetTypes(); foreach (Type tChild in types) { if (tChild == parentType || tChild.IsSubclassOf(parentType)) { listType.Add(tChild); } } } return listType.ToArray(); } /// /// 获取类型 /// /// /// public static Type GetType(string name) { // 获取所有程序集 Assembly[] asms = AppDomain.CurrentDomain.GetAssemblies(); foreach (Assembly assembly in asms) { Type[] types = assembly.GetTypes(); foreach (Type type in types) { if (type.Name == name) { return type; } } } return null; } /// /// 字符串转换成枚举 /// /// 枚举类型 /// 转换的字符串名 /// public static T StringToEnum(string name) where T : Enum { return (T)Enum.Parse(typeof(T), name); } /// /// 获取枚举数量 /// /// /// public static int GetNumberOfEnumValues() where T : Enum { return typeof(T).GetFields(BindingFlags.Public | BindingFlags.Static).Length; } public static List<(string, string, string)> GetAllFeildStr(string typeName) { Type type = GetType(typeName); FieldInfo[] allField = type.GetFields(BindingFlags.Public | BindingFlags.Static); List<(string, string, string)> tempList = new List<(string, string, string)>(); foreach (var info in allField) { string fieldName = info.Name; string fieldType = info.FieldType.Name.ToString(); string fieldValue = info.GetValue(type).ToString(); tempList.Add((fieldName, fieldType, fieldValue)); } return tempList; } /// /// 获取枚举的所有值名称 /// /// /// public static string[] GetAllEnumName() where T : Enum { FieldInfo[] allEnum = typeof(T).GetFields(BindingFlags.Public | BindingFlags.Static); string[] names = new string[allEnum.Length]; for (int i = 0; i < allEnum.Length; i++) { names[i] = allEnum[i].Name; } return names; } /// /// 获取枚举属性对应的attribute /// /// /// /// /// public static K[] GetEnumFieldAllAttribute() where T : Enum where K : Attribute { //反射获取字段 FieldInfo[] allFields = typeof(T).GetFields(); List fields = new List(); foreach (FieldInfo field in allFields) { if (field.FieldType == typeof(T)) { fields.Add(field); } } Type attType = typeof(K); K[] allAttributeValue = new K[fields.Count]; for (int i = 0; i < fields.Count; i++) { if (fields[i].IsDefined(attType, false)) { //获取id //int value = (int)fields[i].GetValue(null); //获取api,读取字段的自定义Attribute object attribute = fields[i].GetCustomAttributes(typeof(K), false)[0]; allAttributeValue[i] = attribute as K; } else { allAttributeValue[i] = null; } } return allAttributeValue; } /// /// 获取枚举属性对应的attribute /// /// /// /// /// public static K GetEnumFieldAttribute(int enumValue) where T : Enum where K : Attribute { return GetEnumFieldAttribute(typeof(T), enumValue); } public static K GetEnumFieldAttribute(Type type, int enumValue) where K : Attribute { //反射获取字段 System.Reflection.FieldInfo[] fields = type.GetFields(); System.Type attType = typeof(K); for (int i = 0; i < fields.Length; i++) { if (fields[i].IsDefined(attType, false)) { //获取id int value = (int)fields[i].GetValue(null); if (value == enumValue) { //获取api,读取字段的自定义Attribute object attribute = fields[i].GetCustomAttributes(typeof(K), false)[0]; return attribute as K; } } } return null; } ///// ///// 获取对应枚举的描述文字 ///// ///// ///// ///// ///// //public static string GetEnumLabelText(int enumValue) where T : Enum where K : Attribute //{ // LabelTextAttribute attribute = GetEnumFieldAttribute(enumValue); // return attribute.Text; //} public static void TakeScreenshot() { string screenshotPath = Application.persistentDataPath + "/Screenshots/"; string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".png"; if (!Directory.Exists(screenshotPath)) { Directory.CreateDirectory(screenshotPath); } ScreenCapture.CaptureScreenshot(screenshotPath + fileName); } } }