forked from AyARL/UnityGUIExamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButtonExample.cs
More file actions
46 lines (36 loc) · 1.37 KB
/
Copy pathButtonExample.cs
File metadata and controls
46 lines (36 loc) · 1.37 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 UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using System.Collections;
public class ButtonExample : MonoBehaviour
{
// Assigned in editor
[SerializeField]
private Button actionButton = null;
[SerializeField]
private Button disableListenerButton = null;
[SerializeField]
private Text clickCounterText = null;
UnityAction action = null;
private int clickCounter = 0;
private void Start()
{
/* Add a listener to the button's onClick event
The code between { } will be executed when the button is clicked
Any values for the variables will also be captured at the time of the event (and not at the time of creating the listener) */
action = () => { clickCounter += 1; SetClickCount(clickCounter); }; // save the event to a variable so it can be removed later on
actionButton.onClick.AddListener(action);
disableListenerButton.onClick.AddListener(() => DisableListener()); // create and add listener at once - can only be removed by onClick.RemoveAllListeners();
}
private void SetClickCount(int clicks)
{
clickCounterText.text = clicks.ToString();
}
private void DisableListener()
{
if (action != null)
{
actionButton.onClick.RemoveListener(action);
}
}
}