At the moment, it seems impossible to preload texture atlases for Spine objects created after a loading screen. Please correct me if I'm wrong. At first, we were adding our atlas PNGs to our preloader, but we discovered Spine doesn't use the underlying TextureCache. As a workaround, we no longer preload the spine atlases (this actually reduces total load times) and we are lucky enough in our project that Spine loading the images itself is hidden between the transition from our load scene to our game scene.
[c++, request] Use Cocos2d-x TextureCache
- Modifié
I haven't noticed that issue in my own projects. From what I can see in the code:
void Cocos2dTextureLoader::load(AtlasPage& page, const spine::String& path) {
Texture2D* texture = Director::getInstance()->getTextureCache()->addImage(path.buffer());
CCASSERT(texture != nullptr, "Invalid image");
if (texture)
{
texture->retain();
Texture2D::TexParams textureParams = { filter(page.minFilter), filter(page.magFilter), wrap(page.uWrap), wrap(page.vWrap) };
texture->setTexParameters(textureParams);
page.setRendererObject(texture);
page.width = texture->getPixelsWide();
page.height = texture->getPixelsHigh();
}
}
It's clearly using the TextureCache, since the addImage() function checks if the texture already exists, and if it does, it returns a pointer to it, otherwise it loads it and adds it to the cache.
If you preload the texture, then this should work, but you have to watch out for the value of the path
parameter, which is the key used to locate the texture in the std::unordered_map
. If it's not precisely the same, then the image will be loaded again and a new texture created for that path key.
One other thing, if after preloading, but before loading your spine models, anything calls Director::getInstance()->getTextureCache()->removeUnusedTextures();
, and retain()
was never called on the texture, then that texture will be removed if it's not being referenced anywhere else.
Thank you! This got us back on track.