LoginSignup
2
1

More than 5 years have passed since last update.

【メモ】UnityEventのUnityActionを抽出する

Last updated at Posted at 2017-04-28

#この記事について
UnityEvent型を用意すると Buttonコンポーネントのようにインスペクターでメソッドを指定する事が出来るようになります。
この記事は、UnityEventに指定されたUnityAction(この中にメソッドがある?)を抽出するやり方のメモ書きです。
ものすごく調べた経緯があるのでメモ書きを残す事に致しました。

#この記事の内容の実用性
尚実際に使うケースはコンポーネントを作る時などになるのかと思います。

しかし、今回の記事では最小のコードに絞っているので
そのままでは 目的を果たすだけ つまり実用性はありません。
その点は何卒ご了承ください。

以下サンプルです。

#最小限のサンプル
以下はメソッドが格納されているクラス

Sample.cs
//<Summary>
//このSampleのメソッドをGetActionのUnityEventに指定する
//</Summary>

    public void TestA()
    {
        Debug.logger.Log("TestA");
    }

    public void TestB()
    {
        Debug.logger.Log("TestB");
    }

GameObjectにこのGetActionをAdd
Unity Eventに 上記のSampleクラスのメソッドを指定する

GetAction.cs
    public UnityEvent SetUnityEvent;/*< UnityEvent*/
    private List<Action> m_getActionList = new List<Action>();/* <取得したActionの格納先*/


    void Start()
    {

        int count = SetUnityEvent.GetPersistentEventCount();

        for (int i = 0; i < count; ++i)
        {
            //CreateDelegateでUnityActionを作っている
            Action action = (Action)System.Delegate.CreateDelegate(typeof(Action), SetUnityEvent.GetPersistentTarget(i), SetUnityEvent.GetPersistentMethodName(i));
            m_getActionList.Add(action);
            Debug.logger.Log("m_getActionList:" + m_getActionList[i].Method);

        }
    }

中身はLogで確認

2
1
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
2
1