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 updatevoid 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 framevoid 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);}}}

用法

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 updatevoid 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();}
}

本文链接:https://my.lmcjl.com/post/2231.html

展开阅读全文

4 评论

留下您的评论.