- 編集済み
Async Skeletondata loading
Hello,
I'm trying to avoid the big freeze every time a skeleton is loaded for the first time. Having an async method to load a specific skeleton would be nice.
I guess a simple way to achieve this would be to change GetSkeletonData() to an Async method or a coroutine, then add some yield or wait in ReadSkeletonData(), between each X bones or slots loaded for example.
Is there any way to already do this, or is this planned? Modifying a plugin is a bit annoying because everytime we update the plugin, a code merge is needed.
Thank in advance,
Hello,
In general it is considered good practice to use pooling wherever possible and avoid instantiation from-scratch in the middle of action, especially if for the first time (which lead to texture loads, etc).
So use pools of objects and pre-warm the pool, then enable (and reset) objects on spawn and disable them on destruction. Reuse objects where possible, instead of instantiating and destroying prefabs. Enabling objects and re-positioning them should in general be much faster than instantiation.
Other sources of optimization can be found in this thread at the bottom for example:
Optimisation Tips (Nintendo Switch + General)
Currently, whenever you load a skeleton the first time, it freezes the game. Even during a loading phase, it is problematic. In my particular case, I need parts of level loaded dynamically (the only problem is skeleton data).
Pooling has nothing to do with it because a skeletondata is cached until unloaded, and is not reloaded each time you use a new object with the same skeleton. The problematic moment is the first load. (And of course and don't want to load every skeleton of the game during the game launch).
If I have no other solution I will add an async version of ReadSkeletonData, but an official way would be handy.
If you cannot solve your problem by moving loading to a timepoint where it does not hurt you, then you could either:
a) simplify the data to be loaded (by reducing the number of vertices, bones, etc) as described in the thread referenced above. I assume you have exported your skeletons in binary (.skel.bytes) and not json form right?
b) or move the costly calls to another thread as you described above. We have added your request for an async call of ReadSkeletonData() to the list of planned parallelization improvements here: [unity] Performance improvement - parallelization · #1348
Unfortunately I must say that this will take some time until we get to implement the parallelization of ReadSkeletonData(), as we are currently working on many high-priority tasks regarding Unity 2019 and other bugreports. So for now I would suggest to follow your solution to add an async version of ReadSkeletonData.
Yes of course it's binary (I don't really get why someone would use json format, who want to have a 3 sec delay before loading a skeleton?).
Ok I will do my own solution. But even by keeping the process in the main thread, just delaying it (in a coroutine for exemple) should do the job. Using thread in Unity is really annoying (everything Unity Object related have to be in the main thread so...), so i'll try the simple way.
Thank for adding it in the to do list.
Good that you used binary format, just stating the obvious since quite often the easy things are missed.
A separate thread should work well in the case of ReadSkeletonData
since it accesses nothing Unity-specific, it's contained in spine-csharp and not in spine-unity.