using System.Collections;
using UnityEngine;
using Spine.Unity;
namespace Game
{
//[ExecuteInEditMode]
public class SpineForAnimator : MonoBehaviour
{
[SerializeField]
public int index = 0;
[SerializeField]
public bool loop = false;
private SkeletonGraphic _ui;
private SkeletonAnimation _scene;
private int _playingIdx = -1;
private void Awake()
{
_ui = GetComponent<SkeletonGraphic>();
_scene = GetComponent<SkeletonAnimation>();
}
private void Update()
{
if (_playingIdx == index)
{
return;
}
if (_ui != null)
{
PlayUISpine();
}else if (_scene != null)
{
PlaySceneSpine();
}
}
private void PlayUISpine()
{
var animations = _ui.Skeleton.Data.Animations;
Debug.Assert(index < animations.Count, "property index can not more than animation count!");
var state = _ui.AnimationState;
var willName = animations.Items[index].Name;
//播放Spine动画
state.ClearTrack(0);
_ui.Skeleton.SetToSetupPose();
state.SetAnimation(0, willName, loop);
_playingIdx = index;
}
private void PlaySceneSpine()
{
var animations = _scene.skeleton.Data.Animations;
Debug.Assert(index < animations.Count, "property index can not more than animation count!");
var state = _scene.AnimationState;
var willName = animations.Items[index].Name;
//播放Spine动画
state.ClearTrack(0);
_scene.Skeleton.SetToSetupPose();
state.SetAnimation(0, willName, loop);
_playingIdx = index;
}
}
}
spine for unity animator 动画 |