• 日本語
  • UnityでのSkeletonDataAssetの付替え時のラグについて

Unityのゲーム内で素体を動的に変更したいと思っております。
(例キャラの出撃画面など)
そのため下記のようにスクリプトを作成しました。(かなり簡略化してあります。)

using UnityEngine;
using Spine.Unity;

public class SkeletonChange : MonoBehaviour
{
    public SkeletonDataAsset SkeletonDataAsset1;
    public SkeletonDataAsset SkeletonDataAsset2;
    public bool Change;
    void Update()
    {
        if(Input.GetButtonDown("Fire1")){
            if (Change)
            {
                this.GetComponent<SkeletonAnimation>().skeletonDataAsset = SkeletonDataAsset1;
            }
            else
            {
                this.GetComponent<SkeletonAnimation>().skeletonDataAsset = SkeletonDataAsset2;
            }
            this.GetComponent<SkeletonAnimation>().Skeleton.Skin = null;
            this.GetComponent<SkeletonAnimation>().ClearState();
            this.GetComponent<SkeletonAnimation>().Initialize(true);
            Change = !Change;
        }
    }
}


上記を実行すると、SkeletonDataAssetの変更タイミングでラグが発生するようです。
変更するスケルトンが12Mのファイルサイズだとラグなく動くのですが、使おうと思っているSpineデータが20Mあり、0.5秒ほどゲームが一時停止してしまい、見栄えが悪くなってしまいます。
上記現象は初回の変更時のみに起きるようで、2回目以降の読み込みなら発生しないようなのです。
また、20Mのファイルが20種類くらいあるので裏で読み込みなどできますと助かります。
(例、「裏でSkeletonDataAssetを事前に非同期で読み込みさせる」等)
こちらのラグの対策方法などありますでしょうか。

  • Đã chỉnh sửa

すべてのSkeletonAnimationコンポーネントを事前ロードし、それらを再初期化する代わりに、必要に応じてそれらを切り替えることができます。

20MBのスケルトンデータというのはかなり大きいです! アセットを共有できますか? 自由変形キーをたくさん使っているのでしょうか?

You could preload all SkeletonAnimation components and just switch between them as needed instead of reinitializing them.

20MB of skeleton data is a lot! Can you share your assets? I assume you are using a lot of free-form deformation keys?

ありがとうございます。
「preload 」でフォーラムを検索し、
SkeletonDataAsset.GetSkeletonData(true);
こちらを事前に読み込んでおくことで解決するというところまでたどり着きました。
上記方針で対応しようと思います、ありがとうございます!

また、20MBのスケルトンデータですが、問い合わせフォームから送らせていただきました。
こちらもご確認いただけますと幸いです。

Thank you very much.
I searched the forum for "preload" and found
SkeletonDataAsset.GetSkeletonData(true);
I've reached the point where I can solve the problem by preloading this one.
I'll try the above policy, thanks!

Also, I have sent you the 20MB skeleton data via the contact form.
Thank you for your cooperation.
Translated with www.DeepL.com/Translator (free version)

再現ファイルの送付ありがとうございます。提供パッケージのエクスポートされたスケルトンデータのjsonファイルは5MBと大きいようです。少なくとも20MBよりはましですが。

一つ重要なことがあります。ゲーム内で.skel.bytesで終わるバイナリスケルトン・ファイルを使っていますか?それとも私たちに提供いただいた.jsonファイルを使っていますか? もし、.jsonファイルを使用しているのであれば、バイナリ出力(.skel.bytesファイル)の使用に切り替えてください、そうすれば、読み込みがより速くなります。

あなたのプロジェクトを見たところ、残念ながらアニメーションの中に変形キーがあまり見当たりませんでした。その代わり、たくさんのアニメーションがありますが、それらも不必要なキーをあまり含んでいないように見えます。むしろ、アニメーションの数が非常に多いため、ファイルサイズが大きくなっているように見えます。エクスポート時に(セットアップポーズと同じキーを削除する)アニメーションクリーンアップを有効にしても、ファイルサイズに対する効果はあまり無く、 .skel.bytes ファイルが 2.39MB から 1.9MB に減っただけでした。


Thanks for sending the reproduction files. It seems that the exported skeleton data json file in the provided package is 5MB large, which is at least better than 20MB.

One important note: are you using a binary skeleton file ending with .skel.bytes in your game, or are you using the .json file that you have provided? If you are using .json files, please be sure to switch to using binary export (.skel.bytes files) instead, because this will make loading much faster.

After having a look at your project, I unfortunately don't see many deform keys in your animations. Instead I see lots of animations, which also don't seem to contain many unnecessary keys. It looks more as if the very high number of animations simply sums up to the large file size. Enabling Animation cleanup upon export (removing keys identical to the setup pose) also has rather low benefit on the file size, reducing the .skel.bytes file from 2.39MB to 1.9MB.