U3d:关于对方法的链式扩展
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Test7 : MonoBehaviour { public GameObject go; // Start is called before the first frame update void Start() { go.Show() .Hide() .Name("aa"); } // Update is called once per frame void Update() { } } public static class GameObjectExtension { public static GameObject Show(this GameObject selfObject) { selfObject.SetActive(true); return selfObject; } public static GameObject Hide(this GameObject selfObject) { selfObject.SetActive(false); return selfObject; } public static GameObject Name(this GameObject selfObject, string str) { selfObject.name = str; return selfObject; } }
扩展方法被定义为静态方法,但它们是通过实例方法语法进行调用的。它们的第一个参数指定该方法作用于哪个类型,并且该参数以 this 修饰符为前缀。仅当您使用 using 指令将命名空间显式导入到源代码中之后,扩展方法才位于范围中。

浙公网安备 33010602011771号