WobbleAnim.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using UnityEngine;
  2. using System.Collections;
  3. /// <summary>
  4. /// This component is responsible for wobbling a UI element. It will wobble smoothly using
  5. /// perlin noise around its original position. It will also stretch and squash.
  6. /// </summary>
  7. public class WobbleAnim : MonoBehaviour
  8. {
  9. Vector3 _originalPosition;
  10. float _noiseX, _noiseY;
  11. const float MAX_DISPLACEMENT = 20;
  12. const float NOISE_SPEED = 0.7f;
  13. const float Y_OFFSET = 100;
  14. public UIElementTransition elementTransition;
  15. bool isWobbling;
  16. void Awake()
  17. {
  18. if (elementTransition == null)
  19. {
  20. elementTransition = GetComponent<UIElementTransition>();
  21. }
  22. AVDebug.Assert(elementTransition != null, "WobbleAnim is assumed to be on a UI element which is transitioned. UIElementTransition component not found on "+name);
  23. elementTransition.OnHiding += OnHiding;
  24. elementTransition.OnShowComplete += OnShowComplete;
  25. enabled = false;
  26. _originalPosition = transform.localPosition;
  27. _noiseY = Random.value * 300; //just some random value
  28. }
  29. void OnDestroy()
  30. {
  31. if (elementTransition != null)
  32. {
  33. elementTransition.OnHiding -= OnHiding;
  34. elementTransition.OnShowComplete -= OnShowComplete;
  35. }
  36. }
  37. void Start()
  38. {
  39. iTween.ScaleTo(gameObject, iTween.Hash(
  40. "islocal", true,
  41. "time", Random.Range(0.5f, 0.8f), //make the time random so they don't look wobbling the same
  42. "y", 0.95f * transform.localScale.y,
  43. "x", 1.05f * transform.localScale.x,
  44. "easetype", iTween.EaseType.easeInOutSine,
  45. "looptype", iTween.LoopType.pingPong));
  46. }
  47. void Update()
  48. {
  49. if (isWobbling)
  50. {
  51. transform.localPosition = GetNextWobblyPosition();
  52. }
  53. }
  54. void OnHiding()
  55. {
  56. enabled = false;
  57. isWobbling = false;
  58. }
  59. void OnShowComplete()
  60. {
  61. enabled = true;
  62. //tween to the first wobbly position to avoid a glitch between original position and the first wobbly position
  63. iTween.ValueTo(gameObject, iTween.Hash(
  64. "from", 0,
  65. "to", 1.0f,
  66. "time", 0.8f,
  67. "easetype", iTween.EaseType.linear,
  68. "onupdate", "OniTweenTransitToWobbling",
  69. "onupdatetarget", gameObject));
  70. }
  71. Vector3 GetNextWobblyPosition()
  72. {
  73. _noiseX += NOISE_SPEED * Time.deltaTime;
  74. float x = Mathf.PerlinNoise(_noiseX, _noiseY) - 0.5f; //note that perlin noise is between 0 and 1... shiftin so it's between -0.5 to 0.5
  75. float y = Mathf.PerlinNoise(_noiseX, _noiseY + Y_OFFSET) - 0.5f;
  76. return _originalPosition + new Vector3(x * MAX_DISPLACEMENT, y * MAX_DISPLACEMENT);
  77. }
  78. void OniTweenTransitToWobbling(float f)
  79. {
  80. if (f == 1)
  81. {
  82. isWobbling = true;
  83. } else
  84. {
  85. transform.localPosition = transform.localPosition * (1-f) + GetNextWobblyPosition() * f;
  86. }
  87. }
  88. }