How do you change a listeners parameters(For example the 0 in these two listeners) via script in custom events?
When you Invoke the method it will have whatever value you want in it.
Here is an example.
Create a GameObject and stick this code on it:
using UnityEngine;
using System.Collections;
public class ExampleTest : MonoBehaviour
{
public void OnMouseClick(int count)
{
Debug.Log("You've pressed the mouse " + count + " times");
}
}
Then Create another GameObject and attach this script:
using System;using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class EventController : MonoBehaviour {
[System.Serializable]
public class MouseClickHandler : UnityEvent<int> { }
[SerializeField]
public MouseClickHandler mcHandler;
private int count;
void Awake()
{
count = 0;
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
count++;
mcHandler.Invoke(count);
}
}
}
Then in the inspector of this gameObject, add a new Listener. Drag the first GameObject onto it and choose OnMouseClick. Now whenever you click the mouse The event will fire from the 2nd GO and be caught be the first. It will print out how many times you’ve clicked the mouse.