添加一点帮助类
parent
f3d3aeca5e
commit
1bd7da7824
|
@ -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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置透明的,从根源下手 有color的都能设置
|
||||
/// </summary>
|
||||
/// <param name="component"></param>
|
||||
/// <param name="alpha"></param>
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 52ae0aef13d2f144dada00649ee8e196
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,94 @@
|
|||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// 组件拓展
|
||||
/// </summary>
|
||||
public static class ComponentExtend
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取某对象下的子物体,适用于多层级父子关系的查找
|
||||
/// </summary>
|
||||
/// <param name="self">子物体名称</param>
|
||||
/// <param name="objName"></param>
|
||||
/// <returns></returns>
|
||||
public static Transform FindChildByName(this Component self, string objName)
|
||||
{
|
||||
if (self == null || string.IsNullOrEmpty(objName)) return null;
|
||||
|
||||
Transform[] children = self.GetComponentsInChildren<Transform>(true);
|
||||
|
||||
for (int i = 0; i < children.Length; i++)
|
||||
{
|
||||
if (children[i].name.Equals(objName))
|
||||
return children[i];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取某物体下的子物体上的某个组件,适用于多层级父子关系的查找
|
||||
/// </summary>
|
||||
/// <typeparam name="T">组件类型</typeparam>
|
||||
/// <param name="self"></param>
|
||||
/// <param name="objNmae">子物体名称</param>
|
||||
/// <returns></returns>
|
||||
public static T GetComponentByChildName<T>(this Component self, string objNmae) where T : Component
|
||||
{
|
||||
Transform tf = FindChildByName(self, objNmae);
|
||||
if (tf != null)
|
||||
return tf.GetComponent<T>();
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 自动检查并添加组件
|
||||
/// </summary>
|
||||
/// <typeparam name="T">组件类型</typeparam>
|
||||
/// <param name="self">添加组件的Component</param>
|
||||
/// <param name="CoverModel">是否删除已有并重新添加组件</param>
|
||||
/// <returns></returns>
|
||||
public static T AutoComponent<T>(this Component self, bool CoverModel = false) where T : Component
|
||||
{
|
||||
if (CoverModel)
|
||||
{
|
||||
if (self.TryGetComponent(out T destroy))
|
||||
Object.Destroy(destroy);
|
||||
|
||||
return self.gameObject.AddComponent<T>();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (self.TryGetComponent(out T component))
|
||||
return component;
|
||||
else
|
||||
return self.gameObject.AddComponent<T>();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 自动检查并添加组件
|
||||
/// </summary>
|
||||
/// <typeparam name="T">组件类型</typeparam>
|
||||
/// <param name="self">添加组件的GameObject</param>
|
||||
/// <param name="CoverModel">是否删除已有并重新添加组件</param>
|
||||
/// <returns></returns>
|
||||
public static T AutoComponent<T>(this GameObject self, bool CoverModel = false) where T : Component
|
||||
{
|
||||
if (CoverModel)
|
||||
{
|
||||
if (self.TryGetComponent(out T destroy))
|
||||
Object.Destroy(destroy);
|
||||
return self.gameObject.AddComponent<T>();
|
||||
}
|
||||
else
|
||||
{
|
||||
if(self.TryGetComponent(out T component))
|
||||
return component;
|
||||
else
|
||||
return self.gameObject.AddComponent<T>();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 0542da760f800f649ae9db2d6729dbc9
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,35 @@
|
|||
|
||||
using UnityEngine.Events;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
public static class EventTriggerExtend
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 添加事件
|
||||
/// </summary>
|
||||
/// <param name="self">组件</param>
|
||||
/// <param name="eventType">事件类型</param>
|
||||
/// <param name="callBack">回调</param>
|
||||
public static void AddEvent(this EventTrigger self, EventTriggerType eventType, UnityAction<BaseEventData> 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);
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 4ae9dd61c800ef04fb99870d74c0a9cf
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Loading…
Reference in New Issue