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