「Unity/Csharp/Action」の版間の差分
提供: 初心者エンジニアの簡易メモ
行10: | 行10: | ||
}; | }; | ||
OnMsg("hello"); | OnMsg("hello"); | ||
+ | </pre> | ||
+ | |||
+ | ==Actionサンプル(別クラス呼び出し)== | ||
+ | <pre> | ||
+ | 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(); | ||
+ | } | ||
+ | } | ||
+ | |||
</pre> | </pre> |
2022年10月29日 (土) 02:52時点における版
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(); } }