- Editado
使用FGUI按钮事件时,会有一帧的骨骼设置状态。使用其他代码方式没有该问题
以下是示例代码:其中Update中的方法可以做到正确切换,start中的ui.GetChild("Sword").onClick.Set切换动画文件时会有默认的设置姿态
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Spine;
using Spine.Unity;
using FairyGUI;
public class Demo : MonoBehaviour
{
public SkeletonDataAsset Sword;
public SkeletonDataAsset Wand;
public SkeletonAnimation curAnimation;
public GameObject body;
public GComponent ui;
// Start is called before the first frame update
void Start()
{
curAnimation = GetComponent<SkeletonAnimation>();
ui = GameObject.Find("UIPanel").GetComponent<UIPanel>().ui;
ui.GetChild("Sword").onClick.Set(() =>
{
ReloadSkeletonAsset(1);
});
ui.GetChild("Wand").onClick.Set(() =>
{
ReloadSkeletonAsset(2);
});
}
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(KeyCode.P))
{
ReloadSkeletonAsset(2);
}
if (Input.GetKeyDown(KeyCode.O))
{
ReloadSkeletonAsset(1);
}
}
public void ReloadSkeletonAsset(int id)
{
SkeletonDataAsset skeleton;
string animation;
if(id == 1)
{
skeleton = Sword;
animation = "n-b0029-防守-姿势循环";
}
else
{
skeleton = Wand;
animation = "h-b0029-防守-姿势循环";
}
if (skeleton != null)
{
body.GetComponent<SkeletonAnimation>().ClearState();
body.GetComponent<SkeletonAnimation>().skeletonDataAsset = skeleton;
body.GetComponent<SkeletonAnimation>().Initialize(true);
body.GetComponent<SkeletonAnimation>().Skeleton.SetSkin("No1");
PlayAnimation(animation);
}
}
public void PlayAnimation(string animationName)
{
curAnimation.AnimationState.SetAnimation(0, animationName, true);
}
}
我可以提供一个14MB的示例项目