SlowMoButton.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. namespace UnityStandardAssets.SceneUtils
  5. {
  6. public class SlowMoButton : MonoBehaviour
  7. {
  8. public Sprite FullSpeedTex; // the ui texture for full speed
  9. public Sprite SlowSpeedTex; // the ui texture for slow motion mode
  10. public float fullSpeed = 1;
  11. public float slowSpeed = 0.3f;
  12. public Button button; // reference to the ui texture that will be changed
  13. private bool m_SlowMo;
  14. void Start()
  15. {
  16. m_SlowMo = false;
  17. }
  18. void OnDestroy()
  19. {
  20. Time.timeScale = 1;
  21. }
  22. public void ChangeSpeed()
  23. {
  24. // toggle slow motion state
  25. m_SlowMo = !m_SlowMo;
  26. // update button texture
  27. var image = button.targetGraphic as Image;
  28. if (image != null)
  29. {
  30. image.sprite = m_SlowMo ? SlowSpeedTex : FullSpeedTex;
  31. }
  32. button.targetGraphic = image;
  33. Time.timeScale = m_SlowMo ? slowSpeed : fullSpeed;
  34. }
  35. }
  36. }