Spine with Adventure Creator[]
Sources: http://esotericsoftware.com/spine-unity, https://www.youtube.com/watch?time_continue=137&v=8YssVJZ18V8
1. Install spine-unity[]
To use the spine-unity runtime in your Unity project:
- Download the latest spine-unity unitypackage. Alternatively you can get the latest changes via Git as described below.
- Import the unitypackage (you can double-click on it and Unity will open it).
- Enabling Compatibility with 2D Toolkit:
spine-unity supports 2D Toolkit and can render Spine skeletons using TK2D's texture atlas system. To enable 2D Toolkit compatibility, open Unity's Preferences via Edit -> Preferences... and in section Spine select Define TK2D - Enable.
2. Export the animations[]
The following will export all animations as one object. We handle it later in Unity. Exporting in binary format instead of JSON will result in smaller file size and faster loading.
- Choose Binary on the upper-left of the Export window instead of JSON.
- Set the Extension to .skel.bytes.
3. Importing the animations to Unity[]
Simply drag the generated files into the assets folder. For example Characters/<CharacterName> Then drag the _SkeletonData asset into the Scene view or the Hierarchy panel and choose SkeletonAnimation. A new GameObject will be instantiated, with the required Spine components already set up.
4. Creating a character[]
Take the GameObject from the last step and run the Adventure Creator Character Wizard.
NOTE: Make sure to select Sprites Unity as the Animation Engine.
Once the Character is set up, rename it to the Character name and drag it into the assets folder to create a prefab of your new player. For example Characters/<CharacterName>
5. Setting up the animations[]
Next up is creating a PlayerController and and mapping all the animations to the direction and state of the player.
- Open the player prefab and change the Player component to animate in 8 directions.
- Add an animator component to the prefab child skeleton. The GameObject from the previous step.
- Create a PlayerController for the animator compontent.
Open the PlayerController in the Animator window and create states for all the expected anmations.
5.1 Custom scripting[]
To get everything to work you need to add a custom script. The script will allow us to override the basic animation with the skeleton animations that we made in Spine. We also add a variable speed to be able to adjust the speed of the animation. Create a news script the assets folder. I chose to call it SpineAnimationScript.cs. Open visual studio and paste the following:
using UnityEngine; using System.Collections; using Spine.Unity; public class SpineAnimationScript : StateMachineBehaviour { public string animationName; public float speed; public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { SkeletonAnimation anim = animator.GetComponent<SkeletonAnimation>(); anim.state.SetAnimation(0, animationName, true).TimeScale = speed; } }
Attach this script to all of the states. Edit the variable animationName and speed to play the correct animation on different states.
NOTE: The animation name directly maps to what animation names are available in the skeleton component.
That's all!