mistergreen

So I'm profiling my 2D platformer game and it seems SkeletonRenderer.LateUpdate() & SkeletonRenderer.Update() takes up the most resources at any given frame. It looks like they run whether the Skeletons are in viewport or not. I assumed occlusion culling would tell the skeletons not to run the updates that's not in the viewport. I'm expecting 60fps but I'm getting 30fps on a mobile device.

So what methods do you guys use to cut down on SkeletonRenderer.LateUpdate() & SkeletonRenderer.Update()?
mistergreen
Posts: 15

Pharan

You can check if the GameObject is far away enough from the camera, and enable/disable your Spine GameObjects or SkeletonAnimation components accordingly.
Official Esoteric Assorted Furniture Cleaner and Teahouse | Check out the Spine Users Tumblr Blog: spine-users.tumblr.com
pharan.deviantart.com | pharantriestoanimatestuff.tumblr.com - - - Windows 10 - Spine-Unity.
User avatar
Pharan

Pharan
Posts: 4360

mistergreen

I'm testing now with OnBecameVisible() & OnBecameInvisible() to do GetComponent<SkeletonAnimation>().enabled
There is a small boost in performance. I'll have to try gameObject.SetActive() to see if there is an even bigger boost.

Your idea of distance from the camera might be a better option in terms of behaviors.
mistergreen
Posts: 15

Mitch

Another option is if you're really crammed for performance, you can pretty trivially force skeletonrenderer(s) to update every other frame, and off-phase half of them. I do this for ~300 skeletons visible on devices as old as a Nexus 4 phone. It's kind of like animation LOD.
User avatar
Mitch

Mitch
Posts: 965

Pharan

The OnBecameVisible/OnBecameInvisible thing is another option. The best solution depends on how your game works. Basing your skeleton/character activity on visibility works well for some games but not others, and you need to be the judge.

Performance tips on the Skeleton structure include not having too many keys, too many bones, or too many vertices on meshes on the skeletons that you use the most. For animations, try the Clean Up button to remove unnecessary keys as extra keys can need too much calculation if you really have a lot of them * a lot of skeletons. If you have a lot of that skeleton, try to limit your use of IK and Path constraints.

You can also disable the SkeletonAnimation component and call its Update and LateUpdate yourself manually, skipping frames as needed.
Here's a sample component that does that:
using UnityEngine;
using Spine.Unity;

public class ManualUpdateSkeletonAnimation : MonoBehaviour {

public SkeletonAnimation skeletonAnimation;

[Range(1/60f, 1f/8f)] // slider from 60fps to 8fps
public float timeInterval = 1f / 24f; // 24fps

float deltaTime;

#if UNITY_EDITOR
void OnValidate () {
if (skeletonAnimation == null)
skeletonAnimation = GetComponent<SkeletonAnimation>();
}
#endif

void Start () {
if (skeletonAnimation == null)
skeletonAnimation = GetComponent<SkeletonAnimation>();

skeletonAnimation.Initialize(false);
skeletonAnimation.clearStateOnDisable = false;
skeletonAnimation.enabled = false;
ManualUpdate();
}

void Update () {
deltaTime += Time.deltaTime;
if (deltaTime >= timeInterval)
ManualUpdate();
}

void ManualUpdate () {
skeletonAnimation.Update(deltaTime);
skeletonAnimation.LateUpdate();
deltaTime -= timeInterval; //deltaTime = deltaTime % timeInterval; // optional time accuracy.
}

}
Official Esoteric Assorted Furniture Cleaner and Teahouse | Check out the Spine Users Tumblr Blog: spine-users.tumblr.com
pharan.deviantart.com | pharantriestoanimatestuff.tumblr.com - - - Windows 10 - Spine-Unity.
User avatar
Pharan

Pharan
Posts: 4360

Nate

Some info here about adjusting projects for performance:
Metrics - Spine User Guide: Performance
Usually the most important parts of the Metrics view are vertex count and vertex transforms.
User avatar
Nate

Nate
Posts: 7430

mistergreen

Thanks for the tips. SkeletonRenderer.LateUpdate() & SkeletonRenderer.Update() are now 3&4 place in the profile and FPS is up.
mistergreen
Posts: 15


Return to Unity