diff --git a/Assets/DemoGame/GameScript/Hotfix/Helper/ColorExtend.cs b/Assets/DemoGame/GameScript/Hotfix/Helper/ColorExtend.cs new file mode 100644 index 0000000..5630227 --- /dev/null +++ b/Assets/DemoGame/GameScript/Hotfix/Helper/ColorExtend.cs @@ -0,0 +1,39 @@ +using System.Collections; +using System.Collections.Generic; +using System.Runtime.CompilerServices; +using UnityEngine; + +public static class ColorExtend +{ + + public static string ColorToHex(this Color c) + { + return "#" + ColorUtility.ToHtmlStringRGBA(c); + } + + public static string ColorToHexRGB(this Color c) + { + return "#" + ColorUtility.ToHtmlStringRGB(c); + } + + public static Color HexToColor(this string Self) + { + if (ColorUtility.TryParseHtmlString(Self, out Color color)) + { + return color; + } + return Color.white; + } + + /// + /// 设置透明的,从根源下手 有color的都能设置 + /// + /// + /// + public static void SetAlpha(this UnityEngine.UI.Graphic component, float alpha) + { + Color temp = component.color; + component.color = new Color(temp.r, temp.g, temp.b, alpha); + } + +} diff --git a/Assets/DemoGame/GameScript/Hotfix/Helper/ColorExtend.cs.meta b/Assets/DemoGame/GameScript/Hotfix/Helper/ColorExtend.cs.meta new file mode 100644 index 0000000..342998a --- /dev/null +++ b/Assets/DemoGame/GameScript/Hotfix/Helper/ColorExtend.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 52ae0aef13d2f144dada00649ee8e196 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/DemoGame/GameScript/Hotfix/Helper/ComponentExtend.cs b/Assets/DemoGame/GameScript/Hotfix/Helper/ComponentExtend.cs new file mode 100644 index 0000000..d2bc111 --- /dev/null +++ b/Assets/DemoGame/GameScript/Hotfix/Helper/ComponentExtend.cs @@ -0,0 +1,94 @@ +using UnityEngine; + +/// +/// 组件拓展 +/// +public static class ComponentExtend +{ + /// + /// 获取某对象下的子物体,适用于多层级父子关系的查找 + /// + /// 子物体名称 + /// + /// + public static Transform FindChildByName(this Component self, string objName) + { + if (self == null || string.IsNullOrEmpty(objName)) return null; + + Transform[] children = self.GetComponentsInChildren(true); + + for (int i = 0; i < children.Length; i++) + { + if (children[i].name.Equals(objName)) + return children[i]; + } + + return null; + } + + /// + /// 获取某物体下的子物体上的某个组件,适用于多层级父子关系的查找 + /// + /// 组件类型 + /// + /// 子物体名称 + /// + public static T GetComponentByChildName(this Component self, string objNmae) where T : Component + { + Transform tf = FindChildByName(self, objNmae); + if (tf != null) + return tf.GetComponent(); + else + return null; + } + + /// + /// 自动检查并添加组件 + /// + /// 组件类型 + /// 添加组件的Component + /// 是否删除已有并重新添加组件 + /// + public static T AutoComponent(this Component self, bool CoverModel = false) where T : Component + { + if (CoverModel) + { + if (self.TryGetComponent(out T destroy)) + Object.Destroy(destroy); + + return self.gameObject.AddComponent(); + } + else + { + if (self.TryGetComponent(out T component)) + return component; + else + return self.gameObject.AddComponent(); + } + } + + /// + /// 自动检查并添加组件 + /// + /// 组件类型 + /// 添加组件的GameObject + /// 是否删除已有并重新添加组件 + /// + public static T AutoComponent(this GameObject self, bool CoverModel = false) where T : Component + { + if (CoverModel) + { + if (self.TryGetComponent(out T destroy)) + Object.Destroy(destroy); + return self.gameObject.AddComponent(); + } + else + { + if(self.TryGetComponent(out T component)) + return component; + else + return self.gameObject.AddComponent(); + } + } + +} diff --git a/Assets/DemoGame/GameScript/Hotfix/Helper/ComponentExtend.cs.meta b/Assets/DemoGame/GameScript/Hotfix/Helper/ComponentExtend.cs.meta new file mode 100644 index 0000000..8ec7333 --- /dev/null +++ b/Assets/DemoGame/GameScript/Hotfix/Helper/ComponentExtend.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0542da760f800f649ae9db2d6729dbc9 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/DemoGame/GameScript/Hotfix/Helper/EventTriggerExtend.cs b/Assets/DemoGame/GameScript/Hotfix/Helper/EventTriggerExtend.cs new file mode 100644 index 0000000..e764675 --- /dev/null +++ b/Assets/DemoGame/GameScript/Hotfix/Helper/EventTriggerExtend.cs @@ -0,0 +1,35 @@ + +using UnityEngine.Events; +using UnityEngine.EventSystems; + +public static class EventTriggerExtend +{ + + /// + /// 添加事件 + /// + /// 组件 + /// 事件类型 + /// 回调 + public static void AddEvent(this EventTrigger self, EventTriggerType eventType, UnityAction callBack) + { + if (self.triggers.Count != 0) + { + for (int i = 0; i < self.triggers.Count; i++) + { + if (self.triggers[i].eventID == eventType) + { + self.triggers[i].callback.AddListener(callBack); + return; + } + } + } + + EventTrigger.Entry entry = new EventTrigger.Entry(); + entry.eventID = eventType; + entry.callback.AddListener(callBack); + self.triggers.Add(entry); + } + + +} diff --git a/Assets/DemoGame/GameScript/Hotfix/Helper/EventTriggerExtend.cs.meta b/Assets/DemoGame/GameScript/Hotfix/Helper/EventTriggerExtend.cs.meta new file mode 100644 index 0000000..71dd363 --- /dev/null +++ b/Assets/DemoGame/GameScript/Hotfix/Helper/EventTriggerExtend.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4ae9dd61c800ef04fb99870d74c0a9cf +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: