-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathToggleExample.cs
More file actions
33 lines (25 loc) · 948 Bytes
/
Copy pathToggleExample.cs
File metadata and controls
33 lines (25 loc) · 948 Bytes
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
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using System.Collections;
public class ToggleExample : MonoBehaviour
{
// Set in editor
[SerializeField]
private Toggle toggle = null;
[SerializeField]
private Text toggleStatusText = null;
UnityAction<bool> action = null; // The Toggle.OnValueChanged event sends one bool argument that the action needs to accept
// Use this for initialization
void Start()
{
toggleStatusText.text = toggle.isOn.ToString(); // grab the inital toggle value;
// create an action to attach to the toggle
action = (value) => OnToggleChange(value); // value represents the argument that will be passed from Toggle.OnValueChanged
toggle.onValueChanged.AddListener(action);
}
private void OnToggleChange(bool value)
{
toggleStatusText.text = value.ToString();
}
}