Spine with Unity Mecanim


前言

最近這兩天剛剛接觸Spine,研究了一下Unity Mecanim Animator如何控制Spine,在此分享記錄一下,如有不當之處,請留言指出,歡迎討論。

Unity & Spine

想要在Unity中使用Spine動畫,目前有三種方式。
第一種,利用SkeletonAnimation,這是Spine提供的原生方式,支持所有Spine功能。
第二種,利用Mecanim (or SkeletonAnimator),使用此方式Spine-unity會將SkeletonData中的動畫轉換為一系列AnimationClips ,並生成一個Unity Animator Controller。
SkeletonAnimator 組件關聯Unity Animator 和 Spine Animation ,並利用Spine Animation  API Mix 和 Aplly 方法控制動畫。這允許我們使用Unity的原有流程控制動畫。
但是可能有些副作用關於attachment keyframes,可能很難追蹤問題。attachment keyframes目前我還沒有研究到。
第三種,利用Baking,這個不推薦,這個是專門為了那些沒有spine runtime的准備的。使用此方式無法使用Spine特有的功能。

Spine with Unity Mecanim

第零步,准備資源,下載官方的Spine Uinty Package, 里面有一些示例場景。今天我們要改造一下Controling Animation Continued這個場景。
原場景使用第一種方式控制動畫。我們把它改造成使用第二種方式控制動畫。
第一步,新建腳本MyRaptor ,並將腳本添加到raptor Skeleton。
腳本內容如下:

using UnityEngine;
using System.Collections;
using Spine.Unity;

namespace Ron.Tang {
	public class MyRaptor : MonoBehaviour {

		#region Inspector
		[SpineAnimation]
		public string walk = "walk";

		[SpineAnimation]
		public string gungrab = "gungrab";

		[SpineAnimation]
		public string gunkeep = "gunkeep";

		[SpineEvent]
		public string footstepEvent = "footstep";

		public AudioSource footstepAudioSource;
		#endregion

		SkeletonAnimation skeletonAnimation;
        Animator myAnimator;
        
		void Start () {
            //skeletonAnimation = GetComponent<SkeletonAnimation>();
            //skeletonAnimation.AnimationState.Event += HandleEvent;
            myAnimator = GetComponent<Animator>();
            StartCoroutine(GunGrabRoutine());
		}

		
        void footstep()
        {
            footstepAudioSource.pitch = 0.5f + Random.Range(-0.2f, 0.2f);
            footstepAudioSource.Play();
        }

        IEnumerator GunGrabRoutine () {		
			
			while (true) {
				yield return new WaitForSeconds(Random.Range(0.5f, 3f));
                //skeletonAnimation.AnimationState.SetAnimation(1, gungrab, false);
                myAnimator.SetTrigger("gungrab");

				yield return new WaitForSeconds(Random.Range(0.5f, 3f));
                myAnimator.SetTrigger("gunkeep");
                //skeletonAnimation.AnimationState.SetAnimation(1, gunkeep, false);
            }

		}

	}
}

 通過簡單的對比,可以看出把原有使用skeletonAnimation的地方,替換為使用animator。並添加了footstep函數用以響應動畫事件。

第二步,從raptor_SkeletonData生成raptor_Controller。點擊raptor_SkeletonData在Inspector里面有生成按鈕。

第三步,給場景中raptor_skeleton的gameobject添加兩個組件Animator 和 Skeleton Animator。並設置Animator 里的Controller為raptor_Controller

以及Skeleton Animator里的SkeletonData Asset 為raptor_SkeletonData。具體如下圖所示。

第四步,設置animator controller 里面的 layer ,transition, parameter。如下圖所示:

第五步,運行測試,效果和原先保持一致。

PS:上圖人物形象及動畫乃是Spine官方資源,版權歸Spine所有,僅供學習交流使用。

 

轉載請標明出處,謝謝


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM