[LayaAir 2.0]3D场景销毁后,仍有引用存在,内存中场景及引用其的对象无法销毁
this.scene = Laya.stage.addChild(new Laya.Scene3D()) as Laya.Scene3D;
this.camera = new Laya.Camera();
this.camera.transform.translate(new Laya.Vector3(0, 2, 5));
this.camera.transform.rotate(new Laya.Vector3(-30, 0, 0), true, false);
this.scene.addChild(this.camera);
Laya.loader.create("res/scene_Effect/Conventional/Effect_zhujue_attack.lh", Laya.Handler.create(this, () => {
let item = Laya.loader.getRes("res/scene_Effect/Conventional/Effect_zhujue_attack.lh");
let tarItem = Laya.Sprite3D.instantiate(item);
this.scene.addChild(tarItem);
tarItem.transform.position = new Laya.Vector3(0, 0, 0);
item.destroy();
item = null;
tarItem.destroy();
tarItem = null;
this.camera.removeSelf();
this.camera.destroy(true);
this.camera = null;
this.scene.destroy();
Laya.Scene.gc();
}));
以上为测试代码,若在相机添加到场景之前load资源,可正常销毁3d场景及其内存占用
this.camera = new Laya.Camera();
this.camera.transform.translate(new Laya.Vector3(0, 2, 5));
this.camera.transform.rotate(new Laya.Vector3(-30, 0, 0), true, false);
this.scene.addChild(this.camera);
Laya.loader.create("res/scene_Effect/Conventional/Effect_zhujue_attack.lh", Laya.Handler.create(this, () => {
let item = Laya.loader.getRes("res/scene_Effect/Conventional/Effect_zhujue_attack.lh");
let tarItem = Laya.Sprite3D.instantiate(item);
this.scene.addChild(tarItem);
tarItem.transform.position = new Laya.Vector3(0, 0, 0);
item.destroy();
item = null;
tarItem.destroy();
tarItem = null;
this.camera.removeSelf();
this.camera.destroy(true);
this.camera = null;
this.scene.destroy();
Laya.Scene.gc();
}));
以上为测试代码,若在相机添加到场景之前load资源,可正常销毁3d场景及其内存占用
没有找到相关结果
已邀请:
要回复问题请先登录
5 个回复
Xiiiyue
赞同来自:
Laya.Render._context.destroy(false);
Laya.RenderContext3D._instance.scene = null;
Laya.RenderContext3D._instance.camera = null;
//clearshaders
let shaderIncetance = Laya.RenderContext3D._instance.shader;
shaderIncetance.destroy();
Laya.RenderContext3D._instance.shader = null;
for (let shaderKey in Laya.Shader3D._preCompileShader)
{
Laya.Shader3D._preCompileShader[shaderKey]._subShaders.forEach(subShader =>
{
subShader._passes.forEach(pass =>
{
pass._cacheSharders.forEach(cache =>
{
cache && cache.forEach(allInstance =>
{
allInstance && allInstance.forEach(ins =>
{
ins && ins.destroy();
})
allInstance = [];
})
cache = [];
})
pass._cacheSharders = [];
})
})
};
添加以上代码后,可以强行清除引用并且清空内存,但是这种做法感觉不太科学,求大佬提供更多思路
Aar0n
赞同来自:
1575604368用户
赞同来自:
代码是
/*
* 场景资源
*/
this.m_info_bg = null;
this.m_gun_root = null;
this.m_complete = null;
/*
* 战场数据
*/
this._NormalBattleInfo = null;
this._InfiniteBattleInfo = null;
this._zombie_info_obj = null;
this.m_fight_2d_root.destroy();//销毁场景
this.m_fight_2d_root = null;
this.m_cur_scence.destroy();//销毁场景
this.m_cur_scence = null;
Laya.Resource.destroyUnusedResources();//销毁无用资源(没有被场景树引用,并且没有加资源锁的)
if(Laya.Browser.onMiniGame)
Laya.Browser.window.wx.triggerGC();
小程序KID
赞同来自:
缨
赞同来自: