Unity 使用this关键字进行函数拓展 - GameObject

Example:

private void Start()
{
    var testComponent = gameObject
        .Activate()
        .Deactivate()
        .Name("")
        .Layer(0)
        .Layer("")
        .Tag("")
        .GetComponentForcibly(typeof(LineRenderer));

    bool isActive = testComponent
        .Activate()
        .Deactivate()
        .Name("")
        .Layer(0)
        .Layer("")
        .Tag("")
        .IsActive();

}

Extension:

/// <summary>
    /// The extension of gameobject.
    /// </summary>
    public static class GameObjectExtension
    {
        /// <summary>
        /// Activate the gameobject.
        /// </summary>
        /// <param name="self"></param>
        /// <returns></returns>
        public static GameObject Activate(this GameObject self)
        {
            self.SetActive(true);
            return self;
        }
        /// <summary>
        /// Deactivate the gameobject.
        /// </summary>
        /// <param name="self"></param>
        /// <returns></returns>
        public static GameObject Deactivate(this GameObject self)
        {
            self.SetActive(false);
            return self;
        }
        /// <summary>
        /// Set name for the gameobject.
        /// </summary>
        /// <param name="self"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static GameObject Name(this GameObject self, string name)
        {
            self.name = name;
            return self;
        }
        /// <summary>
        /// Set layer for the gameobject.
        /// </summary>
        /// <param name="self"></param>
        /// <param name="layer"></param>
        /// <returns></returns>
        public static GameObject Layer(this GameObject self, int layer)
        {
            self.layer = layer;
            return self;
        }
        /// <summary>
        /// Set layer for the gameobject.
        /// </summary>
        /// <param name="self"></param>
        /// <param name="layer"></param>
        /// <returns></returns>
        public static GameObject Layer(this GameObject self, string layer)
        {
            self.layer = LayerMask.NameToLayer(layer);
            return self;
        }
        /// <summary>
        /// Set tag for the gameobject.
        /// </summary>
        /// <param name="self"></param>
        /// <param name="tag"></param>
        /// <returns></returns>
        public static GameObject Tag(this GameObject self, string tag)
        {
            self.tag = tag;
            return self;
        }
        /// <summary>
        /// Get specified component, if it's null, add and return.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="self"></param>
        /// <returns></returns>
        public static T GetComponentForcibly<T>(this GameObject self) where T : Component
        {
            var component = self.GetComponent<T>();
            return component ?? self.AddComponent<T>();
        }
        /// <summary>
        /// Get specified component, if it's null, add and return.
        /// </summary>
        /// <param name="self"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static Component GetComponentForcibly(this GameObject self, Type type)
        {
            var component = self.GetComponent(type);
            return component ?? self.AddComponent(type);
        }
        /// <summary>
        /// Get the local active state of the compontent's gameObject.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="self"></param>
        /// <returns></returns>
        public static bool IsActive<T>(this T self) where T : Component
        {
            return self.gameObject.activeSelf;
        }
        /// <summary>
        /// Activate the component's gameobject.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="self"></param>
        /// <returns></returns>
        public static T Activate<T>(this T self) where T : Component
        {
            self.gameObject.SetActive(true);
            return self;
        }
        /// <summary>
        /// Deactivate the component's gameobject.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="self"></param>
        /// <returns></returns>
        public static T Deactivate<T>(this T self) where T : Component
        {
            self.gameObject.SetActive(false);
            return self;
        }
        /// <summary>
        /// Set name for the component's gameobject.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="self"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static T Name<T>(this T self, string name) where T : Component
        {
            self.gameObject.name = name;
            return self;
        }
        /// <summary>
        /// Set layer for the component's gameobject.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="self"></param>
        /// <param name="layer"></param>
        /// <returns></returns>
        public static T Layer<T>(this T self, int layer) where T : Component
        {
            self.gameObject.layer = layer;
            return self;
        }
        /// <summary>
        /// Set layer for the component's gameobject.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="self"></param>
        /// <param name="layer"></param>
        /// <returns></returns>
        public static T Layer<T>(this T self, string layer) where T : Component
        {
            self.gameObject.layer = LayerMask.NameToLayer(layer);
            return self;
        }
        /// <summary>
        /// Set tag for the component's gameobject.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="self"></param>
        /// <param name="tag"></param>
        /// <returns></returns>
        public static T Tag<T>(this T self, string tag) where T : Component
        {
            self.gameObject.tag = tag;
            return self;
        }    
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CoderZ1010

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值