facebook twitter hatena line email

Unity/Csharp/Action

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

Actionとは

関数を変数にできるもの

Actionサンプル

System.Action<string> OnMsg = delegate (string msg) { };
OnMsg += (msg) =>
{
    Debug.Log("msg1:" + msg);
};
OnMsg("hello");

Actionサンプル(別クラス呼び出し)

public class ActionScene : MonoBehaviour
{
    void Start()
    {
        var eventActionClass = new EventActionClass();
        eventActionClass.Setup(
             callOnAction: () =>
             {
                 Debug.Log("callOnAction");
             },
             callOffAction: () =>
             {
                 Debug.Log("callOffAction");
             });
    }
}

public class EventActionClass
{
    public void Setup(System.Action callOnAction, System.Action callOffAction)
    {
        callOnAction();
        callOffAction();
    }
}