AnimationLink.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. namespace Pathfinding {
  4. [HelpURL("http://arongranberg.com/astar/documentation/stable/class_pathfinding_1_1_animation_link.php")]
  5. public class AnimationLink : NodeLink2 {
  6. public string clip;
  7. public float animSpeed = 1;
  8. public bool reverseAnim = true;
  9. public GameObject referenceMesh;
  10. public LinkClip[] sequence;
  11. public string boneRoot = "bn_COG_Root";
  12. [System.Serializable]
  13. public class LinkClip {
  14. public AnimationClip clip;
  15. public Vector3 velocity;
  16. public int loopCount = 1;
  17. public string name {
  18. get {
  19. return clip != null ? clip.name : "";
  20. }
  21. }
  22. }
  23. static Transform SearchRec (Transform tr, string name) {
  24. int childCount = tr.childCount;
  25. for (int i = 0; i < childCount; i++) {
  26. Transform ch = tr.GetChild(i);
  27. if (ch.name == name) return ch;
  28. else {
  29. Transform rec = SearchRec(ch, name);
  30. if (rec != null) return rec;
  31. }
  32. }
  33. return null;
  34. }
  35. public void CalculateOffsets (List<Vector3> trace, out Vector3 endPosition) {
  36. //Vector3 opos = transform.position;
  37. endPosition = transform.position;
  38. if (referenceMesh == null) return;
  39. GameObject ob = GameObject.Instantiate(referenceMesh, transform.position, transform.rotation) as GameObject;
  40. ob.hideFlags = HideFlags.HideAndDontSave;
  41. Transform root = SearchRec(ob.transform, boneRoot);
  42. if (root == null) throw new System.Exception("Could not find root transform");
  43. Animation anim = ob.GetComponent<Animation>();
  44. if (anim == null) anim = ob.AddComponent<Animation>();
  45. for (int i = 0; i < sequence.Length; i++) {
  46. anim.AddClip(sequence[i].clip, sequence[i].clip.name);
  47. }
  48. Vector3 prevOffset = Vector3.zero;
  49. Vector3 position = transform.position;
  50. Vector3 firstOffset = Vector3.zero;
  51. for (int i = 0; i < sequence.Length; i++) {
  52. LinkClip c = sequence[i];
  53. if (c == null) {
  54. endPosition = position;
  55. return;
  56. }
  57. anim[c.clip.name].enabled = true;
  58. anim[c.clip.name].weight = 1;
  59. for (int repeat = 0; repeat < c.loopCount; repeat++) {
  60. anim[c.clip.name].normalizedTime = 0;
  61. anim.Sample();
  62. Vector3 soffset = root.position - transform.position;
  63. if (i > 0) {
  64. position += prevOffset - soffset;
  65. } else {
  66. firstOffset = soffset;
  67. }
  68. for (int t = 0; t <= 20; t++) {
  69. float tf = t/20.0f;
  70. anim[c.clip.name].normalizedTime = tf;
  71. anim.Sample();
  72. Vector3 tmp = position + (root.position-transform.position) + c.velocity*tf*c.clip.length;
  73. trace.Add(tmp);
  74. }
  75. position = position + c.velocity*1*c.clip.length;
  76. anim[c.clip.name].normalizedTime = 1;
  77. anim.Sample();
  78. Vector3 eoffset = root.position - transform.position;
  79. prevOffset = eoffset;
  80. }
  81. anim[c.clip.name].enabled = false;
  82. anim[c.clip.name].weight = 0;
  83. }
  84. position += prevOffset - firstOffset;
  85. GameObject.DestroyImmediate(ob);
  86. endPosition = position;
  87. }
  88. public override void OnDrawGizmosSelected () {
  89. base.OnDrawGizmosSelected();
  90. List<Vector3> buffer = Pathfinding.Util.ListPool<Vector3>.Claim();
  91. Vector3 endPosition = Vector3.zero;
  92. CalculateOffsets(buffer, out endPosition);
  93. Gizmos.color = Color.blue;
  94. for (int i = 0; i < buffer.Count-1; i++) {
  95. Gizmos.DrawLine(buffer[i], buffer[i+1]);
  96. }
  97. }
  98. }
  99. }