当前位置:   article > 正文

Spine动画基础用法:停止,播放,倍速,暂停,继续,添加播放顺序,按顺序播放,左右旋转_spine 播放

spine 播放

疫情,TNND,害的我到现在才敢找工作,在家活生生耽误半年.
新工作接触了Spine动画,然后美术来找我调效果,为了偷懒就写了个Demo给他,目前用着还不错,哈哈哈
为了简单化,所有的变量就都在界面上显示了.不过权重什么的没做调整,因为我们没有边跑边打的需求.
我是在Unity3D引擎下开发的,语言用的C#,其实大家灵活运用,具体方法就那几个.

代码简介:
Q:停止
(播放Qname动画,不循环)
W:播放
(循环播放Wanme的动画)
E:慢速
(根据Etime改变速度)
R:快速
(根据Rtime改变速度)
T:暂停
(暂停播放当前动画,不改变播放速度和顺序)
Y:继续(继续播放动画,速度调至正常速度)
U:添加(填好播放顺序后,启动测试,先按此键,再按I键)
P:平滑过渡(Lname:前动画,Rname:后动画,timess:过渡时间)
I:按照添加好的顺序播放
A:向左转
D:向右转
以上所有效果启动程序测试后,按键生效,不按不管用

管理脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Spine;
using Spine.Unity;
using System;
using System.Runtime.CompilerServices;

public class SpineManage : MonoBehaviour
{
    static SkeletonAnimation skeletonAnimation;//骨骼动画
    static AnimationStateData animStateData;//动画状态数据
    Spine.AnimationState saState;//动画状态

    static Dictionary<int, string> playDic = new Dictionary<int, string>();

    // Start is called before the first frame update
    void Start()
    {
        skeletonAnimation = GetComponent<SkeletonAnimation>();
        saState = skeletonAnimation.AnimationState;
        animStateData = skeletonAnimation.SkeletonDataAsset.GetAnimationStateData();


        //ExposedList<Spine.Animation> animations = skeletonAnimation.skeleton.Data.Animations;
        //Debug.Log(animations.Count);
        //for (int i = 0, n = animations.Count; i < n; i++)
        //{
        //    Spine.Animation animation = animations.Items[i];
        //    Debug.Log(animation.Duration);
        //}

    }

    // Update is called once per frame
    void Update()
    {

    }

    /// <summary>
    /// 动画添加
    /// </summary>
    /// <param name="weight">权重值:数字越大优先级越高</param>
    /// <param name="spineName">动画名称</param>
    /// <param name="flag">是否循环</param>
    /// <param name="delay">延迟系数</param>
    public static void SpineAdd(int weight, string spineName, bool flag, float delay)
    {
        skeletonAnimation.AnimationState.AddAnimation(weight, spineName, flag, delay);
        var trackEntry = skeletonAnimation.state.GetCurrent(0);
        Debug.Log(trackEntry);

    }

    /// <summary>
    /// 动画播放
    /// </summary>
    /// <param name="weight">权重值:数字越大优先级越高</param>
    /// <param name="spineName">动画名称</param>
    /// <param name="flag">是否循环</param>
    public static void SpinePlay(int weight, string spineName, bool flag)
    {
        skeletonAnimation.AnimationState.SetAnimation(weight, spineName, flag);
    }


    /// <summary>
    /// 动画按添加顺序播放(是否循环)
    /// </summary>
    public static void SpinePlayDic(bool flag)
    {
        if (flag)
        {
            //我就不信有啥动画能连续放三十遍
            for (int i = 0; i < 30; i++)
            {
                showDic();
            }
        }
        else
        {
            showDic();
        }

    }

    /// <summary>
    /// 遍历播放字典
    /// </summary>
    private static void showDic()
    {
        foreach (string str in playDic.Values)
        {
            skeletonAnimation.AnimationState.AddAnimation(0, str, false, 0f);
        }
    }

    /// <summary>
    /// 设置前一个动画向后一个动画之间平滑过渡的时间,切换的时候混合多少
    /// </summary>
    /// <param name="beforeName">前一个动画</param>
    /// <param name="afterName">后一个动画</param>
    /// <param name="time">平滑过渡的时间</param>
    public static void SpineMix(string beforeName, string afterName, float time)
    {
        animStateData.SetMix(beforeName, afterName, time);
    }

