LoaderAnime.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="Launcher.cs" company="Exit Games GmbH">
  3. // Part of: Photon Unity Networking Demos
  4. // </copyright>
  5. // <summary>
  6. // Used in PUN Basics Tutorial to connect, and join/create room automatically
  7. // </summary>
  8. // <author>developer@exitgames.com</author>
  9. // --------------------------------------------------------------------------------------------------------------------
  10. using UnityEngine;
  11. namespace Photon.Pun.Demo.PunBasics
  12. {
  13. /// <summary>
  14. /// Simple behaviour to animate particles around to create a typical "Ajax Loader". this is actually very important to visual inform the user that something is happening
  15. /// or better say that the application is not frozen, so a animation of some sort helps reassuring the user that the system is idle and well.
  16. ///
  17. /// TODO: hide when connection failed.
  18. ///
  19. /// </summary>
  20. public class LoaderAnime : MonoBehaviour {
  21. #region Public Variables
  22. [Tooltip("Angular Speed in degrees per seconds")]
  23. public float speed = 180f;
  24. [Tooltip("Radius os the loader")]
  25. public float radius = 1f;
  26. public GameObject particles;
  27. #endregion
  28. #region Private Variables
  29. Vector3 _offset;
  30. Transform _transform;
  31. Transform _particleTransform;
  32. bool _isAnimating;
  33. #endregion
  34. #region MonoBehaviour CallBacks
  35. /// <summary>
  36. /// MonoBehaviour method called on GameObject by Unity during early initialization phase.
  37. /// </summary>
  38. void Awake()
  39. {
  40. // cache for efficiency
  41. _particleTransform =particles.GetComponent<Transform>();
  42. _transform = GetComponent<Transform>();
  43. }
  44. /// <summary>
  45. /// MonoBehaviour method called on GameObject by Unity on every frame.
  46. /// </summary>
  47. void Update () {
  48. // only care about rotating particles if we are animating
  49. if (_isAnimating)
  50. {
  51. // we rotate over time. Time.deltaTime is mandatory to have a frame rate independant animation,
  52. _transform.Rotate(0f,0f,speed*Time.deltaTime);
  53. // we move from the center to the desired radius to prevent the visual artifacts of particles jumping from their current spot, it's not very nice visually
  54. // so the particle is centered in the scene so that when it starts rotating, it doesn't jump and slowy we animate it to its final radius giving a smooth transition.
  55. _particleTransform.localPosition = Vector3.MoveTowards(_particleTransform.localPosition, _offset, 0.5f*Time.deltaTime);
  56. }
  57. }
  58. #endregion
  59. #region Public Methods
  60. /// <summary>
  61. /// Starts the loader animation. Becomes visible
  62. /// </summary>
  63. public void StartLoaderAnimation()
  64. {
  65. _isAnimating = true;
  66. _offset = new Vector3(radius,0f,0f);
  67. if (particles != null)
  68. {
  69. particles.SetActive(true);
  70. }
  71. }
  72. /// <summary>
  73. /// Stops the loader animation. Becomes invisible
  74. /// </summary>
  75. public void StopLoaderAnimation()
  76. {
  77. if (this.particles != null)
  78. {
  79. particles.SetActive(false);
  80. }
  81. }
  82. #endregion
  83. }
  84. }