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



