找回密码
 立即注册
查看: 246|回复: 0

XLUA中使用GetComponentsInChildren问题

[复制链接]
发表于 2023-4-8 19:38 | 显示全部楼层 |阅读模式
要实现一个功能,就是获取一个组件下所有的UI控件。
上代码
  1. --利用面向对象
  2. Object:subClass("BasePanel")
  3. BasePanel.panelObj = nil
  4. --相当于模拟一个字典键为控件名 值为控件本身
  5. BasePanel.controls = {}
  6. function BasePanel:Init(name)
  7.     if self.panelObj == nil then
  8.         --公共的实例化对象的方法
  9.         self.panelObj = ABManager:LoadRes("ui","name",typeof(GameObject))
  10.         self.panelObj.transform:SetParent(Canvas,false)
  11.         --GetComponentsInChildren
  12.         --找 所有UI控件(父类 UIBehaviour)存起来
  13.         local allControls =  self.panelObj:GetComponentsInChildren(typeof(UIBehaviour))
  14.         --如果存了一些没有用的UI控件
  15.         --为了避免找各种无用控件 我们定一个规则 拼面板时 控件命名一定按规范来
  16.         --Button btn名字
  17.         --Toggle tog名字
  18.         --Image image名字
  19.         --ScrollRect sv名字
  20.         for i = 0, allControls.Length-1 do
  21.             local controlName = allControls[i].name
  22.             --按照名字的规则 去找控件 必须满足命名规范 才存起来
  23.             if string.find(controlName,"btn") ~=nil or
  24.                string.find(controlName,"Tog") ~=nil or
  25.                string.find(controlName,"image") ~=nil or
  26.                string.find(controlName,"sv") ~=nil or
  27.                string.find(controlName,"txt") ~=nil then
  28.                --避免出现一个对象上 挂载多个UI控件 出现覆盖的问题
  29.                --都会被存到一个容器中 相当于像列表数组的形式
  30.                if self.controls[controlName] ~= nil then
  31.                     table.insert(self.controls[controlName],allControls[i])
  32.                else
  33.                   --如果controls内不存在
  34.                   self.controls[controlName] = {allControls[i]}
  35.                end
  36.             end   
  37.         end
  38.         
  39.     end   
  40. end
复制代码
令我纠结了一段时间的是,在判断是否为需要存储的控件后,将控件存入controls的这段代码:
  1. --利用面向对象
  2. Object:subClass("BasePanel")
  3. BasePanel.panelObj = nil
  4. --相当于模拟一个字典键为控件名 值为控件本身
  5. BasePanel.controls = {}
  6. function BasePanel:Init(name)
  7.     if self.panelObj == nil then
  8.         --公共的实例化对象的方法
  9.         self.panelObj = ABManager:LoadRes("ui","name",typeof(GameObject))
  10.         self.panelObj.transform:SetParent(Canvas,false)
  11.         --GetComponentsInChildren
  12.         --找 所有UI控件(父类 UIBehaviour)存起来
  13.         local allControls =  self.panelObj:GetComponentsInChildren(typeof(UIBehaviour))
  14.         --如果存了一些没有用的UI控件
  15.         --为了避免找各种无用控件 我们定一个规则 拼面板时 控件命名一定按规范来
  16.         --Button btn名字
  17.         --Toggle tog名字
  18.         --Image image名字
  19.         --ScrollRect sv名字
  20.         for i = 0, allControls.Length-1 do
  21.             local controlName = allControls[i].name
  22.             --按照名字的规则 去找控件 必须满足命名规范 才存起来
  23.             if string.find(controlName,"btn") ~=nil or
  24.                string.find(controlName,"Tog") ~=nil or
  25.                string.find(controlName,"image") ~=nil or
  26.                string.find(controlName,"sv") ~=nil or
  27.                string.find(controlName,"txt") ~=nil then
  28.                self.controls[allControls[i].name = allControls[i]
  29.                end
  30.             end   
  31.         end
  32.         
  33.     end   
  34. end
复制代码
这么做会导致控件的插入不完全。(当时一脸疑惑,为啥不完全呢?)
意思大概是:我们用
local allControls =  self.panelObj:GetComponentsInChildren(typeof(UIBehaviour))
allControls里包含所有的控件。
以Button为例子


Button中我们有两个控件,Image和Button 因为之前用GetComponentsInChildren都只是导出子类的transform等。一般一个子物体只返回一个相应类的对象存储在数组中。
self.controls[allControls.name = allControls
而这句代码 就表达了,例如此时的allControls.name为Button, 那么controls表中就相当于有一个键值对:Button = allControls。我当时觉得 ,没问题啊,GetComponentsInChildren(x,bool)返回的是x类型的数组。子类返回一个x类型的对象,放入数组中。
问题就出现在一个上。
UIBehaviour是大部分UI控件的父类。也就是说如果执行
local allControls =  self.panelObj:GetComponentsInChildren(typeof(UIBehaviour))
那么一个子物体返回的不止一个对象,有多少个UI控件就返回多少个。
上代码:
把脚本挂载到一个Button中
  1. void Start()
  2.     {
  3.         LuaManager.GetInstance().Init();
  4.         LuaManager.GetInstance().DoluaFile("Main");
  5.         
  6.         UIBehaviour[] ub = transform.GetComponentsInChildren<UIBehaviour>(false);
  7.         for(int i = 0;i<ub.Length;i++)
  8.         {
  9.             UIBehaviour ub2 = ub[i];
  10.             UnityEngine.Debug.Log(ub2.name);
  11.         }
  12.     }
复制代码


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


因此
使用这种方法,将allControls变成一个表的形式,如果存在名字相同的则用insert进行插入
没有则创建关系。
  1.                if self.controls[controlName] ~= nil then
  2.                     table.insert(self.controls[controlName],allControls[i])
  3.                else
  4.                   --如果controls内不存在
  5.                   self.controls[controlName] = {allControls[i]}
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-5-17 21:30 , Processed in 0.088161 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表