LoginSignup
4
2

More than 5 years have passed since last update.

UnityActionのeventラッパークラス

Posted at

使用例

よく使うフレーズをラッピング
演算子を拡張したのでこんな風に書ける

/*** 使用例 ***/
var OnDamage = new UnityActionWrapper<int>();

// ダメージ通知の関数を登録.
OnDamage += (damage) => { Player.SetDamage(damage); };

// 実際にダメージを与える
OnDamage.Invoke(100);

// ~~~ (略) ~~~

// 登録した関数を全破棄.
OnDamage.Clear();

コード

"お約束"となるadd/removeプロパティとか
結構めんどくさいイベントの全破棄など、その辺を実装

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();
    }
}
4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2