FootSoldierExample.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*****************************************************************************
  2. * FootSoldierExample created by Mitch Thompson
  3. * Full irrevocable rights and permissions granted to Esoteric Software
  4. *****************************************************************************/
  5. using UnityEngine;
  6. using System.Collections;
  7. using Spine.Unity;
  8. public class FootSoldierExample : MonoBehaviour {
  9. [SpineAnimation("Idle")]
  10. public string idleAnimation;
  11. [SpineAnimation]
  12. public string attackAnimation;
  13. [SpineAnimation]
  14. public string moveAnimation;
  15. [SpineSlot]
  16. public string eyesSlot;
  17. [SpineAttachment(currentSkinOnly: true, slotField: "eyesSlot")]
  18. public string eyesOpenAttachment;
  19. [SpineAttachment(currentSkinOnly: true, slotField: "eyesSlot")]
  20. public string blinkAttachment;
  21. [Range(0, 0.2f)]
  22. public float blinkDuration = 0.05f;
  23. public KeyCode attackKey = KeyCode.Mouse0;
  24. public KeyCode rightKey = KeyCode.D;
  25. public KeyCode leftKey = KeyCode.A;
  26. public float moveSpeed = 3;
  27. private SkeletonAnimation skeletonAnimation;
  28. void Awake() {
  29. skeletonAnimation = GetComponent<SkeletonAnimation>();
  30. skeletonAnimation.OnRebuild += Apply;
  31. }
  32. void Apply(SkeletonRenderer skeletonRenderer) {
  33. StartCoroutine("Blink");
  34. }
  35. void Update() {
  36. if (Input.GetKey(attackKey)) {
  37. skeletonAnimation.AnimationName = attackAnimation;
  38. } else {
  39. if (Input.GetKey(rightKey)) {
  40. skeletonAnimation.AnimationName = moveAnimation;
  41. skeletonAnimation.skeleton.FlipX = false;
  42. transform.Translate(moveSpeed * Time.deltaTime, 0, 0);
  43. } else if(Input.GetKey(leftKey)) {
  44. skeletonAnimation.AnimationName = moveAnimation;
  45. skeletonAnimation.skeleton.FlipX = true;
  46. transform.Translate(-moveSpeed * Time.deltaTime, 0, 0);
  47. } else {
  48. skeletonAnimation.AnimationName = idleAnimation;
  49. }
  50. }
  51. }
  52. IEnumerator Blink() {
  53. while (true) {
  54. yield return new WaitForSeconds(Random.Range(0.25f, 3f));
  55. skeletonAnimation.skeleton.SetAttachment(eyesSlot, blinkAttachment);
  56. yield return new WaitForSeconds(blinkDuration);
  57. skeletonAnimation.skeleton.SetAttachment(eyesSlot, eyesOpenAttachment);
  58. }
  59. }
  60. }