super1 发表于 2023-4-8 19:38

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.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 ~= nil then
                  table.insert(self.controls,allControls)
               else
                  --如果controls内不存在
                  self.controls = {allControls}

               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.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.name = allControls
               end
            end   
      end
      
    end   
end这么做会导致控件的插入不完全。(当时一脸疑惑,为啥不完全呢?)
意思大概是:我们用
local allControls =self.panelObj:GetComponentsInChildren(typeof(UIBehaviour))
allControls里包含所有的控件。
以Button为例子


Button中我们有两个控件,Image和Button 因为之前用GetComponentsInChildren都只是导出子类的transform等。一般一个子物体只返回一个相应类的对象存储在数组中。
self.controls.name = allControls
而这句代码 就表达了,例如此时的allControls.name为Button, 那么controls表中就相当于有一个键值对:Button = allControls。我当时觉得 ,没问题啊,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;
            UnityEngine.Debug.Log(ub2.name);
      }
    }

发现Button打印了两次!
那么现在就可以理解直接对lua中表进行覆盖,会少控件的原因了。
self.controls.name = allControls
第一次 allControls = Button.Image(看懂就好==)
此时表control内键值对 Button = Image。
self.controls.name = allControls
第二次 allControls = Button.Button
此时表control内键值对 Button = Button。Image就被覆盖了!


因此
使用这种方法,将allControls变成一个表的形式,如果存在名字相同的则用insert进行插入
没有则创建关系。
               if self.controls ~= nil then
                  table.insert(self.controls,allControls)
               else
                  --如果controls内不存在
                  self.controls = {allControls}
页: [1]
查看完整版本: XLUA中使用GetComponentsInChildren问题