Skip to content

Instantly share code, notes, and snippets.

@iwashihead
Created September 29, 2016 04:08
Show Gist options
  • Save iwashihead/68b982b570142024c75bc5cba36b4d59 to your computer and use it in GitHub Desktop.
Save iwashihead/68b982b570142024c75bc5cba36b4d59 to your computer and use it in GitHub Desktop.
UnityActionのeventラッパークラス ref: http://qiita.com/iwashihead/items/dca4f721c93295ae9fe3
/*** 使用例 ***/
var OnDamage = new UnityActionWrapper<int>();
// ダメージ通知の関数を登録.
OnDamage += (damage) => { Player.SetDamage(damage); };
// 実際にダメージを与える
OnDamage.Invoke(100);
// ~~~ (略) ~~~
// 登録した関数を全破棄.
OnDamage.Clear();
using System.Collections.Generic;
using UnityEngine.Events;
/// <summary>
/// UnityAction拡張イベントクラス ver引数なし
/// </summary>
public class UnityActionWrapper
{
/// <summary>
/// イベント.
/// </summary>
public event UnityAction Event
{
add { lock (_event) _event += value; _delegates.Add(value); }
remove { lock (_event) _event -= value; _delegates.Remove(value); }
}
private event UnityAction _event = () => { };
private List<UnityAction> _delegates = new List<UnityAction>();
/// <summary>
/// Add 拡張演算子.
/// </summary>
public static UnityActionWrapper operator +(UnityActionWrapper self, UnityAction del)
{
lock (self._event)
{
self._event += del;
self._delegates.Add(del);
}
return self;
}
/// <summary>
/// Remove 拡張演算子.
/// </summary>
public static UnityActionWrapper operator -(UnityActionWrapper self, UnityAction del)
{
lock (self._event)
{
self._event -= del;
self._delegates.Remove(del);
}
return self;
}
/// <summary>
/// 登録されているメソッドを実行します.
/// </summary>
public void Invoke()
{
if (_event != null) _event.Invoke();
}
/// <summary>
/// 登録されているメソッドを全てremoveします.
/// </summary>
public void Clear()
{
foreach (var del in _delegates)
{
_event -= del;
}
_delegates.Clear();
}
}
/// <summary>
/// UnityAction拡張イベントクラス ver引数1
/// </summary>
public class UnityActionWrapper<T0>
{
/// <summary>
/// イベント.
/// </summary>
public event UnityAction<T0> Event
{
add { lock (_event) _event += value; _delegates.Add(value); }
remove { lock (_event) _event -= value; _delegates.Remove(value); }
}
private event UnityAction<T0> _event = arg0 => { };
private List<UnityAction<T0>> _delegates = new List<UnityAction<T0>>();
/// <summary>
/// Add 拡張演算子.
/// </summary>
public static UnityActionWrapper<T0> operator +(UnityActionWrapper<T0> self, UnityAction<T0> del)
{
lock (self._event)
{
self._event += del;
self._delegates.Add(del);
}
return self;
}
/// <summary>
/// Remove 拡張演算子.
/// </summary>
public static UnityActionWrapper<T0> operator -(UnityActionWrapper<T0> self, UnityAction<T0> del)
{
lock (self._event)
{
self._event -= del;
self._delegates.Remove(del);
}
return self;
}
/// <summary>
/// 登録されているメソッドを実行します.
/// </summary>
public void Invoke(T0 arg)
{
if (_event != null) _event.Invoke(arg);
}
/// <summary>
/// 登録されているメソッドを全てremoveします.
/// </summary>
public void Clear()
{
foreach (var del in _delegates)
{
_event -= del;
}
_delegates.Clear();
}
}
/// <summary>
/// UnityAction拡張イベントクラス ver引数2
/// </summary>
public class UnityActionWrapper<T0, T1>
{
/// <summary>
/// イベント.
/// </summary>
public event UnityAction<T0, T1> Event
{
add { lock (_event) _event += value; _delegates.Add(value); }
remove { lock (_event) _event -= value; _delegates.Remove(value); }
}
private event UnityAction<T0, T1> _event = (arg0, arg1) => { };
private List<UnityAction<T0, T1>> _delegates = new List<UnityAction<T0, T1>>();
/// <summary>
/// Add 拡張演算子.
/// </summary>
public static UnityActionWrapper<T0, T1> operator +(UnityActionWrapper<T0, T1> self, UnityAction<T0, T1> del)
{
lock (self._event)
{
self._event += del;
self._delegates.Add(del);
}
return self;
}
/// <summary>
/// Remove 拡張演算子.
/// </summary>
public static UnityActionWrapper<T0, T1> operator -(UnityActionWrapper<T0, T1> self, UnityAction<T0, T1> del)
{
lock (self._event)
{
self._event -= del;
self._delegates.Remove(del);
}
return self;
}
/// <summary>
/// 登録されているメソッドを実行します.
/// </summary>
public void Invoke(T0 arg0, T1 arg1)
{
if (_event != null) _event.Invoke(arg0, arg1);
}
/// <summary>
/// 登録されているメソッドを全てremoveします.
/// </summary>
public void Clear()
{
foreach (var del in _delegates)
{
_event -= del;
}
_delegates.Clear();
}
}
/// <summary>
/// UnityAction拡張イベントクラス ver引数3
/// </summary>
public class UnityActionWrapper<T0, T1, T2>
{
/// <summary>
/// イベント.
/// </summary>
public event UnityAction<T0, T1, T2> Event
{
add { lock (_event) _event += value; _delegates.Add(value); }
remove { lock (_event) _event -= value; _delegates.Remove(value); }
}
private event UnityAction<T0, T1, T2> _event = (arg0, arg1, arg2) => { };
private List<UnityAction<T0, T1, T2>> _delegates = new List<UnityAction<T0, T1, T2>>();
/// <summary>
/// Add 拡張演算子.
/// </summary>
public static UnityActionWrapper<T0, T1, T2> operator +(UnityActionWrapper<T0, T1, T2> self, UnityAction<T0, T1, T2> del)
{
lock (self._event)
{
self._event += del;
self._delegates.Add(del);
}
return self;
}
/// <summary>
/// Remove 拡張演算子.
/// </summary>
public static UnityActionWrapper<T0, T1, T2> operator -(UnityActionWrapper<T0, T1, T2> self, UnityAction<T0, T1, T2> del)
{
lock (self._event)
{
self._event -= del;
self._delegates.Remove(del);
}
return self;
}
/// <summary>
/// 登録されているメソッドを実行します.
/// </summary>
public void Invoke(T0 arg0, T1 arg1, T2 arg2)
{
if (_event != null) _event.Invoke(arg0, arg1, arg2);
}
/// <summary>
/// 登録されているメソッドを全てremoveします.
/// </summary>
public void Clear()
{
foreach (var del in _delegates)
{
_event -= del;
}
_delegates.Clear();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment