/**************************************************************************** * Copyright (c) 2017 ~ 2022 liangxiegame UNDER MIT LICENSE * * https://qframework.cn * https://github.com/liangxiegame/QFramework * https://gitee.com/liangxiegame/QFramework ****************************************************************************/ namespace QFramework { using System; using System.Collections.Generic; using System.Linq; using System.IO; using System.Text.RegularExpressions; using System.Reflection; using System.Text; using UnityEngine; using UnityEngine.Events; using UnityEngine.UI; public static class DelegateExtension { #region Func Extension /// /// 功能:不为空则调用 Func /// /// 示例: /// /// Func func = ()=> 1; /// var number = func.InvokeGracefully(); // 等价于 if (func != null) number = func(); /// /// /// /// /// public static T InvokeGracefully(this Func selfFunc) { return null != selfFunc ? selfFunc() : default(T); } #endregion #region Action /// /// 功能:不为空则调用 Action /// /// 示例: /// /// System.Action action = () => Log.I("action called"); /// action.InvokeGracefully(); // if (action != null) action(); /// /// /// action 对象 /// 是否调用成功 public static bool InvokeGracefully(this Action selfAction) { if (null != selfAction) { selfAction(); return true; } return false; } /// /// 不为空则调用 Action /// /// 示例: /// /// System.Action action = (number) => Log.I("action called" + number); /// action.InvokeGracefully(10); // if (action != null) action(10); /// /// /// action 对象 /// 参数 /// 是否调用成功 public static bool InvokeGracefully(this Action selfAction, T t) { if (null != selfAction) { selfAction(t); return true; } return false; } /// /// 不为空则调用 Action /// /// 示例 /// /// System.Action action = (number,name) => Log.I("action called" + number + name); /// action.InvokeGracefully(10,"qframework"); // if (action != null) action(10,"qframework"); /// /// /// /// call succeed public static bool InvokeGracefully(this Action selfAction, T t, K k) { if (null != selfAction) { selfAction(t, k); return true; } return false; } /// /// 不为空则调用委托 /// /// 示例: /// /// // delegate /// TestDelegate testDelegate = () => { }; /// testDelegate.InvokeGracefully(); /// /// /// /// call suceed public static bool InvokeGracefully(this Delegate selfAction, params object[] args) { if (null != selfAction) { selfAction.DynamicInvoke(args); return true; } return false; } #endregion } public static class CSharpObjectExtension { /// /// 是否相等 /// /// 示例: /// /// if (this.Is(player)) /// { /// ... /// } /// /// /// /// /// public static bool Is(this object selfObj, object value) { return selfObj == value; } public static bool Is(this T selfObj, Func condition) { return condition(selfObj); } /// /// 表达式成立 则执行 Action /// /// 示例: /// /// (1 == 1).Do(()=>Debug.Log("1 == 1"); /// /// /// /// /// public static bool Do(this bool selfCondition, Action action) { if (selfCondition) { action(); } return selfCondition; } /// /// 不管表达成不成立 都执行 Action,并把结果返回 /// /// 示例: /// /// (1 == 1).Do((result)=>Debug.Log("1 == 1:" + result); /// /// /// /// /// public static bool Do(this bool selfCondition, Action action) { action(selfCondition); return selfCondition; } /// /// 功能:判断是否为空 /// /// 示例: /// /// var simpleObject = new object(); /// /// if (simpleObject.IsNull()) // 等价于 simpleObject == null /// { /// // do sth /// } /// /// /// 判断对象(this) /// 对象的类型(可不填) /// 是否为空 public static bool IsNull(this T selfObj) where T : class { return null == selfObj; } /// /// 功能:判断不是为空 /// 示例: /// /// var simpleObject = new object(); /// /// if (simpleObject.IsNotNull()) // 等价于 simpleObject != null /// { /// // do sth /// } /// /// /// 判断对象(this) /// 对象的类型(可不填) /// 是否不为空 public static bool IsNotNull(this T selfObj) where T : class { return null != selfObj; } public static void DoIfNotNull(this T selfObj, Action action) where T : class { if (selfObj != null) { action(selfObj); } } } /// /// 泛型工具 /// /// 实例: /// /// 示例: /// var typeName = GenericExtention.GetTypeName(); /// typeName.LogInfo(); // string /// /// public static class GenericUtil { /// /// 获取泛型名字 /// /// var typeName = GenericExtention.GetTypeName(); /// typeName.LogInfo(); // string /// /// /// /// public static string GetTypeName() { return typeof(T).ToString(); } } /// /// 可枚举的集合扩展(Array、List、Dictionary) /// public static class IEnumerableExtension { #region Array Extension /// /// 遍历数组 /// /// var testArray = new[] { 1, 2, 3 }; /// testArray.ForEach(number => number.LogInfo()); /// /// /// The each. /// Self array. /// Action. /// The 1st type parameter. /// 返回自己 public static T[] ForEach(this T[] selfArray, Action action) { Array.ForEach(selfArray, action); return selfArray; } /// /// 遍历 IEnumerable /// /// // IEnumerable /// IEnumerable testIenumerable = new List { 1, 2, 3 }; /// testIenumerable.ForEach(number => number.LogInfo()); /// // 支持字典的遍历 /// new Dictionary() /// .ForEach(keyValue => Log.I("key:{0},value:{1}", keyValue.Key, keyValue.Value)); /// /// /// The each. /// Self array. /// Action. /// The 1st type parameter. public static IEnumerable ForEach(this IEnumerable selfArray, Action action) { if (action == null) throw new ArgumentException(); foreach (var item in selfArray) { action(item); } return selfArray; } #endregion #region List Extension /// /// 倒序遍历 /// /// var testList = new List { 1, 2, 3 }; /// testList.ForEachReverse(number => number.LogInfo()); // 3, 2, 1 /// /// /// 返回自己 /// Self list. /// Action. /// The 1st type parameter. public static List ForEachReverse(this List selfList, Action action) { if (action == null) throw new ArgumentException(); for (var i = selfList.Count - 1; i >= 0; --i) action(selfList[i]); return selfList; } /// /// 倒序遍历(可获得索引) /// /// var testList = new List { 1, 2, 3 }; /// testList.ForEachReverse((number,index)=> number.LogInfo()); // 3, 2, 1 /// /// /// The each reverse. /// Self list. /// Action. /// The 1st type parameter. public static List ForEachReverse(this List selfList, Action action) { if (action == null) throw new ArgumentException(); for (var i = selfList.Count - 1; i >= 0; --i) action(selfList[i], i); return selfList; } /// /// 遍历列表(可获得索引) /// /// var testList = new List {1, 2, 3 }; /// testList.Foreach((number,index)=>number.LogInfo()); // 1, 2, 3, /// /// /// 列表类型 /// 目标表 /// 行为 public static void ForEach(this List list, Action action) { for (var i = 0; i < list.Count; i++) { action(i, list[i]); } } #endregion #region Dictionary Extension /// /// 合并字典 /// /// // 示例 /// var dictionary1 = new Dictionary { { "1", "2" } }; /// var dictionary2 = new Dictionary { { "3", "4" } }; /// var dictionary3 = dictionary1.Merge(dictionary2); /// dictionary3.ForEach(pair => Log.I("{0}:{1}", pair.Key, pair.Value)); /// /// /// The merge. /// Dictionary. /// Dictionaries. /// The 1st type parameter. /// The 2nd type parameter. public static Dictionary Merge(this Dictionary dictionary, params Dictionary[] dictionaries) { return dictionaries.Aggregate(dictionary, (current, dict) => current.Union(dict).ToDictionary(kv => kv.Key, kv => kv.Value)); } /// /// 遍历字典 /// /// var dict = new Dictionary {{"name","liangxie},{"age","18"}}; /// dict.ForEach((key,value)=> Log.I("{0}:{1}",key,value);// name:liangxie age:18 /// /// /// /// /// /// public static void ForEach(this Dictionary dict, Action action) { var dictE = dict.GetEnumerator(); while (dictE.MoveNext()) { var current = dictE.Current; action(current.Key, current.Value); } dictE.Dispose(); } /// /// 字典添加新的词典 /// /// /// /// /// /// public static void AddRange(this Dictionary dict, Dictionary addInDict, bool isOverride = false) { var dictE = addInDict.GetEnumerator(); while (dictE.MoveNext()) { var current = dictE.Current; if (dict.ContainsKey(current.Key)) { if (isOverride) dict[current.Key] = current.Value; continue; } dict.Add(current.Key, current.Value); } dictE.Dispose(); } #endregion } /// /// 对 System.IO 的一些扩展 /// public static class IOExtension { /// /// 检测路径是否存在,如果不存在则创建 /// /// public static string CreateDirIfNotExists4FilePath(this string path) { var direct = Path.GetDirectoryName(path); if (!Directory.Exists(direct)) { Directory.CreateDirectory(direct); } return path; } /// /// 创建新的文件夹,如果存在则不创建 /// /// var testDir = "Assets/TestFolder"; /// testDir.CreateDirIfNotExists(); /// // 结果为,在 Assets 目录下创建 TestFolder /// /// public static string CreateDirIfNotExists(this string dirFullPath) { if (!Directory.Exists(dirFullPath)) { Directory.CreateDirectory(dirFullPath); } return dirFullPath; } /// /// 删除文件夹,如果存在 /// /// var testDir = "Assets/TestFolder"; /// testDir.DeleteDirIfExists(); /// // 结果为,在 Assets 目录下删除了 TestFolder /// /// public static void DeleteDirIfExists(this string dirFullPath) { if (Directory.Exists(dirFullPath)) { Directory.Delete(dirFullPath, true); } } /// /// 清空 Dir(保留目录),如果存在。 /// /// var testDir = "Assets/TestFolder"; /// testDir.EmptyDirIfExists(); /// // 结果为,清空了 TestFolder 里的内容 /// /// public static void EmptyDirIfExists(this string dirFullPath) { if (Directory.Exists(dirFullPath)) { Directory.Delete(dirFullPath, true); } Directory.CreateDirectory(dirFullPath); } /// /// 删除文件 如果存在 /// /// // 示例 /// var filePath = "Assets/Test.txt"; /// File.Create("Assets/Test); /// filePath.DeleteFileIfExists(); /// // 结果为,删除了 Test.txt /// /// /// /// 是否进行了删除操作 public static bool DeleteFileIfExists(this string fileFullPath) { if (File.Exists(fileFullPath)) { File.Delete(fileFullPath); return true; } return false; } /// /// 合并路径 /// /// // 示例: /// Application.dataPath.CombinePath("Resources").LogInfo(); // /projectPath/Assets/Resources /// /// /// /// /// 合并后的路径 public static string CombinePath(this string selfPath, string toCombinePath) { return Path.Combine(selfPath, toCombinePath); } #region 未经过测试 /// /// 打开文件夹 /// /// public static void OpenFolder(string path) { #if UNITY_STANDALONE_OSX System.Diagnostics.Process.Start("open", path); #elif UNITY_STANDALONE_WIN System.Diagnostics.Process.Start("explorer.exe", path); #endif } /// /// 获取文件夹名 /// /// /// public static string GetDirectoryName(string fileName) { fileName = MakePathStandard(fileName); return fileName.Substring(0, fileName.LastIndexOf('/')); } /// /// 获取文件名 /// /// /// /// public static string GetFileName(string path, char separator = '/') { path = IOExtension.MakePathStandard(path); return path.Substring(path.LastIndexOf(separator) + 1); } /// /// 获取不带后缀的文件名 /// /// /// /// public static string GetFileNameWithoutExtention(string fileName, char separator = '/') { return GetFilePathWithoutExtention(GetFileName(fileName, separator)); } /// /// 获取不带后缀的文件路径 /// /// /// public static string GetFilePathWithoutExtention(string fileName) { if (fileName.Contains(".")) return fileName.Substring(0, fileName.LastIndexOf('.')); return fileName; } /// /// 使目录存在,Path可以是目录名必须是文件名 /// /// public static void MakeFileDirectoryExist(string path) { string root = Path.GetDirectoryName(path); if (!Directory.Exists(root)) { Directory.CreateDirectory(root); } } /// /// 使目录存在 /// /// public static void MakeDirectoryExist(string path) { if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } } /// /// 获取父文件夹 /// /// /// public static string GetPathParentFolder(this string path) { if (string.IsNullOrEmpty(path)) { return string.Empty; } return Path.GetDirectoryName(path); } /// /// 使路径标准化,去除空格并将所有'\'转换为'/' /// /// /// public static string MakePathStandard(string path) { return path.Trim().Replace("\\", "/"); } public static List GetDirSubFilePathList(this string dirABSPath, bool isRecursive = true, string suffix = "") { var pathList = new List(); var di = new DirectoryInfo(dirABSPath); if (!di.Exists) { return pathList; } var files = di.GetFiles(); foreach (var fi in files) { if (!string.IsNullOrEmpty(suffix)) { if (!fi.FullName.EndsWith(suffix, System.StringComparison.CurrentCultureIgnoreCase)) { continue; } } pathList.Add(fi.FullName); } if (isRecursive) { var dirs = di.GetDirectories(); foreach (var d in dirs) { pathList.AddRange(GetDirSubFilePathList(d.FullName, isRecursive, suffix)); } } return pathList; } public static List GetDirSubDirNameList(this string dirABSPath) { var di = new DirectoryInfo(dirABSPath); var dirs = di.GetDirectories(); return dirs.Select(d => d.Name).ToList(); } public static string GetFileName(this string absOrAssetsPath) { var name = absOrAssetsPath.Replace("\\", "/"); var lastIndex = name.LastIndexOf("/"); return lastIndex >= 0 ? name.Substring(lastIndex + 1) : name; } public static string GetFileNameWithoutExtend(this string absOrAssetsPath) { var fileName = GetFileName(absOrAssetsPath); var lastIndex = fileName.LastIndexOf("."); return lastIndex >= 0 ? fileName.Substring(0, lastIndex) : fileName; } public static string GetFileExtendName(this string absOrAssetsPath) { var lastIndex = absOrAssetsPath.LastIndexOf("."); if (lastIndex >= 0) { return absOrAssetsPath.Substring(lastIndex); } return string.Empty; } public static string GetDirPath(this string absOrAssetsPath) { var name = absOrAssetsPath.Replace("\\", "/"); var lastIndex = name.LastIndexOf("/"); return name.Substring(0, lastIndex + 1); } public static string GetLastDirName(this string absOrAssetsPath) { var name = absOrAssetsPath.Replace("\\", "/"); var dirs = name.Split('/'); return absOrAssetsPath.EndsWith("/") ? dirs[dirs.Length - 2] : dirs[dirs.Length - 1]; } #endregion } /// /// 简单的概率计算 /// public static class ProbilityHelper { public static T RandomValueFrom(params T[] values) { return values[UnityEngine.Random.Range(0, values.Length)]; } /// /// percent probability /// /// 0 ~ 100 /// public static bool PercentProbability(int percent) { return UnityEngine.Random.Range(0, 1000) * 0.001f < 50 * 0.01f; } } /// /// 面向对象扩展(继承、封装、多态) /// public static class OOPExtension { interface ExampleInterface { } public static void Example() { if (typeof(OOPExtension).ImplementsInterface()) { } if (new object().ImplementsInterface()) { } } /// /// Determines whether the type implements the specified interface /// and is not an interface itself. /// /// true, if interface was implementsed, false otherwise. /// Type. /// The 1st type parameter. public static bool ImplementsInterface(this Type type) { return !type.IsInterface && type.GetInterfaces().Contains(typeof(T)); } /// /// Determines whether the type implements the specified interface /// and is not an interface itself. /// /// true, if interface was implementsed, false otherwise. /// Type. /// The 1st type parameter. public static bool ImplementsInterface(this object obj) { var type = obj.GetType(); return !type.IsInterface && type.GetInterfaces().Contains(typeof(T)); } } /// /// 程序集工具 /// public class AssemblyUtil { /// /// 获取 Assembly-CSharp 程序集 /// public static Assembly DefaultCSharpAssembly { get { return AppDomain.CurrentDomain.GetAssemblies() .SingleOrDefault(a => a.GetName().Name == "Assembly-CSharp"); } } /// /// 获取默认的程序集中的类型 /// /// /// public static Type GetDefaultAssemblyType(string typeName) { return DefaultCSharpAssembly.GetType(typeName); } } /// /// 反射扩展 /// public static class ReflectionExtension { public static void Example() { // var selfType = ReflectionExtension.GetAssemblyCSharp().GetType("QFramework.ReflectionExtension"); // selfType.LogInfo(); } public static Assembly GetAssemblyCSharp() { var assemblies = AppDomain.CurrentDomain.GetAssemblies(); foreach (var a in assemblies) { if (a.FullName.StartsWith("Assembly-CSharp,")) { return a; } } // Log.E(">>>>>>>Error: Can\'t find Assembly-CSharp.dll"); return null; } public static Assembly GetAssemblyCSharpEditor() { var assemblies = AppDomain.CurrentDomain.GetAssemblies(); foreach (var a in assemblies) { if (a.FullName.StartsWith("Assembly-CSharp-Editor,")) { return a; } } // Log.E(">>>>>>>Error: Can\'t find Assembly-CSharp-Editor.dll"); return null; } /// /// 通过反射方式调用函数 /// /// /// 方法名 /// 参数 /// public static object InvokeByReflect(this object obj, string methodName, params object[] args) { var methodInfo = obj.GetType().GetMethod(methodName); return methodInfo == null ? null : methodInfo.Invoke(obj, args); } /// /// 通过反射方式获取域值 /// /// /// 域名 /// public static object GetFieldByReflect(this object obj, string fieldName) { var fieldInfo = obj.GetType().GetField(fieldName); return fieldInfo == null ? null : fieldInfo.GetValue(obj); } /// /// 通过反射方式获取属性 /// /// /// 属性名 /// public static object GetPropertyByReflect(this object obj, string propertyName, object[] index = null) { var propertyInfo = obj.GetType().GetProperty(propertyName); return propertyInfo == null ? null : propertyInfo.GetValue(obj, index); } /// /// 拥有特性 /// /// public static bool HasAttribute(this PropertyInfo prop, Type attributeType, bool inherit) { return prop.GetCustomAttributes(attributeType, inherit).Length > 0; } /// /// 拥有特性 /// /// public static bool HasAttribute(this FieldInfo field, Type attributeType, bool inherit) { return field.GetCustomAttributes(attributeType, inherit).Length > 0; } /// /// 拥有特性 /// /// public static bool HasAttribute(this Type type, Type attributeType, bool inherit) { return type.GetCustomAttributes(attributeType, inherit).Length > 0; } /// /// 拥有特性 /// /// public static bool HasAttribute(this MethodInfo method, Type attributeType, bool inherit) { return method.GetCustomAttributes(attributeType, inherit).Length > 0; } /// /// 获取第一个特性 /// public static T GetFirstAttribute(this MethodInfo method, bool inherit) where T : Attribute { var attrs = (T[])method.GetCustomAttributes(typeof(T), inherit); if (attrs != null && attrs.Length > 0) return attrs[0]; return null; } /// /// 获取第一个特性 /// public static T GetFirstAttribute(this FieldInfo field, bool inherit) where T : Attribute { var attrs = (T[])field.GetCustomAttributes(typeof(T), inherit); if (attrs != null && attrs.Length > 0) return attrs[0]; return null; } /// /// 获取第一个特性 /// public static T GetFirstAttribute(this PropertyInfo prop, bool inherit) where T : Attribute { var attrs = (T[])prop.GetCustomAttributes(typeof(T), inherit); if (attrs != null && attrs.Length > 0) return attrs[0]; return null; } /// /// 获取第一个特性 /// public static T GetFirstAttribute(this Type type, bool inherit) where T : Attribute { var attrs = (T[])type.GetCustomAttributes(typeof(T), inherit); if (attrs != null && attrs.Length > 0) return attrs[0]; return null; } } /// /// 类型扩展 /// public static class TypeEx { /// /// 获取默认值 /// /// /// public static object DefaultForType(this Type targetType) { return targetType.IsValueType ? Activator.CreateInstance(targetType) : null; } } /// /// 字符串扩展 /// public static class StringExtention { public static void Example() { var emptyStr = string.Empty; emptyStr.IsNotNullAndEmpty(); emptyStr.IsNullOrEmpty(); emptyStr = emptyStr.Append("appended").Append("1").ToString(); emptyStr.IsNullOrEmpty(); } /// /// Check Whether string is null or empty /// /// /// public static bool IsNullOrEmpty(this string selfStr) { return string.IsNullOrEmpty(selfStr); } /// /// Check Whether string is null or empty /// /// /// public static bool IsNotNullAndEmpty(this string selfStr) { return !string.IsNullOrEmpty(selfStr); } /// /// Check Whether string trim is null or empty /// /// /// public static bool IsTrimNotNullAndEmpty(this string selfStr) { return selfStr != null && !string.IsNullOrEmpty(selfStr.Trim()); } public static bool IsTrimNullOrEmpty(this string selfStr) { return selfStr == null || string.IsNullOrEmpty(selfStr.Trim()); } /// /// 缓存 /// private static readonly char[] mCachedSplitCharArray = { '.' }; /// /// Split /// /// /// /// public static string[] Split(this string selfStr, char splitSymbol) { mCachedSplitCharArray[0] = splitSymbol; return selfStr.Split(mCachedSplitCharArray); } /// /// 首字母大写 /// /// /// public static string UppercaseFirst(this string str) { return char.ToUpper(str[0]) + str.Substring(1); } /// /// 首字母小写 /// /// /// public static string LowercaseFirst(this string str) { return char.ToLower(str[0]) + str.Substring(1); } /// /// /// /// /// public static string ToUnixLineEndings(this string str) { return str.Replace("\r\n", "\n").Replace("\r", "\n"); } /// /// 转换成 CSV /// /// /// public static string ToCSV(this string[] values) { return string.Join(", ", values .Where(value => !string.IsNullOrEmpty(value)) .Select(value => value.Trim()) .ToArray() ); } public static string[] ArrayFromCSV(this string values) { return values .Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries) .Select(value => value.Trim()) .ToArray(); } public static string ToSpacedCamelCase(this string text) { var sb = new StringBuilder(text.Length * 2); sb.Append(char.ToUpper(text[0])); for (var i = 1; i < text.Length; i++) { if (char.IsUpper(text[i]) && text[i - 1] != ' ') { sb.Append(' '); } sb.Append(text[i]); } return sb.ToString(); } /// /// 有点不安全,编译器不会帮你排查错误。 /// /// /// /// public static string FillFormat(this string selfStr, params object[] args) { return string.Format(selfStr, args); } /// /// 添加前缀 /// /// /// /// public static StringBuilder Append(this string selfStr, string toAppend) { return new StringBuilder(selfStr).Append(toAppend); } /// /// 添加后缀 /// /// /// /// public static string AddPrefix(this string selfStr, string toPrefix) { return new StringBuilder(toPrefix).Append(selfStr).ToString(); } /// /// 格式化 /// /// /// /// /// public static StringBuilder AppendFormat(this string selfStr, string toAppend, params object[] args) { return new StringBuilder(selfStr).AppendFormat(toAppend, args); } /// /// 最后一个单词 /// /// /// public static string LastWord(this string selfUrl) { return selfUrl.Split('/').Last(); } /// /// 解析成数字类型 /// /// /// /// public static int ToInt(this string selfStr, int defaulValue = 0) { var retValue = defaulValue; return int.TryParse(selfStr, out retValue) ? retValue : defaulValue; } /// /// 解析到时间类型 /// /// /// /// public static DateTime ToDateTime(this string selfStr, DateTime defaultValue = default(DateTime)) { var retValue = defaultValue; return DateTime.TryParse(selfStr, out retValue) ? retValue : defaultValue; } /// /// 解析 Float 类型 /// /// /// /// public static float ToFloat(this string selfStr, float defaulValue = 0) { var retValue = defaulValue; return float.TryParse(selfStr, out retValue) ? retValue : defaulValue; } /// /// 是否存在中文字符 /// /// /// public static bool HasChinese(this string input) { return Regex.IsMatch(input, @"[\u4e00-\u9fa5]"); } /// /// 是否存在空格 /// /// /// public static bool HasSpace(this string input) { return input.Contains(" "); } /// /// 删除特定字符 /// /// /// /// public static string RemoveString(this string str, params string[] targets) { return targets.Aggregate(str, (current, t) => current.Replace(t, string.Empty)); } } public static class BehaviourExtension { public static void Example() { var gameObject = new GameObject(); var component = gameObject.GetComponent(); component.Enable(); // component.enabled = true component.Disable(); // component.enabled = false } public static T Enable(this T selfBehaviour) where T : Behaviour { selfBehaviour.enabled = true; return selfBehaviour; } public static T Disable(this T selfBehaviour) where T : Behaviour { selfBehaviour.enabled = false; return selfBehaviour; } } public static class CameraExtension { public static void Example() { var screenshotTexture2D = Camera.main.CaptureCamera(new Rect(0, 0, Screen.width, Screen.height)); Debug.Log(screenshotTexture2D); } public static Texture2D CaptureCamera(this Camera camera, Rect rect) { var renderTexture = new RenderTexture(Screen.width, Screen.height, 0); camera.targetTexture = renderTexture; camera.Render(); RenderTexture.active = renderTexture; var screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false); screenShot.ReadPixels(rect, 0, 0); screenShot.Apply(); camera.targetTexture = null; RenderTexture.active = null; UnityEngine.Object.Destroy(renderTexture); return screenShot; } } public static class ColorExtension { public static void Example() { var color = "#C5563CFF".HtmlStringToColor(); Log.I(color); } /// /// #C5563CFF -> 197.0f / 255,86.0f / 255,60.0f / 255 /// /// /// public static Color HtmlStringToColor(this string htmlString) { Color retColor; var parseSucceed = ColorUtility.TryParseHtmlString(htmlString, out retColor); return parseSucceed ? retColor : Color.black; } /// /// unity's color always new a color /// public static Color White = Color.white; } public static class GraphicExtension { public static void Example() { var gameObject = new GameObject(); var image = gameObject.AddComponent(); var rawImage = gameObject.AddComponent(); // image.color = new Color(image.color.r,image.color.g,image.color.b,1.0f); image.ColorAlpha(1.0f); rawImage.ColorAlpha(1.0f); } public static T ColorAlpha(this T selfGraphic, float alpha) where T : Graphic { var color = selfGraphic.color; color.a = alpha; selfGraphic.color = color; return selfGraphic; } } public static class ImageExtension { public static void Example() { var gameObject = new GameObject(); var image1 = gameObject.AddComponent(); image1.FillAmount(0.0f); // image1.fillAmount = 0.0f; } public static Image FillAmount(this Image selfImage, float fillamount) { selfImage.fillAmount = fillamount; return selfImage; } } public static class LightmapExtension { public static void SetAmbientLightHTMLStringColor(string htmlStringColor) { RenderSettings.ambientLight = htmlStringColor.HtmlStringToColor(); } } public static class ObjectExtension { public static void Example() { var gameObject = new GameObject(); gameObject.Instantiate() .Name("ExtensionExample") .DestroySelf(); gameObject.Instantiate() .DestroySelfGracefully(); gameObject.Instantiate() .DestroySelfAfterDelay(1.0f); gameObject.Instantiate() .DestroySelfAfterDelayGracefully(1.0f); gameObject .ApplySelfTo(selfObj => Log.I(selfObj.name)) .Name("TestObj") .ApplySelfTo(selfObj => Log.I(selfObj.name)) .Name("ExtensionExample") .DontDestroyOnLoad(); } #region CEUO001 Instantiate public static T Instantiate(this T selfObj) where T : UnityEngine.Object { return UnityEngine.Object.Instantiate(selfObj); } public static T Instantiate(this T selfObj, Vector3 position, Quaternion rotation) where T : UnityEngine.Object { return UnityEngine.Object.Instantiate(selfObj, position, rotation); } public static T Instantiate( this T selfObj, Vector3 position, Quaternion rotation, Transform parent) where T : UnityEngine.Object { return UnityEngine.Object.Instantiate(selfObj, position, rotation, parent); } public static T InstantiateWithParent(this T selfObj, Transform parent, bool worldPositionStays) where T : UnityEngine.Object { return (T)UnityEngine.Object.Instantiate((UnityEngine.Object)selfObj, parent, worldPositionStays); } public static T InstantiateWithParent(this T selfObj, Transform parent) where T : UnityEngine.Object { return UnityEngine.Object.Instantiate(selfObj, parent, false); } #endregion #region CEUO002 Name public static T Name(this T selfObj, string name) where T : UnityEngine.Object { selfObj.name = name; return selfObj; } #endregion #region CEUO003 Destroy Self public static void DestroySelf(this T selfObj) where T : UnityEngine.Object { UnityEngine.Object.Destroy(selfObj); } public static T DestroySelfGracefully(this T selfObj) where T : UnityEngine.Object { if (selfObj) { UnityEngine.Object.Destroy(selfObj); } return selfObj; } #endregion #region CEUO004 Destroy Self AfterDelay public static T DestroySelfAfterDelay(this T selfObj, float afterDelay) where T : UnityEngine.Object { UnityEngine.Object.Destroy(selfObj, afterDelay); return selfObj; } public static T DestroySelfAfterDelayGracefully(this T selfObj, float delay) where T : UnityEngine.Object { if (selfObj) { UnityEngine.Object.Destroy(selfObj, delay); } return selfObj; } #endregion #region CEUO005 Apply Self To public static T ApplySelfTo(this T selfObj, System.Action toFunction) where T : UnityEngine.Object { toFunction.InvokeGracefully(selfObj); return selfObj; } #endregion #region CEUO006 DontDestroyOnLoad public static T DontDestroyOnLoad(this T selfObj) where T : UnityEngine.Object { UnityEngine.Object.DontDestroyOnLoad(selfObj); return selfObj; } #endregion public static T As(this object selfObj) where T : class { return selfObj as T; } } public static class RectTransformExtension { public static Vector2 GetPosInRootTrans(this RectTransform selfRectTransform, Transform rootTrans) { return RectTransformUtility.CalculateRelativeRectTransformBounds(rootTrans, selfRectTransform).center; } public static RectTransform AnchorPosX(this RectTransform selfRectTrans, float anchorPosX) { var anchorPos = selfRectTrans.anchoredPosition; anchorPos.x = anchorPosX; selfRectTrans.anchoredPosition = anchorPos; return selfRectTrans; } public static RectTransform AnchorPosY(this RectTransform selfRectTrans, float anchorPosY) { var anchorPos = selfRectTrans.anchoredPosition; anchorPos.y = anchorPosY; selfRectTrans.anchoredPosition = anchorPos; return selfRectTrans; } public static RectTransform SetSizeWidth(this RectTransform selfRectTrans, float sizeWidth) { var sizeDelta = selfRectTrans.sizeDelta; sizeDelta.x = sizeWidth; selfRectTrans.sizeDelta = sizeDelta; return selfRectTrans; } public static RectTransform SetSizeHeight(this RectTransform selfRectTrans, float sizeHeight) { var sizeDelta = selfRectTrans.sizeDelta; sizeDelta.y = sizeHeight; selfRectTrans.sizeDelta = sizeDelta; return selfRectTrans; } public static Vector2 GetWorldSize(this RectTransform selfRectTrans) { return RectTransformUtility.CalculateRelativeRectTransformBounds(selfRectTrans).size; } } public static class SelectableExtension { public static T EnableInteract(this T selfSelectable) where T : Selectable { selfSelectable.interactable = true; return selfSelectable; } public static T DisableInteract(this T selfSelectable) where T : Selectable { selfSelectable.interactable = false; return selfSelectable; } public static T CancalAllTransitions(this T selfSelectable) where T : Selectable { selfSelectable.transition = Selectable.Transition.None; return selfSelectable; } } public static class ToggleExtension { public static void RegOnValueChangedEvent(this Toggle selfToggle, UnityAction onValueChangedEvent) { selfToggle.onValueChanged.AddListener(onValueChangedEvent); } } /// /// Transform's Extension /// public static class TransformExtension { public static void Example() { var selfScript = new GameObject().AddComponent(); var transform = selfScript.transform; transform // .Parent(null) .LocalIdentity() .LocalPositionIdentity() .LocalRotationIdentity() .LocalScaleIdentity() .LocalPosition(Vector3.zero) .LocalPosition(0, 0, 0) .LocalPosition(0, 0) .LocalPositionX(0) .LocalPositionY(0) .LocalPositionZ(0) .LocalRotation(Quaternion.identity) .LocalScale(Vector3.one) .LocalScaleX(1.0f) .LocalScaleY(1.0f) .Identity() .PositionIdentity() .RotationIdentity() .Position(Vector3.zero) .PositionX(0) .PositionY(0) .PositionZ(0) .Rotation(Quaternion.identity) .DestroyChildren() .AsLastSibling() .AsFirstSibling() .SiblingIndex(0); selfScript // .Parent(null) .LocalIdentity() .LocalPositionIdentity() .LocalRotationIdentity() .LocalScaleIdentity() .LocalPosition(Vector3.zero) .LocalPosition(0, 0, 0) .LocalPosition(0, 0) .LocalPositionX(0) .LocalPositionY(0) .LocalPositionZ(0) .LocalRotation(Quaternion.identity) .LocalScale(Vector3.one) .LocalScaleX(1.0f) .LocalScaleY(1.0f) .Identity() .PositionIdentity() .RotationIdentity() .Position(Vector3.zero) .PositionX(0) .PositionY(0) .PositionZ(0) .Rotation(Quaternion.identity) .DestroyChildren() .AsLastSibling() .AsFirstSibling() .SiblingIndex(0); } /// /// 缓存的一些变量,免得每次声明 /// private static Vector3 mLocalPos; private static Vector3 mScale; private static Vector3 mPos; #region CETR001 Parent public static T Parent(this T selfComponent, Component parentComponent) where T : Component { selfComponent.transform.SetParent(parentComponent == null ? null : parentComponent.transform); return selfComponent; } /// /// 设置成为顶端 Transform /// /// The root transform. /// Self component. /// The 1st type parameter. public static T AsRootTransform(this T selfComponent) where T : Component { selfComponent.transform.SetParent(null); return selfComponent; } #endregion #region CETR002 LocalIdentity public static T LocalIdentity(this T selfComponent) where T : Component { selfComponent.transform.localPosition = Vector3.zero; selfComponent.transform.localRotation = Quaternion.identity; selfComponent.transform.localScale = Vector3.one; return selfComponent; } #endregion #region CETR003 LocalPosition public static T LocalPosition(this T selfComponent, Vector3 localPos) where T : Component { selfComponent.transform.localPosition = localPos; return selfComponent; } public static Vector3 GetLocalPosition(this T selfComponent) where T : Component { return selfComponent.transform.localPosition; } public static T LocalPosition(this T selfComponent, float x, float y, float z) where T : Component { selfComponent.transform.localPosition = new Vector3(x, y, z); return selfComponent; } public static T LocalPosition(this T selfComponent, float x, float y) where T : Component { mLocalPos = selfComponent.transform.localPosition; mLocalPos.x = x; mLocalPos.y = y; selfComponent.transform.localPosition = mLocalPos; return selfComponent; } public static T LocalPositionX(this T selfComponent, float x) where T : Component { mLocalPos = selfComponent.transform.localPosition; mLocalPos.x = x; selfComponent.transform.localPosition = mLocalPos; return selfComponent; } public static T LocalPositionY(this T selfComponent, float y) where T : Component { mLocalPos = selfComponent.transform.localPosition; mLocalPos.y = y; selfComponent.transform.localPosition = mLocalPos; return selfComponent; } public static T LocalPositionZ(this T selfComponent, float z) where T : Component { mLocalPos = selfComponent.transform.localPosition; mLocalPos.z = z; selfComponent.transform.localPosition = mLocalPos; return selfComponent; } public static T LocalPositionIdentity(this T selfComponent) where T : Component { selfComponent.transform.localPosition = Vector3.zero; return selfComponent; } #endregion #region CETR004 LocalRotation public static Quaternion GetLocalRotation(this T selfComponent) where T : Component { return selfComponent.transform.localRotation; } public static T LocalRotation(this T selfComponent, Quaternion localRotation) where T : Component { selfComponent.transform.localRotation = localRotation; return selfComponent; } public static T LocalRotationIdentity(this T selfComponent) where T : Component { selfComponent.transform.localRotation = Quaternion.identity; return selfComponent; } #endregion #region CETR005 LocalScale public static T LocalScale(this T selfComponent, Vector3 scale) where T : Component { selfComponent.transform.localScale = scale; return selfComponent; } public static Vector3 GetLocalScale(this T selfComponent) where T : Component { return selfComponent.transform.localScale; } public static T LocalScale(this T selfComponent, float xyz) where T : Component { selfComponent.transform.localScale = Vector3.one * xyz; return selfComponent; } public static T LocalScale(this T selfComponent, float x, float y, float z) where T : Component { mScale = selfComponent.transform.localScale; mScale.x = x; mScale.y = y; mScale.z = z; selfComponent.transform.localScale = mScale; return selfComponent; } public static T LocalScale(this T selfComponent, float x, float y) where T : Component { mScale = selfComponent.transform.localScale; mScale.x = x; mScale.y = y; selfComponent.transform.localScale = mScale; return selfComponent; } public static T LocalScaleX(this T selfComponent, float x) where T : Component { mScale = selfComponent.transform.localScale; mScale.x = x; selfComponent.transform.localScale = mScale; return selfComponent; } public static T LocalScaleY(this T selfComponent, float y) where T : Component { mScale = selfComponent.transform.localScale; mScale.y = y; selfComponent.transform.localScale = mScale; return selfComponent; } public static T LocalScaleZ(this T selfComponent, float z) where T : Component { mScale = selfComponent.transform.localScale; mScale.z = z; selfComponent.transform.localScale = mScale; return selfComponent; } public static T LocalScaleIdentity(this T selfComponent) where T : Component { selfComponent.transform.localScale = Vector3.one; return selfComponent; } #endregion #region CETR006 Identity public static T Identity(this T selfComponent) where T : Component { selfComponent.transform.position = Vector3.zero; selfComponent.transform.rotation = Quaternion.identity; selfComponent.transform.localScale = Vector3.one; return selfComponent; } #endregion #region CETR007 Position public static T Position(this T selfComponent, Vector3 position) where T : Component { selfComponent.transform.position = position; return selfComponent; } public static Vector3 GetPosition(this T selfComponent) where T : Component { return selfComponent.transform.position; } public static T Position(this T selfComponent, float x, float y, float z) where T : Component { selfComponent.transform.position = new Vector3(x, y, z); return selfComponent; } public static T Position(this T selfComponent, float x, float y) where T : Component { mPos = selfComponent.transform.position; mPos.x = x; mPos.y = y; selfComponent.transform.position = mPos; return selfComponent; } public static T PositionIdentity(this T selfComponent) where T : Component { selfComponent.transform.position = Vector3.zero; return selfComponent; } public static T PositionX(this T selfComponent, float x) where T : Component { mPos = selfComponent.transform.position; mPos.x = x; selfComponent.transform.position = mPos; return selfComponent; } public static T PositionX(this T selfComponent, Func xSetter) where T : Component { mPos = selfComponent.transform.position; mPos.x = xSetter(mPos.x); selfComponent.transform.position = mPos; return selfComponent; } public static T PositionY(this T selfComponent, float y) where T : Component { mPos = selfComponent.transform.position; mPos.y = y; selfComponent.transform.position = mPos; return selfComponent; } public static T PositionY(this T selfComponent, Func ySetter) where T : Component { mPos = selfComponent.transform.position; mPos.y = ySetter(mPos.y); selfComponent.transform.position = mPos; return selfComponent; } public static T PositionZ(this T selfComponent, float z) where T : Component { mPos = selfComponent.transform.position; mPos.z = z; selfComponent.transform.position = mPos; return selfComponent; } public static T PositionZ(this T selfComponent, Func zSetter) where T : Component { mPos = selfComponent.transform.position; mPos.z = zSetter(mPos.z); selfComponent.transform.position = mPos; return selfComponent; } #endregion #region CETR008 Rotation public static T RotationIdentity(this T selfComponent) where T : Component { selfComponent.transform.rotation = Quaternion.identity; return selfComponent; } public static T Rotation(this T selfComponent, Quaternion rotation) where T : Component { selfComponent.transform.rotation = rotation; return selfComponent; } public static Quaternion GetRotation(this T selfComponent) where T : Component { return selfComponent.transform.rotation; } #endregion #region CETR009 WorldScale/LossyScale/GlobalScale/Scale public static Vector3 GetGlobalScale(this T selfComponent) where T : Component { return selfComponent.transform.lossyScale; } public static Vector3 GetScale(this T selfComponent) where T : Component { return selfComponent.transform.lossyScale; } public static Vector3 GetWorldScale(this T selfComponent) where T : Component { return selfComponent.transform.lossyScale; } public static Vector3 GetLossyScale(this T selfComponent) where T : Component { return selfComponent.transform.lossyScale; } #endregion #region CETR010 Destroy All Child [Obsolete("弃用啦 请使用 DestroyChildren")] public static T DestroyAllChild(this T selfComponent) where T : Component { return selfComponent.DestroyChildren(); } [Obsolete("弃用啦 请使用 DestroyChildren")] public static GameObject DestroyAllChild(this GameObject selfGameObj) { return selfGameObj.DestroyChildren(); } public static T DestroyChildren(this T selfComponent) where T : Component { var childCount = selfComponent.transform.childCount; for (var i = 0; i < childCount; i++) { selfComponent.transform.GetChild(i).DestroyGameObjGracefully(); } return selfComponent; } public static GameObject DestroyChildren(this GameObject selfGameObj) { var childCount = selfGameObj.transform.childCount; for (var i = 0; i < childCount; i++) { selfGameObj.transform.GetChild(i).DestroyGameObjGracefully(); } return selfGameObj; } #endregion #region CETR011 Sibling Index public static T AsLastSibling(this T selfComponent) where T : Component { selfComponent.transform.SetAsLastSibling(); return selfComponent; } public static T AsFirstSibling(this T selfComponent) where T : Component { selfComponent.transform.SetAsFirstSibling(); return selfComponent; } public static T SiblingIndex(this T selfComponent, int index) where T : Component { selfComponent.transform.SetSiblingIndex(index); return selfComponent; } #endregion public static Transform FindByPath(this Transform selfTrans, string path) { return selfTrans.Find(path.Replace(".", "/")); } public static Transform SeekTrans(this Transform selfTransform, string uniqueName) { var childTrans = selfTransform.Find(uniqueName); if (null != childTrans) return childTrans; foreach (Transform trans in selfTransform) { childTrans = trans.SeekTrans(uniqueName); if (null != childTrans) return childTrans; } return null; } public static T ShowChildTransByPath(this T selfComponent, string tranformPath) where T : Component { selfComponent.transform.Find(tranformPath).gameObject.Show(); return selfComponent; } public static T HideChildTransByPath(this T selfComponent, string tranformPath) where T : Component { selfComponent.transform.Find(tranformPath).Hide(); return selfComponent; } public static void CopyDataFromTrans(this Transform selfTrans, Transform fromTrans) { selfTrans.SetParent(fromTrans.parent); selfTrans.localPosition = fromTrans.localPosition; selfTrans.localRotation = fromTrans.localRotation; selfTrans.localScale = fromTrans.localScale; } /// /// 递归遍历子物体,并调用函数 /// /// /// public static void ActionRecursion(this Transform tfParent, Action action) { action(tfParent); foreach (Transform tfChild in tfParent) { tfChild.ActionRecursion(action); } } /// /// 递归遍历查找指定的名字的子物体 /// /// 当前Transform /// 目标名 /// 字符串比较规则 /// public static Transform FindChildRecursion(this Transform tfParent, string name, StringComparison stringComparison = StringComparison.Ordinal) { if (tfParent.name.Equals(name, stringComparison)) { //Debug.Log("Hit " + tfParent.name); return tfParent; } foreach (Transform tfChild in tfParent) { Transform tfFinal = null; tfFinal = tfChild.FindChildRecursion(name, stringComparison); if (tfFinal) { return tfFinal; } } return null; } /// /// 递归遍历查找相应条件的子物体 /// /// 当前Transform /// 条件 /// public static Transform FindChildRecursion(this Transform tfParent, Func predicate) { if (predicate(tfParent)) { Log.I("Hit " + tfParent.name); return tfParent; } foreach (Transform tfChild in tfParent) { Transform tfFinal = null; tfFinal = tfChild.FindChildRecursion(predicate); if (tfFinal) { return tfFinal; } } return null; } public static string GetPath(this Transform transform) { var sb = new System.Text.StringBuilder(); var t = transform; while (true) { sb.Insert(0, t.name); t = t.parent; if (t) { sb.Insert(0, "/"); } else { return sb.ToString(); } } } } public static class UnityActionExtension { public static void Example() { UnityAction action = () => { }; UnityAction actionWithInt = num => { }; UnityAction actionWithIntString = (num, str) => { }; action.InvokeGracefully(); actionWithInt.InvokeGracefully(1); actionWithIntString.InvokeGracefully(1, "str"); } /// /// Call action /// /// /// call succeed public static bool InvokeGracefully(this UnityAction selfAction) { if (null != selfAction) { selfAction(); return true; } return false; } /// /// Call action /// /// /// /// public static bool InvokeGracefully(this UnityAction selfAction, T t) { if (null != selfAction) { selfAction(t); return true; } return false; } /// /// Call action /// /// /// call succeed public static bool InvokeGracefully(this UnityAction selfAction, T t, K k) { if (null != selfAction) { selfAction(t, k); return true; } return false; } /// /// 获得随机列表中元素 /// /// 元素类型 /// 列表 /// public static T GetRandomItem(this List list) { return list[UnityEngine.Random.Range(0, list.Count)]; } /// /// 根据权值来获取索引 /// /// /// public static int GetRandomWithPower(this List powers) { var sum = 0; foreach (var power in powers) { sum += power; } var randomNum = UnityEngine.Random.Range(0, sum); var currentSum = 0; for (var i = 0; i < powers.Count; i++) { var nextSum = currentSum + powers[i]; if (randomNum >= currentSum && randomNum <= nextSum) { return i; } currentSum = nextSum; } Log.E("权值范围计算错误!"); return -1; } /// /// 根据权值获取值,Key为值,Value为权值 /// /// /// /// public static T GetRandomWithPower(this Dictionary powersDict) { var keys = new List(); var values = new List(); foreach (var key in powersDict.Keys) { keys.Add(key); values.Add(powersDict[key]); } var finalKeyIndex = values.GetRandomWithPower(); return keys[finalKeyIndex]; } } public static class AnimatorExtension { public static void AddAnimatorParameterIfExists(this Animator animator, string parameterName, AnimatorControllerParameterType type, List parameterList) { if (animator.HasParameterOfType(parameterName, type)) { parameterList.Add(parameterName); } } // /// Updates the animator bool. /// /// Animator. /// Parameter name. /// If set to true value. public static void UpdateAnimatorBool(this Animator self, string parameterName, bool value, List parameterList) { if (parameterList.Contains(parameterName)) { self.SetBool(parameterName, value); } } public static void UpdateAnimatorTrigger(this Animator self, string parameterName, List parameterList) { if (parameterList.Contains(parameterName)) { self.SetTrigger(parameterName); } } /// /// Triggers an animator trigger. /// /// Animator. /// Parameter name. /// If set to true value. public static void SetAnimatorTrigger(this Animator self, string parameterName, List parameterList) { if (parameterList.Contains(parameterName)) { self.SetTrigger(parameterName); } } /// /// Updates the animator float. /// /// Animator. /// Parameter name. /// Value. public static void UpdateAnimatorFloat(this Animator self, string parameterName, float value, List parameterList) { if (parameterList.Contains(parameterName)) { self.SetFloat(parameterName, value); } } /// /// Updates the animator integer. /// /// self. /// Parameter name. /// Value. public static void UpdateAnimatorInteger(this Animator self, string parameterName, int value, List parameterList) { if (parameterList.Contains(parameterName)) { self.SetInteger(parameterName, value); } } // /// Updates the animator bool without checking the parameter's existence. /// /// self. /// Parameter name. /// If set to true value. public static void UpdateAnimatorBool(this Animator self, string parameterName, bool value) { self.SetBool(parameterName, value); } /// /// Updates the animator trigger without checking the parameter's existence /// /// self. /// Parameter name. public static void UpdateAnimatorTrigger(this Animator self, string parameterName) { self.SetTrigger(parameterName); } /// /// Triggers an animator trigger without checking for the parameter's existence. /// /// self. /// Parameter name. /// If set to true value. public static void SetAnimatorTrigger(this Animator self, string parameterName) { self.SetTrigger(parameterName); } /// /// Updates the animator float without checking for the parameter's existence. /// /// self. /// Parameter name. /// Value. public static void UpdateAnimatorFloat(this Animator self, string parameterName, float value) { self.SetFloat(parameterName, value); } /// /// Updates the animator integer without checking for the parameter's existence. /// /// self. /// Parameter name. /// Value. public static void UpdateAnimatorInteger(this Animator self, string parameterName, int value) { self.SetInteger(parameterName, value); } // /// Updates the animator bool after checking the parameter's existence. /// /// Animator. /// Parameter name. /// If set to true value. public static void UpdateAnimatorBoolIfExists(this Animator self, string parameterName, bool value) { if (self.HasParameterOfType(parameterName, AnimatorControllerParameterType.Bool)) { self.SetBool(parameterName, value); } } public static void UpdateAnimatorTriggerIfExists(this Animator self, string parameterName) { if (self.HasParameterOfType(parameterName, AnimatorControllerParameterType.Trigger)) { self.SetTrigger(parameterName); } } /// /// Triggers an animator trigger after checking for the parameter's existence. /// /// Animator. /// Parameter name. /// If set to true value. public static void SetAnimatorTriggerIfExists(this Animator self, string parameterName) { if (self.HasParameterOfType(parameterName, AnimatorControllerParameterType.Trigger)) { self.SetTrigger(parameterName); } } /// /// Updates the animator float after checking for the parameter's existence. /// /// Animator. /// Parameter name. /// Value. public static void UpdateAnimatorFloatIfExists(this Animator self, string parameterName, float value) { if (self.HasParameterOfType(parameterName, AnimatorControllerParameterType.Float)) { self.SetFloat(parameterName, value); } } /// /// Updates the animator integer after checking for the parameter's existence. /// /// Animator. /// Parameter name. /// Value. public static void UpdateAnimatorIntegerIfExists(this Animator self, string parameterName, int value) { if (self.HasParameterOfType(parameterName, AnimatorControllerParameterType.Int)) { self.SetInteger(parameterName, value); } } /// /// Determines if an animator contains a certain parameter, based on a type and a name /// /// true if has parameter of type the specified self name type; otherwise, false. /// Self. /// Name. /// Type. public static bool HasParameterOfType(this Animator self, string name, AnimatorControllerParameterType type) { if (string.IsNullOrEmpty(name)) { return false; } var parameters = self.parameters; return parameters.Any(currParam => currParam.type == type && currParam.name == name); } } /// /// GameObject's Util/Static This Extension /// public static class GameObjectExtension { public static void Example() { var gameObject = new GameObject(); var transform = gameObject.transform; var selfScript = gameObject.AddComponent(); var boxCollider = gameObject.AddComponent(); gameObject.Show(); // gameObject.SetActive(true) selfScript.Show(); // this.gameObject.SetActive(true) boxCollider.Show(); // boxCollider.gameObject.SetActive(true) gameObject.transform.Show(); // transform.gameObject.SetActive(true) gameObject.Hide(); // gameObject.SetActive(false) selfScript.Hide(); // this.gameObject.SetActive(false) boxCollider.Hide(); // boxCollider.gameObject.SetActive(false) transform.Hide(); // transform.gameObject.SetActive(false) selfScript.DestroyGameObj(); boxCollider.DestroyGameObj(); transform.DestroyGameObj(); selfScript.DestroyGameObjGracefully(); boxCollider.DestroyGameObjGracefully(); transform.DestroyGameObjGracefully(); selfScript.DestroyGameObjAfterDelay(1.0f); boxCollider.DestroyGameObjAfterDelay(1.0f); transform.DestroyGameObjAfterDelay(1.0f); selfScript.DestroyGameObjAfterDelayGracefully(1.0f); boxCollider.DestroyGameObjAfterDelayGracefully(1.0f); transform.DestroyGameObjAfterDelayGracefully(1.0f); gameObject.Layer(0); selfScript.Layer(0); boxCollider.Layer(0); transform.Layer(0); gameObject.Layer("Default"); selfScript.Layer("Default"); boxCollider.Layer("Default"); transform.Layer("Default"); } #region CEGO001 Show public static GameObject Show(this GameObject selfObj) { selfObj.SetActive(true); return selfObj; } public static T Show(this T selfComponent) where T : Component { selfComponent.gameObject.Show(); return selfComponent; } #endregion #region CEGO002 Hide public static GameObject Hide(this GameObject selfObj) { selfObj.SetActive(false); return selfObj; } public static T Hide(this T selfComponent) where T : Component { selfComponent.gameObject.Hide(); return selfComponent; } #endregion #region CEGO003 DestroyGameObj public static void DestroyGameObj(this T selfBehaviour) where T : Component { selfBehaviour.gameObject.DestroySelf(); } #endregion #region CEGO004 DestroyGameObjGracefully public static void DestroyGameObjGracefully(this T selfBehaviour) where T : Component { if (selfBehaviour && selfBehaviour.gameObject) { selfBehaviour.gameObject.DestroySelfGracefully(); } } #endregion #region CEGO005 DestroyGameObjGracefully public static T DestroyGameObjAfterDelay(this T selfBehaviour, float delay) where T : Component { selfBehaviour.gameObject.DestroySelfAfterDelay(delay); return selfBehaviour; } public static T DestroyGameObjAfterDelayGracefully(this T selfBehaviour, float delay) where T : Component { if (selfBehaviour && selfBehaviour.gameObject) { selfBehaviour.gameObject.DestroySelfAfterDelay(delay); } return selfBehaviour; } #endregion #region CEGO006 Layer public static GameObject Layer(this GameObject selfObj, int layer) { selfObj.layer = layer; return selfObj; } public static T Layer(this T selfComponent, int layer) where T : Component { selfComponent.gameObject.layer = layer; return selfComponent; } public static GameObject Layer(this GameObject selfObj, string layerName) { selfObj.layer = LayerMask.NameToLayer(layerName); return selfObj; } public static T Layer(this T selfComponent, string layerName) where T : Component { selfComponent.gameObject.layer = LayerMask.NameToLayer(layerName); return selfComponent; } public static bool IsInLayerMask(this GameObject selfObj, LayerMask layerMask) { return LayerMaskUtility.IsInLayerMask(selfObj, layerMask); } public static bool IsInLayerMask(this T selfComponent, LayerMask layerMask) where T : Component { return LayerMaskUtility.IsInLayerMask(selfComponent.gameObject, layerMask); } #endregion #region CEGO007 Component public static T GetOrAddComponent(this GameObject selfComponent) where T : Component { var comp = selfComponent.gameObject.GetComponent(); return comp ? comp : selfComponent.gameObject.AddComponent(); } public static T GetOrAddComponent(this Component component) where T : Component { return component.gameObject.GetOrAddComponent(); } public static Component GetOrAddComponent(this GameObject selfComponent, Type type) { var comp = selfComponent.gameObject.GetComponent(type); return comp ? comp : selfComponent.gameObject.AddComponent(type); } #endregion } public static class LayerMaskExtension { public static bool ContainsGameObject(this LayerMask selfLayerMask, GameObject gameObject) { return LayerMaskUtility.IsInLayerMask(gameObject, selfLayerMask); } } public static class LayerMaskUtility { public static bool IsInLayerMask(GameObject gameObj, LayerMask layerMask) { // 根据Layer数值进行移位获得用于运算的Mask值 var objLayerMask = 1 << gameObj.layer; return (layerMask.value & objLayerMask) == objLayerMask; } } public static class MaterialExtension { /// /// 参考资料: https://blog.csdn.net/qiminixi/article/details/78402505 /// /// public static void SetStandardMaterialToTransparentMode(this Material self) { self.SetFloat("_Mode", 3); self.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha); self.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); self.SetInt("_ZWrite", 0); self.DisableKeyword("_ALPHATEST_ON"); self.EnableKeyword("_ALPHABLEND_ON"); self.DisableKeyword("_ALPHAPREMULTIPLY_ON"); self.renderQueue = 3000; } } public static class TextureExtensions { public static Sprite CreateSprite(this Texture2D self) { return Sprite.Create(self, new Rect(0, 0, self.width, self.height), Vector2.one * 0.5f); } } internal static class Log { public enum LogLevel { None = 0, Exception = 1, Error = 2, Warning = 3, Normal = 4, Max = 5, } internal static void LogInfo(this object selfMsg) { I(selfMsg); } internal static void LogWarning(this object selfMsg) { W(selfMsg); } internal static void LogError(this object selfMsg) { E(selfMsg); } internal static void LogException(this Exception selfExp) { E(selfExp); } private static LogLevel mLogLevel = LogLevel.Normal; public static LogLevel Level { get { return mLogLevel; } set { mLogLevel = value; } } internal static void I(object msg, params object[] args) { if (mLogLevel < LogLevel.Normal) { return; } if (args == null || args.Length == 0) { Debug.Log(msg); } else { Debug.LogFormat(msg.ToString(), args); } } internal static void E(Exception e) { if (mLogLevel < LogLevel.Exception) { return; } Debug.LogException(e); } internal static void E(object msg, params object[] args) { if (mLogLevel < LogLevel.Error) { return; } if (args == null || args.Length == 0) { Debug.LogError(msg); } else { Debug.LogError(string.Format(msg.ToString(), args)); } } internal static void W(object msg) { if (mLogLevel < LogLevel.Warning) { return; } Debug.LogWarning(msg); } internal static void W(string msg, params object[] args) { if (mLogLevel < LogLevel.Warning) { return; } Debug.LogWarning(string.Format(msg, args)); } } }