EventArgsを継承して独自の値を返すクラスを作成する。
イベント発行側
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 |
using UnityEngine; using UnityEngine.UI; using System.Collections; using System; public class SampleButtonArgs : EventArgs { public int id { get; set; } } public class SampleButton : MonoBehaviour { public event EventHandler<SampleButtonArgs> eventOnClickButton; [SerializeField] Button _button; void Awake() { _button.onClick.AddListener(OnClickButton); } void OnClickButton() { if (eventOnClickButton != null) { SampleButtonArgs args = new SampleButtonArgs(); args.id = UnityEngine.Random.Range(0, 256); eventOnClickButton(this, args); } } } |
イベント受信側
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using UnityEngine; using System.Collections; public class Sample : MonoBehaviour { [SerializeField] SampleButton _sampleButton; void Awake() { _sampleButton.eventOnClickButton += _sampleButton_eventOnClickButton; } private void _sampleButton_eventOnClickButton(object sender, SampleButtonArgs e) { Debug.Log("click! id=" + e.id); } } |