-
Notifications
You must be signed in to change notification settings - Fork 1
/
DynamicListener.cs
47 lines (39 loc) · 1.21 KB
/
DynamicListener.cs
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
47
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class DynamicListener : MonoBehaviour {
public string objectListeningTag = "Enter GameObject's tag";
public bool thisIsListener = false;
public bool parameter = false;
public string sendMessage = "Enter GameObject's method name";
public string messageParameter;
Button b;
GameObject objectListening;
void Start()
{
b = GetComponent<Button>();
GetObjectListening();
}
void GetObjectListening()
{
if (thisIsListener)
objectListening = gameObject;
else //if we are listening to another object
objectListening = GameObject.FindGameObjectWithTag(objectListeningTag);
if (objectListening)
SetListener();
}
void SetListener()
{
if (b)
{
if (!parameter)
b.onClick.AddListener(() => objectListening.SendMessage(sendMessage));
else
b.onClick.AddListener(() => objectListening.SendMessage(sendMessage, messageParameter));
}
else
Debug.LogError("Dynamic Listeners belong on buttons.");
}
}