「Unity/Csharp/Action」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→Actionサンプル3引数で渡して戻す) |
|||
(同じ利用者による、間の9版が非表示) | |||
行2: | 行2: | ||
関数を変数にできるもの | 関数を変数にできるもの | ||
− | == | + | ==Actionサンプル1== |
<pre> | <pre> | ||
System.Action<string> OnMsg = delegate (string msg) { }; | System.Action<string> OnMsg = delegate (string msg) { }; | ||
行9: | 行9: | ||
Debug.Log("msg1:" + msg); | Debug.Log("msg1:" + msg); | ||
}; | }; | ||
+ | |||
+ | // 呼び出し | ||
OnMsg("hello"); | OnMsg("hello"); | ||
</pre> | </pre> | ||
− | == | + | ===ラムダ式だとこうかける=== |
+ | <pre> | ||
+ | System.Action<string> OnMsg = (msg) => | ||
+ | { | ||
+ | Debug.Log("msg1:" + msg); | ||
+ | }; | ||
+ | |||
+ | // 呼び出し | ||
+ | OnMsg("hello"); | ||
+ | </pre> | ||
+ | |||
+ | ==Actionサンプル2(別クラス呼び出し)== | ||
+ | <pre> | ||
+ | public class ActionScene : MonoBehaviour | ||
+ | { | ||
+ | void Start() | ||
+ | { | ||
+ | var eventActionClass = new EventActionClass(); | ||
+ | eventActionClass.Setup( | ||
+ | callback: () => | ||
+ | { | ||
+ | Debug.Log("callback"); | ||
+ | }); | ||
+ | } | ||
+ | } | ||
+ | public class EventActionClass | ||
+ | { | ||
+ | public void Setup(System.Action callback) | ||
+ | { | ||
+ | callback(); // callback.Invoke(); でもOK | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </pre> | ||
+ | |||
+ | ===Setup()の部分、ラムダ式の変数に分けると、こうかける=== | ||
+ | <pre> | ||
+ | public class ActionScene : MonoBehaviour | ||
+ | { | ||
+ | void Start() | ||
+ | { | ||
+ | System.Action callback = () => | ||
+ | { | ||
+ | Debug.Log("callback"); | ||
+ | }; | ||
+ | var eventActionClass = new EventActionClass(); | ||
+ | eventActionClass.Setup( | ||
+ | callback: callback); | ||
+ | } | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | ==Actionサンプル3(Actionを複数呼び出し)== | ||
<pre> | <pre> | ||
public class ActionScene : MonoBehaviour | public class ActionScene : MonoBehaviour | ||
行30: | 行84: | ||
} | } | ||
} | } | ||
− | |||
public class EventActionClass | public class EventActionClass | ||
{ | { | ||
行40: | 行93: | ||
} | } | ||
+ | </pre> | ||
+ | |||
+ | ===Setup()の部分、ラムダ式の変数に分けると、こうかける=== | ||
+ | <pre> | ||
+ | public class ActionScene : MonoBehaviour | ||
+ | { | ||
+ | void Start() | ||
+ | { | ||
+ | System.Action onAction = () => | ||
+ | { | ||
+ | Debug.Log("callOnAction"); | ||
+ | }; | ||
+ | System.Action offAction = () => | ||
+ | { | ||
+ | Debug.Log("callOffAction"); | ||
+ | }; | ||
+ | var eventActionClass = new EventActionClass(); | ||
+ | eventActionClass.Setup( | ||
+ | callOnAction: onAction, | ||
+ | callOffAction: offAction); | ||
+ | } | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | ==Actionサンプル4、引数で渡して戻す== | ||
+ | <pre> | ||
+ | public class ActionScene : MonoBehaviour | ||
+ | { | ||
+ | void Start() | ||
+ | { | ||
+ | System.Action<string> event = (name) => | ||
+ | { | ||
+ | Debug.Log(name); // hogeが返ってくる | ||
+ | }; | ||
+ | var sampleLogic new SampleLogic(); | ||
+ | sampleLogic.Load(event); | ||
+ | } | ||
+ | } | ||
+ | public class SampleLogic | ||
+ | { | ||
+ | public void Load(System.Action<string> event) | ||
+ | { | ||
+ | event("hoge"); | ||
+ | } | ||
+ | } | ||
</pre> | </pre> |
2024年12月14日 (土) 03:05時点における最新版
目次
Actionとは
関数を変数にできるもの
Actionサンプル1
System.Action<string> OnMsg = delegate (string msg) { }; OnMsg += (msg) => { Debug.Log("msg1:" + msg); }; // 呼び出し OnMsg("hello");
ラムダ式だとこうかける
System.Action<string> OnMsg = (msg) => { Debug.Log("msg1:" + msg); }; // 呼び出し OnMsg("hello");
Actionサンプル2(別クラス呼び出し)
public class ActionScene : MonoBehaviour { void Start() { var eventActionClass = new EventActionClass(); eventActionClass.Setup( callback: () => { Debug.Log("callback"); }); } } public class EventActionClass { public void Setup(System.Action callback) { callback(); // callback.Invoke(); でもOK } }
Setup()の部分、ラムダ式の変数に分けると、こうかける
public class ActionScene : MonoBehaviour { void Start() { System.Action callback = () => { Debug.Log("callback"); }; var eventActionClass = new EventActionClass(); eventActionClass.Setup( callback: callback); } }
Actionサンプル3(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(); } }
Setup()の部分、ラムダ式の変数に分けると、こうかける
public class ActionScene : MonoBehaviour { void Start() { System.Action onAction = () => { Debug.Log("callOnAction"); }; System.Action offAction = () => { Debug.Log("callOffAction"); }; var eventActionClass = new EventActionClass(); eventActionClass.Setup( callOnAction: onAction, callOffAction: offAction); } }
Actionサンプル4、引数で渡して戻す
public class ActionScene : MonoBehaviour { void Start() { System.Action<string> event = (name) => { Debug.Log(name); // hogeが返ってくる }; var sampleLogic new SampleLogic(); sampleLogic.Load(event); } } public class SampleLogic { public void Load(System.Action<string> event) { event("hoge"); } }