XLUA中使用GetComponentsInChildren问题

要实现一个功能,就是获取一个组件下所有的UI控件。

上代码

--利用面向对象
Object:subClass("BasePanel")

BasePanel.panelObj = nil
--相当于模拟一个字典键为控件名 值为控件本身
BasePanel.controls = {}
function BasePanel:Init(name)
    if self.panelObj == nil then
        --公共的实例化对象的方法
        self.panelObj = ABManager:LoadRes("ui","name",typeof(GameObject))
        self.panelObj.transform:SetParent(Canvas,false)
        --GetComponentsInChildren
        --找 所有UI控件(父类 UIBehaviour)存起来
        local allControls =  self.panelObj:GetComponentsInChildren(typeof(UIBehaviour))
        --如果存了一些没有用的UI控件
        --为了避免找各种无用控件 我们定一个规则 拼面板时 控件命名一定按规范来
        --Button btn名字
        --Toggle tog名字
        --Image image名字
        --ScrollRect sv名字
        for i = 0, allControls.Length-1 do
            local controlName = allControls[i].name
            --按照名字的规则 去找控件 必须满足命名规范 才存起来
            if string.find(controlName,"btn") ~=nil or 
               string.find(controlName,"Tog") ~=nil or 
               string.find(controlName,"image") ~=nil or
               string.find(controlName,"sv") ~=nil or
               string.find(controlName,"txt") ~=nil then
               --避免出现一个对象上 挂载多个UI控件 出现覆盖的问题 
               --都会被存到一个容器中 相当于像列表数组的形式
               if self.controls[controlName] ~= nil then
                    table.insert(self.controls[controlName],allControls[i])
               else 
                  --如果controls内不存在
                  self.controls[controlName] = {allControls[i]}

               end
            end    
        end
        
    end    
end

令我纠结了一段时间的是,在判断是否为需要存储的控件后,将控件存入controls的这段代码:

--利用面向对象
Object:subClass("BasePanel")

BasePanel.panelObj = nil
--相当于模拟一个字典键为控件名 值为控件本身
BasePanel.controls = {}
function BasePanel:Init(name)
    if self.panelObj == nil then
        --公共的实例化对象的方法
        self.panelObj = ABManager:LoadRes("ui","name",typeof(GameObject))
        self.panelObj.transform:SetParent(Canvas,false)
        --GetComponentsInChildren
        --找 所有UI控件(父类 UIBehaviour)存起来
        local allControls =  self.panelObj:GetComponentsInChildren(typeof(UIBehaviour))
        --如果存了一些没有用的UI控件
        --为了避免找各种无用控件 我们定一个规则 拼面板时 控件命名一定按规范来
        --Button btn名字
        --Toggle tog名字
        --Image image名字
        --ScrollRect sv名字
        for i = 0, allControls.Length-1 do
            local controlName = allControls[i].name
            --按照名字的规则 去找控件 必须满足命名规范 才存起来
            if string.find(controlName,"btn") ~=nil or 
               string.find(controlName,"Tog") ~=nil or 
               string.find(controlName,"image") ~=nil or
               string.find(controlName,"sv") ~=nil or
               string.find(controlName,"txt") ~=nil then
               self.controls[allControls[i].name = allControls[i]
               end
            end    
        end
        
    end    
end

这么做会导致控件的插入不完全。(当时一脸疑惑,为啥不完全呢?)

意思大概是:我们用

local allControls =  self.panelObj:GetComponentsInChildren(typeof(UIBehaviour))

allControls里包含所有的控件。

以Button为例子

 Button中我们有两个控件,Image和Button 因为之前用GetComponentsInChildren都只是导出子类的transform等。一般一个子物体只返回一个相应类的对象存储在数组中。

self.controls[allControls[i].name = allControls[i]

 而这句代码 就表达了,例如此时的allControls[i].name为Button, 那么controls表中就相当于有一个键值对:Button = allControls[i]。我当时觉得 ,没问题啊,GetComponentsInChildren(x,bool)返回的是x类型的数组。子类返回一个x类型的对象,放入数组中。

问题就出现在一个上。 

UIBehaviour是大部分UI控件的父类。也就是说如果执行

local allControls =  self.panelObj:GetComponentsInChildren(typeof(UIBehaviour))

那么一个子物体返回的不止一个对象,有多少个UI控件就返回多少个。

上代码:

 把脚本挂载到一个Button中

 void Start()
    {
        LuaManager.GetInstance().Init();
        LuaManager.GetInstance().DoluaFile("Main");
        
        UIBehaviour[] ub = transform.GetComponentsInChildren<UIBehaviour>(false);
        for(int i = 0;i<ub.Length;i++)
        {
            UIBehaviour ub2 = ub[i];
            UnityEngine.Debug.Log(ub2.name);
        }
    }

 发现Button打印了两次!

那么现在就可以理解直接对lua中表进行覆盖,会少控件的原因了。

self.controls[allControls[i].name = allControls[1]

第一次 allControls[1] = Button.Image(看懂就好==)

此时表control内键值对 Button = Image。

self.controls[allControls[i].name = allControls[2]

第二次 allControls[1] = Button.Button

此时表control内键值对 Button = Button。Image就被覆盖了!

因此 

 使用这种方法,将allControls[i]变成一个表的形式,如果存在名字相同的则用insert进行插入

没有则创建关系。

               if self.controls[controlName] ~= nil then
                    table.insert(self.controls[controlName],allControls[i])
               else 
                  --如果controls内不存在
                  self.controls[controlName] = {allControls[i]}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值