Unity常用的一些简易扩展方法
引言
在unity项目的日常开发中,会发现unity现有的一些类的方法不是特别够用,但有些方法使用频率又很高,这时候,我们就可以用到Extension Method 扩展方法,实现自定义的扩展方法。
其次,扩展方法也可以使写法更简便一些。
附:Unity官方教程:教程
实现方法
步骤一
创建一个静态类,没有任何继承。
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// unity扩展类
/// image
/// </summary>
public static class ImageExtension
{
}
步骤二
创建一个静态方法,这个方法的第一个参数使用this,类型为你需要扩展的类名,如Transform,Image等,之后的参数根据自己需求添加。
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// Unity扩展类
/// Image
/// </summary>
public static class ImageExtension
{
/// <summary>
/// 改变图片的透明度
/// </summary>
/// <param name="image"></param>
/// <param name="a">透明度,[0, 1]</param>
public static void ChangeAlpha(this Image image, float alpha)
{
Color oldColor = image.color;
image.color = new Color(oldColor.r, oldColor.g, oldColor.b, alpha);
}
}
使用方式
Image img;
img.ChangeAlpha(1);
常用的一些扩展
以下的扩展是我自己在使用的过程中,觉得比较好使的一些扩展。
图片Image
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// Unity扩展类
/// Image
/// </summary>
public static class ImageExtension
{
/// <summary>
/// 改变图片的透明度
/// </summary>
/// <param name="image"></param>
/// <param name="a">透明度,[0, 1]</param>
public static void ChangeAlpha(this Image image, float alpha)
{
Color oldColor = image.color;
image.color = new Color(oldColor.r, oldColor.g, oldColor.b, alpha);
}
}
Gameobject
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Unity扩展类
/// Gameobject
/// </summary>
public static class GameobjectExtension
{
/// <summary>
/// 改变对象的层级
/// </summary>
/// <param name="gameobject">对象</param>
/// <param name="layerName">层级名称</param>
public static void ChangeLayer(this GameObject gameobject, string layerName)
{
int layer = LayerMask.NameToLayer(layerName);
if (gameobject)
{
foreach (Transform traform in gameobject.GetComponentsInChildren<Transform>(true))
{
traform.gameObject.layer = layer;
}
}
}
/// <summary>
/// 改变对象的层级
/// </summary>
/// <param name="gameobject">对象</param>
/// <param name="layer">层级编号</param>
public static void ChangeLayer(this GameObject gameobject, int layer)
{
if (gameobject)
{
foreach (Transform traform in gameobject.GetComponentsInChildren<Transform>(true))
{
traform.gameObject.layer = layer;
}
}
}
/// <summary>
/// 给对象加上元件
/// 对象身上不存在这个元件
/// 存在则不作处理
/// </summary>
/// <typeparam name="T">元件</typeparam>
/// <param name="gameObject">对象</param>
/// <returns></returns>
public static T AddComponentIfNotFound<T>(this GameObject gameObject) where T : Component
{
T component = gameObject.GetComponent<T>();
if (component == null)
{
component = gameObject.AddComponent<T>();
}
return component;
}
/// <summary>
/// 移除物体上挂载的脚本
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="gameObject"></param>
public static void Remove<T>(this GameObject gameObject) where T : MonoBehaviour
{
if (gameObject.GetComponent<T>())
{
GameObject.Destroy(gameObject.GetComponent<T>());
}
}
}
MonoBehaviour
using System.Collections;
using UnityEngine;
using UnityEngine.Events;
/// <summary>
/// Unity扩展类
/// MonoBehaviour
/// </summary>
public static class MonoBehaviourExtension
{
/// <summary>
/// 起协程
/// 先等待延迟,然后运行事件
/// </summary>
/// <param name="action">事件</param>
/// <param name="time">延迟时间</param>
/// <returns></returns>
public static Coroutine WaitSomeTime(this MonoBehaviour mono, float time, UnityAction action)
{
return mono.StartCoroutine(WaitSomeTime(action, time));
}
private static IEnumerator WaitSomeTime(UnityAction action, float time)
{
yield return new WaitForSeconds(time);
if (action != null)
{
action();
}
}
/// <summary>
/// 起协程
/// 先等待延迟,然后运行事件
/// </summary>
/// <param name="action">事件</param>
/// <param name="time">延迟时间</param>
/// <returns></returns>
public static Coroutine WaitOneUpdate(this MonoBehaviour mono, UnityAction action)
{
return mono.StartCoroutine(WaitOneUpdate(action));
}
private static IEnumerator WaitOneUpdate(UnityAction action)
{
yield return null;
if (action != null)
{
action();
}
}
/// <summary>
/// 起协程
/// 先等待延迟,然后运行事件
/// </summary>
/// <param name="action">事件</param>
/// <param name="time">延迟时间</param>
/// <returns></returns>
public static Coroutine WaitEndOfFrame(this MonoBehaviour mono, UnityAction action)
{
return mono.StartCoroutine(WaitrEndOfFrame(action));
}
private static IEnumerator WaitrEndOfFrame(UnityAction action)
{
yield return null;
if (action != null)
{
action();
}
}
}
Transform
using UnityEngine;
using System.Collections.Generic;
/// <summary>
/// transform扩展类
/// </summary>
public static class TransformExtension
{
/// <summary>
/// 改变对象的层级
/// </summary>
/// <param name="tran">对象</param>
/// <param name="layerName">层级名称</param>
public static void ChangeLayer(this Transform tran, string layerName)
{
int layer = LayerMask.NameToLayer(layerName);
if (tran)
{
foreach (Transform traform in tran.GetComponentsInChildren<Transform>(true))
{
traform.gameObject.layer = layer;
}
}
}
/// <summary>
/// 改变对象的层级
/// </summary>
/// <param name="tran">对象</param>
/// <param name="layer">层级编号</param>
public static void ChangeLayer(this Transform tran, int layer)
{
if (tran)
{
foreach (Transform traform in tran.GetComponentsInChildren<Transform>(true))
{
traform.gameObject.layer = layer;
}
}
}
/// <summary>
/// 设置绝对位置的x坐标
/// </summary>
/// <param name="transform"><see cref="Transform" />对象</param>
/// <param name="newValue">x 坐标值</param>
public static void SetPositionX(this Transform transform, float newValue)
{
Vector3 originalPos = transform.position;
originalPos.x = newValue;
transform.position = originalPos;
}
/// <summary>
/// 设置绝对位置的y坐标
/// </summary>
/// <param name="transform"><see cref="Transform" />对象</param>
/// <param name="newValue">y 坐标值</param>
public static void SetPositionY(this Transform transform, float newValue)
{
Vector3 originalPos = transform.position;
originalPos.y = newValue;
transform.position = originalPos;
}
/// <summary>
/// 设置绝对位置的z坐标
/// </summary>
/// <param name="transform"><see cref="Transform" />对象</param>
/// <param name="newValue">z 坐标值</param>
public static void SetPositionZ(this Transform transform, float newValue)
{
Vector3 originalPos = transform.position;
originalPos.z = newValue;
transform.position = originalPos;
}
}