1. 程式人生 > >unity開發小貼士之三 UGUI-Lua Component回收

unity開發小貼士之三 UGUI-Lua Component回收

gin oca etc unit tran map The panel pair

ugui tolua

local test = {}

test.b = gameobject
test.c = gameobject:GetComponent(typeof(UnityEngine.UI.Button))

首先調用UnityEngine.GameObject.Destroy(test.b)
如果test這個table也被ToLuaUnRef回收之後,發現
test.c這個引用c#中的Button對象並未從ObjectTranslator.objectsBackMap中釋放出來

ObjectTranslator中的對象沒有釋放,應該是你Lua中還引用這這個對象。正常的做法是,保證Lua中的引用及時釋放,但是這樣會有很多xxx=nil的代碼,如果項目開始不註意這些,後期改起來會很煩。我們的做法是在BasePanel中遍歷

function BasePanel:cleanVar()
    for k,v in pairs(self) do
        if (type(v) == "userdata" or type(v) == "table") and k ~= "gameObject" then
            self[k] = nil
        end
    end
end

關閉UI界面是在OnDestroy()中調用cleanVar()

unity開發小貼士之三 UGUI-Lua Component回收