• Unity
  • How to to change to another SkeletonDataAsset in game?

For example, my character has a ability to transform to a dragon from human.
I have created a SkeletonAnimation instance in game, then I want to change its SkeletonDataAsset to another SkeletonDataAsset .

(I know I can use two gameobjects to achieve this, but I just want to know can that be possible.)

This is my code, and in fact it works.
I want to know is this a correct way to do this?

public SkeletonDataAsset data1;
public SkeletonDataAsset data2;
void ChangeToDataAsset1()
{
    SkeletonAnimation sa = GetComponent<SkeletonAnimation>();

sa.skeletonDataAsset = data1;
sa.Initialize(true);
}
void ChangeToDataAsset2()
{
    SkeletonAnimation sa = GetComponent<SkeletonAnimation>();

sa.skeletonDataAsset = data2;
sa.Initialize(true);
}
Related Discussions
...

Spine.AnimationState state =
SpineUtil.ReloadSkeletonDataAsset(h.gameObject, h.GetKaihuaSkeletonDataAsset(), "kaihua");
state.Event += h.HandleAnimationStateEvent;

like this~

gllame wrote

I want to know is this a correct way to do this?

sa.skeletonDataAsset = data1;
sa.Initialize(true);

Yes, this is the correct way. As @c6u2 mentioned, you need to re-subscribe any event listener callback methods to the newly created skeletonAnimation.AnimationState.

c6u2 wrote

Spine.AnimationState state =
SpineUtil.ReloadSkeletonDataAsset(h.gameObject, h.GetKaihuaSkeletonDataAsset(), "kaihua");
state.Event += h.HandleAnimationStateEvent;

@c6u2 Thanks for adding your remark of having to resubscribe to AnimationState events.
It might only be pseudo-code, but I'm not sure which method you are referring to with SpineUtil.ReloadSkeletonDataAsset. While there is a method SpineEditorUtilities.ReloadSkeletonDataAsset available, it's part of the Editor and therefore not available at runtime in your game.

public static Spine.AnimationState ReloadSkeletonDataAsset (GameObject g,SkeletonDataAsset res,string skinname) {

    SkeletonAnimation skeletonAnimation = g.GetComponent<SkeletonAnimation>();
    if (skeletonAnimation == null)
    {
        Debug.LogError("当前对象没有绑定SkeletonAnimation组件");
        return null;
    }

//加载SkeletonDataAsset资源
if (res == null)
return null;
//不清除一次之前的数据会有超出边界的报错啥的
skeletonAnimation.ClearState();
//将加载的资源赋值给SkeletonAnimation对象
skeletonAnimation.skeletonDataAsset = res;
//传递true强制初始化一次资源,这样才能真的刷新SkeletonAnimation和MeshReder中的资源和材质,并加载到当前对象中

    skeletonAnimation.Initialize(true);

    Spine.AnimationState state =null;
    
    if(skeletonAnimation!=null&&skinname!=null)
        state= SpineUtil.AllChange(skeletonAnimation,"kaihua");
    
    return state;
}

Sorry,this is the method.

无需道歉,感谢您的澄清!
No need to apologize, thanks for the clarification!