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