LibGdx-必须在Sprite中设置Sprite或Texture? [英] LibGdx - Dispose Sprite or Texture in Sprite is mandatory?

查看:111
本文介绍了LibGdx-必须在Sprite中设置Sprite或Texture?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用我的资产中的纹理加载精灵,如下所述.

I am loading my sprite with the texture from my assets like I have mentioned below.

gameBoardSprite = new Sprite( new Texture(Gdx.files.internal("data/gameboard.png")));  

我需要在代码中放置以下内容吗?它是强制性的吗?

Do I need to have the below dispose in my code ? Is it mandatory ?

gameBoardSprite.getTexture().dispose();

推荐答案

是的,必须在任何Disposable对象上调用dispose,然后再丢失对它的引用,否则会发生内存泄漏. VM不会自动处理诸如Textures之类的OpenGL对象使用的本机内存,因此您必须先手动执行此操作,然后再删除引用,并让GC声明该对象本身所声明的非本地"内存.

Yes, dispose must be called on any Disposable object before you lose your reference to it or there will be a memory leak. The VM won't automatically dispose of the native memory used by OpenGL objects like Textures so you must manually do it before you drop your reference and let the GC claim the "non-native" memory claimed by the object itself.

如果要在应用程序的整个生命周期内使用Texture,则可能不需要计划对其进行处理,但是在Android上,您仍然必须使用Gamedispose方法,因为在某些情况下,Android将关闭您的活动,但不会关闭整个应用程序,因此当用户重新打开游戏时,所有先前的纹理都会泄漏.

If you are going to use a Texture for the lifetime of your app, you might not be planning to ever need to dispose it, but on Android you still have to in the Game's dispose method because there are cases where Android will shut down your Activity but not the entire Application so when the user reopens your game, all the previous textures are leaked.

这篇关于LibGdx-必须在Sprite中设置Sprite或Texture?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