有时候我们想给unity中的button或者gameobject定制一些工具类函数,这时候便可以定义一个专门的静态工具类去承载这些静态工具函数,具体实现如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test: MonoBehaviour
{
void Start()
{
gameObject.SetActiveEx(true);
}
}
public static class GameObjectExtension//必须是静态类
{
public static void SetActiveEx(this GameObject g, bool t)
{
if (null == g) return;
g.SetActive(t);
}
}
声明了静态拓展函数后IDE会自动为我们提示该函数,通过自定义拓展函数可以有效提高开发效率。