• Runtimes
  • Optimizing loading of .json files

Related Discussions
...

So, in Unity, we have a huge issue. Right now, the only realistic way to work with Spine is to store all of our animations in one .json file that gets loaded into Unity.

We are witnessing long load times as the .json file gets loaded up.

Are there any ways that we could optimize this loading by only loading the animation we need for a particular action? Any other ways people have optimized the loading of .json files so that its more efficient?

9 giorni dopo

Are you loading the json multiple times?

Json isn't really for optimization. You really want to use the binary format, but it is not yet available for spine-unity.

Yes, then this is a burden for us. What happens is


even though the file is pre-loaded


it still wants to load in one frame when we try to draw it onscreen. Consequently there is a pause as we wait for the file to load. Unfortunate. If we could load multiple .json files


such as one .json per animation, then we could mitigate this a bit.

If you're okay with handling it like a typical game (ie, loading things before a level/scene instead of in the middle of gameplay):
Try calling GetSkeletonData on all the SkeletonDataAsset objects you need for a level at scene load time.
There are probably other things you can do like force Unity to load relevant textures onto the GPU at scene load time too.

Is this not an option for your setup? Are your skeletons and animations insanely complex?

un mese dopo

What I can't understand: Why can't I pull out one single animation from the spine file to playback? If you've pre-loaded the spine file into memory, why isn't playing one animation much faster to handle? It seems like every time you want to play an animation, the scene has to load the entire file to screen to play back.

As Pharan said, you want something simple along the lines of

using UnityEngine;
using Spine;

public class SpineRefs : MonoBehaviour 
{
    public SkeletonDataAsset[] skeletonDataAssets;

void Awake()
{
  if(skeletonDataAssets == null)
     return;

    for (int i = 0; i < skeletonDataAssets.Length; i++)
    {
     if(skeletonDataAssets[i] != null)
        skeletonDataAssets[i].GetSkeletonData(false);
    }
}
}

If you throw all the skeletonDataAssests that you use in a particular scene in there then you will pre-load the data into memory from the json. The json associated with each skeletonDataAsset should not be reloaded, unless you somehow null the skeletonData you create with the call to GetSkeletonData.

I have not personally encountered much issue with lag associated with playing a single animation. You could take a look at the underlying code of 'set' and 'add' animation but I think they are fairly lean already.

Sorry if you have already tried this, if so I apologies for the redundant info.

Rob

  • Modificato

Yep. That's pretty much it.
You could have a manager call a method instead of putting this in Awake though, but I guess this is fine for testing.

[EDIT]See Mitch's post below. Don't use foreach for optimizing. foreach has a thing[/EDIT]

Regarding OP's attempts:
Yes, you technically can deserialize just some of the animations in skeletondata instead of all of them but the question is whether that's worth your time mucking with the runtime. This requires you to study and understand how Spine deserialization works, and how Unity/Mono's objects persist and determine where the bottleneck actually is. (ie, Do you know if the animation loading is slowing it down, or the skeleton data loading, or some other thing?) If you wanna do that, go ahead. If you get stuck on the specifics of the process, you can probably ask here. The first code to study would be SkeletonJson.cs if you're not holding your breath for binary.

I've done some really complicated setups with lots of extra animations before and messing with the deserializer has never been necessary.

Your phrase "the scene has to load the entire file to screen". What does that mean?

That post is some juicy stuff.
That sucks, about Unity's compiler bug. (among other bugs)