    /// <summary>
    /// 倍速播放,等于0 动画暂停,注意速度更改后为永久性的
    /// </summary>
    /// <param name="speed">播放速度</param>
    public static void SpineSpeed(float speed)
    {
        skeletonAnimation.timeScale = speed;
    }

    /// <summary>
    /// 从任意某一帧开始播放
    /// </summary>
    /// <param name="weight">权重值</param>
    /// <param name="spineName">动画名称</param>
    /// <param name="flag">是否循环</param>
    /// <param name="frames">要开始的帧数</param>
    public static void SpineAnyPointPlay(int weight, string spineName, bool flag, float frames)
    {
    //原来用这个
      //  skeletonAnimation.state.SetAnimation(weight, spineName, flag).Time = frames / 30f;
      //2019.4.0f1用这个
       //  skeletonAnim.state.SetAnimation(weight, spineName, flag).TrackTime = frames / 30f;
    }

    /// <summary>
    ///  添加播放队列的播放动画.
    ///  建议按照播放顺序添加,注意播放顺序int,如有重复,后添加的会覆盖原来的
    /// </summary>
    /// <param name="location">顺序</param>
    /// <param name="spineName">动画名字</param>
    public static void SpineAddDic(int location, string spineName)
    {
        if (!playDic.ContainsKey(location))
        {
            playDic.Add(location, spineName);
        }
        else
        {
            playDic.Remove(location);
            playDic.Add(location, spineName);
        }

    }

    /// <summary>
    /// 动画左右翻转
    /// </summary>
    [Obsolete]
    public static void SpineRotat(bool flag, GameObject model)
    {
        if (flag)
        {
            //   skeletonAnimation.skeleton.FlipX = true;
            model.transform.localScale = new Vector3(1, 1, 1);

        }
        else
        {
            //   skeletonAnimation.skeleton.FlipX = false;
            model.transform.localScale = new Vector3(-1, 1, 1);
        }


    }


}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176

用法

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameStart : MonoBehaviour
{
    public GameObject Model;
    Text text;
    float timers = 0f;

    //停止参数
    public string Qname;

    //播放参数
    public string Wname;

    //慢速播放
    public float Espeed;

    //快速播放
    public float Rspeed;

    //顺序播放
    public List<string> Slist = new List<string>();

    //动画融合
    public string Lname;
    public string Rname;
    public float timess;


    // Start is called before the first frame update
    void Start()
    {
        Model.AddComponent<SpineManage>();

        text = GameObject.Find("timer").GetComponent<Text>();
    }

    // Update is called once per frame
    [Obsolete]
    void Update()
    {

        //停止
        if (Input.GetKeyDown(KeyCode.Q))
        {
            SpineManage.SpinePlay(0, Qname, false);
        }
        //播放
        if (Input.GetKeyDown(KeyCode.W))
        {
            SpineManage.SpinePlay(0, Wname, true);
        }
        //慢速
        if (Input.GetKeyDown(KeyCode.E))
        {
            SpineManage.SpineSpeed(Espeed);
        }
        //快速
        if (Input.GetKeyDown(KeyCode.R))
        {
            SpineManage.SpineSpeed(Rspeed);
        }
        //暂停
        if (Input.GetKeyDown(KeyCode.T))
        {
            SpineManage.SpineSpeed(0f);
        }
        //继续
        if (Input.GetKeyDown(KeyCode.Y))
        {
            SpineManage.SpineSpeed(1f);
        }
        //添加
        if (Input.GetKeyDown(KeyCode.U))
        {
            for (int i = 0; i < Slist.Count; i++)
            {
                SpineManage.SpineAddDic(i, Slist[i]);
            }
        }
        //按照添加好的顺序播放
        if (Input.GetKeyDown(KeyCode.I))
        {
            SpineManage.SpinePlayDic(true);
        }
        //动画融合
        if (Input.GetKeyDown(KeyCode.P))
        {
            SpineManage.SpineMix(Lname, Rname, timess);
        }

        延时
        //if (Input.GetKeyDown(KeyCode.U))
        //{
        //    SpineManage.SpineAdd(0, "qianjin2", false, 5f);
        //}

        //向左转
        if (Input.GetKeyDown(KeyCode.A))
        {
            SpineManage.SpineRotat(true, Model);
        }

        //向右转
        if (Input.GetKeyDown(KeyCode.D))
        {
            SpineManage.SpineRotat(false, Model);
        }



        timers += Time.deltaTime;
        text.text = timers.ToString();
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/399926
推荐阅读
相关标签
  

闽ICP备14008679号