Skip to content

Instantly share code, notes, and snippets.

@iwashihead
Created October 3, 2016 09:10
Show Gist options
  • Save iwashihead/bf16bed95af29b348f0ae98695a3ae50 to your computer and use it in GitHub Desktop.
Save iwashihead/bf16bed95af29b348f0ae98695a3ae50 to your computer and use it in GitHub Desktop.
UnityActionのイベントラッパークラス
using System.Collections.Generic;
using UnityEngine.Events;
namespace IW.UnityActionEvent
{
/// <summary>
/// Extensionメソッド郡.
/// </summary>
public static class UnityActionWrapperExtension
{
public static void Release(this UnityActionWrapper self) { if (self) self.Clear(); self = null; }
public static void Release<T0>(this UnityActionWrapper<T0> self) { if(self) self.Clear(); self = null; }
public static void Release<T0, T1>(this UnityActionWrapper<T0, T1> self) { if (self) self.Clear(); self = null; }
public static void Release<T0, T1, T2>(this UnityActionWrapper<T0, T1, T2> self) { if (self) self.Clear(); self = null; }
}
/// <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>();
public static implicit operator bool(UnityActionWrapper self)
{
return self != null;
}
public static bool operator true(UnityActionWrapper self)
{
return self != null;
}
public static bool operator false(UnityActionWrapper self)
{
return self == null;
}
/// <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>>();
public static implicit operator bool(UnityActionWrapper<T0> self)
{
return self != null;
}
public static bool operator true(UnityActionWrapper<T0> self)
{
return self != null;
}
public static bool operator false(UnityActionWrapper<T0> self)
{
return self == null;
}
/// <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>>();
public static implicit operator bool(UnityActionWrapper<T0, T1> self)
{
return self != null;
}
public static bool operator true(UnityActionWrapper<T0, T1> self)
{
return self != null;
}
public static bool operator false(UnityActionWrapper<T0, T1> self)
{
return self == null;
}
/// <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>>();
public static implicit operator bool(UnityActionWrapper<T0, T1, T2> self)
{
return self != null;
}
public static bool operator true(UnityActionWrapper<T0, T1, T2> self)
{
return self != null;
}
public static bool operator false(UnityActionWrapper<T0, T1, T2> self)
{
return self == null;
}
/// <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