您当前的位置: 首页 >  unity

CoderZ1010

暂无认证

  • 2浏览

    0关注

    168博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

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

CoderZ1010 发布时间:2020-10-12 10:18:11 ,浏览量:2

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:

/// 
    /// The extension of gameobject.
    /// 
    public static class GameObjectExtension
    {
        /// 
        /// Activate the gameobject.
        /// 
        /// 
        /// 
        public static GameObject Activate(this GameObject self)
        {
            self.SetActive(true);
            return self;
        }
        /// 
        /// Deactivate the gameobject.
        /// 
        /// 
        /// 
        public static GameObject Deactivate(this GameObject self)
        {
            self.SetActive(false);
            return self;
        }
        /// 
        /// Set name for the gameobject.
        /// 
        /// 
        /// 
        /// 
        public static GameObject Name(this GameObject self, string name)
        {
            self.name = name;
            return self;
        }
        /// 
        /// Set layer for the gameobject.
        /// 
        /// 
        /// 
        /// 
        public static GameObject Layer(this GameObject self, int layer)
        {
            self.layer = layer;
            return self;
        }
        /// 
        /// Set layer for the gameobject.
        /// 
        /// 
        /// 
        /// 
        public static GameObject Layer(this GameObject self, string layer)
        {
            self.layer = LayerMask.NameToLayer(layer);
            return self;
        }
        /// 
        /// Set tag for the gameobject.
        /// 
        /// 
        /// 
        /// 
        public static GameObject Tag(this GameObject self, string tag)
        {
            self.tag = tag;
            return self;
        }
        /// 
        /// Get specified component, if it's null, add and return.
        /// 
        /// 
        /// 
        /// 
        public static T GetComponentForcibly(this GameObject self) where T : Component
        {
            var component = self.GetComponent();
            return component ?? self.AddComponent();
        }
        /// 
        /// Get specified component, if it's null, add and return.
        /// 
        /// 
        /// 
        /// 
        public static Component GetComponentForcibly(this GameObject self, Type type)
        {
            var component = self.GetComponent(type);
            return component ?? self.AddComponent(type);
        }
        /// 
        /// Get the local active state of the compontent's gameObject.
        /// 
        /// 
        /// 
        /// 
        public static bool IsActive(this T self) where T : Component
        {
            return self.gameObject.activeSelf;
        }
        /// 
        /// Activate the component's gameobject.
        /// 
        /// 
        /// 
        /// 
        public static T Activate(this T self) where T : Component
        {
            self.gameObject.SetActive(true);
            return self;
        }
        /// 
        /// Deactivate the component's gameobject.
        /// 
        /// 
        /// 
        /// 
        public static T Deactivate(this T self) where T : Component
        {
            self.gameObject.SetActive(false);
            return self;
        }
        /// 
        /// Set name for the component's gameobject.
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T Name(this T self, string name) where T : Component
        {
            self.gameObject.name = name;
            return self;
        }
        /// 
        /// Set layer for the component's gameobject.
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T Layer(this T self, int layer) where T : Component
        {
            self.gameObject.layer = layer;
            return self;
        }
        /// 
        /// Set layer for the component's gameobject.
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T Layer(this T self, string layer) where T : Component
        {
            self.gameObject.layer = LayerMask.NameToLayer(layer);
            return self;
        }
        /// 
        /// Set tag for the component's gameobject.
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T Tag(this T self, string tag) where T : Component
        {
            self.gameObject.tag = tag;
            return self;
        }    
    }
关注
打赏
1653184800
查看更多评论
立即登录/注册

微信扫码登录

0.2780s