local cuee = CS.UnityEngine;
local ui = CS.UnityEngine.UI;
-- 在 C# 的 Start 中調用
--用來初始化
function OnStart(gObj)
--獲取一個 UnityEngine.UI 里的 Text
local texts = GetComponentsInChildren(gObj,ui.Text);
for i = 0, texts.Length - 1 do
print(texts[i].text);
end
end
--獲取子組件
--gObj 父組件
--ty 要獲取的子組件類型
--tag 組件的tag
--retys 返回要獲取的所有組件
function GetComponentsInChildren(gObj,ty,tag)
--獲取所有 ty 類型的組件
local tys = gObj:GetComponentsInChildren(typeof(ty));
if tag == nil then
--返回組件
return tys;
else
--要返回的所有的 ty 類型的組件
local retys = {};
--循環查找 tag 相同的組件
for i,v in pairs(tys) do
--如果 tag 相等進入
if v.gameObject.tag == tag then
--將要獲取的組件存到 retys 里
table.insert(retys,v);
end
end
--返回組件
return retys;
end
end
待更新