値を持つEventArgsを作りたく、値も型に依存しないものにしたかったので調べたらこんなやり方があった。
[ EventValue Class ]
using System;
/// <summary>
/// 値を持つEventHandler。
/// </summary>
public class EventValue<T> : EventArgs {
/// <summary>
/// 値。
/// </summary>
public T value;
/// <summary>
/// コンストラクタ。
/// </summary>
public EventValue(T value) {
this.value = value;
}
}
使う側(EventValueにColorを保持)
using UnityEngine;
using System.Collections;
/// <summary>
/// EventValueのテスト。
/// </summary>
public class EventTest : MonoBehaviour {
/// <summary>
/// EventValueを使用したdelegateの定義。
/// </summary>
public delegate void EventColorHandler(object sender, EventValue<Color> evt);
/// <summary>
/// EventColorHandlerのイベント。
/// </summary>
public event EventColorHandler OnChangeColor;
/// <summary>
/// Start.
/// </summary>
void Start () {
this.OnChangeColor += this.OnChangeColorTest;
this.OnChangeColor(this, new EventValue<Color>(Color.red));
}
/// <summary>
/// イベントを受け取る。
/// </summary>
void OnChangeColorTest(object sender, EventValue<Color> evt) {
print(evt.value);
}
}