We understand this is a big problem for you (and many others using Defold). We have been discussing alternative solutions a bit. We’ll investigate it a bit more and get back to you in a few days to see what we have found.
I just did a test that might point to a way forward for you:
- I created an atlas with some images:
I set up a sprite to use one of these images as texture.
I dug out the texturec from the “build” folder
- I copied the texturec to a folder in the project, named it “test.texturec” and added the folder to the custom resources.
- Now I changed the atlas to contain different images, but the image sizes and order is intact:
- Running the game now shows the pink square:
- But with this little piece of code I can dynamically load and replace the texture resource on the sprite:
local buffer = resource.load("/res/test.texturec")
resource.set(go.get("#sprite", "texture0"), buffer)
Wow! But how to unload this texture from the memory?
It’s unloaded when you stop refering to it. Resources are reference counted.
The complexity of this solution is that the resources of buildings are not of the same type. One building consists of several simple small GO. The second building contains animations and large atlases. We will have to unify the structures of all buildings for easy replacement of textures (and create local custom texture management) - it’s very hard for today.
Perhaps this method is suitable to split one RGBA-texture into two RGB + A.
Hey! Our approach to solve this problem is to improve collection factories to handle dynamic resource loading better. With these changes you should be able to use collection factories to spawn your buildings with ability to control when the resources are loaded/unloaded. While I was experimenting with this, I found that the current implementation is buggy, as it is already loading resources dynamically. It however loads them synchronously so I would expect it to cause a few hitches while loading textures etc. Right now you can however exploit this bug to get rid of the memory issue, i.e. spawn your buildings through collection factories.
Current faulty behaviour:
Collection factory loads resources synchronously when collectionfactory.create is called
Collection factory either (controlled by user):
loads resources when its containing collection (i.e. the game or the level) is being loaded
loads resources asynchronously when collectionfactory.create is called, or a new function collectionfactory.load is called
We will also add the same thing to the factory component, which is used when spawning single game objects.
How to be with atlas removing?
I tried to use this bug, and yes - atlas load synchronously when I call collectionfactory.create. But I can’t remove this atlas. Is it possible?
Dynamic loading for gameobject and collection factories added in release 1.2.115