BoneLocalOverride.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using UnityEngine;
  2. using Spine;
  3. using Spine.Unity;
  4. namespace Spine.Unity.Examples {
  5. public class BoneLocalOverride : MonoBehaviour {
  6. [SpineBone]
  7. public string boneName;
  8. [Space]
  9. [Range(0, 1)] public float alpha = 1;
  10. [Space]
  11. public bool overridePosition = true;
  12. public Vector2 localPosition;
  13. [Space]
  14. public bool overrideRotation = true;
  15. [Range(0, 360)] public float rotation = 0;
  16. ISkeletonAnimation spineComponent;
  17. Bone bone;
  18. #if UNITY_EDITOR
  19. void OnValidate () {
  20. if (Application.isPlaying) return;
  21. spineComponent = spineComponent ?? GetComponent<ISkeletonAnimation>();
  22. if (spineComponent == null) return;
  23. if (bone != null) bone.SetToSetupPose();
  24. OverrideLocal(spineComponent);
  25. }
  26. #endif
  27. void Awake () {
  28. spineComponent = GetComponent<ISkeletonAnimation>();
  29. if (spineComponent == null) { this.enabled = false; return; }
  30. spineComponent.UpdateLocal += OverrideLocal;
  31. if (bone == null) { this.enabled = false; return; }
  32. }
  33. void OverrideLocal (ISkeletonAnimation animated) {
  34. if (bone == null || bone.Data.Name != boneName) {
  35. if (string.IsNullOrEmpty(boneName)) return;
  36. bone = spineComponent.Skeleton.FindBone(boneName);
  37. if (bone == null) {
  38. Debug.LogFormat("Cannot find bone: '{0}'", boneName);
  39. return;
  40. }
  41. }
  42. if (overridePosition) {
  43. bone.X = Mathf.Lerp(bone.X, localPosition.x, alpha);
  44. bone.Y = Mathf.Lerp(bone.Y, localPosition.y, alpha);
  45. }
  46. if (overrideRotation)
  47. bone.Rotation = Mathf.Lerp(bone.Rotation, rotation, alpha);
  48. }
  49. }
  50. }