XLua 調用 unity 中獲取組件的方法


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

 

待更新


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM