learningPixi
learningPixi copied to clipboard
` PIXI.utils.TextCache` instead of `PIXI.loader.resource`?
I thought every texture that Pixi create, will be stored in PIXI.utils.TextCache. But as use PIXI.loader
, Pixi’s development team recommends use PIXI.loader.resources
.
I am confused aboute it. So, I make a demo:
PIXI.loader
.add([
"images/1.png",
"images/2.png"
])
.load(function() {
var sprite = new PIXI.Sprite(PIXI.utils.TextureCache["images/[email protected]"]);
app.stage.addChild(sprite);
});
I found that PIXI.utils.TextCache
works well。
Can I always use PIXI.utils.TextCache
instead of PIXI.loader.resource
.
I'm interested in the answer too. @kittykatattack What is the difference between usage of PIXI.utils.TextureCache
and PIXI.loader.resources
in the load
callback?
Thanks in advance.
I found something (loader.js#L74-L83) in PIXI's source code, the textureParser
calls fromLoader
, which can add resource into cache (Texture.js#L452-L453). So once the resource is loaded, you can find them in PIXI.utils.TextureCache
.
Then I found the loader of PIXI extends from englercj/resource-loader, this line (Loader.js#L389-L401) means that everything you have loaded will be destroyed once you call loader.reset()
.
In a word, reset
can only destroy resources in Loader
, not in TextureCache
. ~~So I think at this moment using PIXI.utils.TextureCache
instead of PIXI.loader.resources
is better, unless you prefer writing a cache system yourself.~~ (See "Update 2".)
Update 1:
Sometimes I have to call reset
so that I can load more resources in bad network situation. And reset
can destroy all resources I have loaded, so I can't use texture in PIXI.loader.resources
anymore, but PIXI.utils.TextureCache
still work.
Update 2:
After some searching, I found I was wrong in some words, see the reply below.
@RexSkz Thanks for the explanation. But why the Pixi’s development team recommends use PIXI.loader.resources
then? :-)
@ova2 I did some searching, maybe (or maybe not) you're talking about this article. The developer shows that PIXI provides two ways to load images: PIXI.loader
and fromImage
, the latter one (which I didn't know before) uses the TextureCache
, and when we have loaded the resource, it won't reload again.
So I come to a conclusion:
- When you're writting a callback function for
loader.setup
, usePIXI.loader.resources
. - When you're sure the image has been loaded, use
PIXI.Texture.fromImage
. - DO NOT use
PIXI.utils.TextureCache
anyway (which shows that my previous reply was wrong, sorry about that).
@ova2 Go ahead and use TextureCache, it's fine! 😄 I've never encountered a situation where it doesn't work.
Thanks for your answers, guys. @kittykatattack I just bought your PIXI book. It's amazing :-) Do you plan a second edition with last changes and more content? I know API is almost the same, but some advanced content like WebGL Shaders would be great to have in the book.
@ova2 Thanks! I I don't have plans for a second edition yet - but maybe when Pixi v5 stabilizes. (There are some API changes between v3 and v4, but fortunately v4 is backwards compatible so all the v3 code will work - and you'll get messages in the console telling you what the API changes have been.)