「Unity/Csharp/Delegate」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→delegateのサンプル) |
(→Delegateのイベント削除) |
||
(同じ利用者による、間の11版が非表示) | |||
行4: | 行4: | ||
==delegateのサンプル== | ==delegateのサンプル== | ||
"やっほー"をリクエストすると1/2の確率で"やっほー"を返してくれるサンプル | "やっほー"をリクエストすると1/2の確率で"やっほー"を返してくれるサンプル | ||
+ | |||
+ | delegateを宣言 | ||
*Mountain.cs | *Mountain.cs | ||
<pre> | <pre> | ||
行14: | 行16: | ||
{ | { | ||
Success, | Success, | ||
− | + | Failure, | |
NetworkError | NetworkError | ||
} | } | ||
public delegate void CustomCallback(ResponseType responseType); | public delegate void CustomCallback(ResponseType responseType); | ||
private CustomCallback callbacks; | private CustomCallback callbacks; | ||
− | public void | + | public void AddCallback(CustomCallback callback) |
{ | { | ||
callbacks += callback; | callbacks += callback; | ||
行28: | 行30: | ||
if (Random.Range(-10.0f, 10.0f) > 0) { | if (Random.Range(-10.0f, 10.0f) > 0) { | ||
this.msg = msg; | this.msg = msg; | ||
− | callbacks(ResponseType.Success); | + | if (callbacks != null) |
+ | { | ||
+ | callbacks(ResponseType.Success); | ||
+ | } | ||
} else { | } else { | ||
this.msg = ""; | this.msg = ""; | ||
− | callbacks(ResponseType. | + | if (callbacks != null) |
+ | { | ||
+ | callbacks(ResponseType.Failure); | ||
+ | } | ||
} | } | ||
} | } | ||
行41: | 行49: | ||
呼び出し元 | 呼び出し元 | ||
− | NewBehaviourScript.cs | + | *NewBehaviourScript.cs |
<pre> | <pre> | ||
public class NewBehaviourScript : MonoBehaviour { | public class NewBehaviourScript : MonoBehaviour { | ||
行49: | 行57: | ||
GameObject gameObj = new GameObject(); | GameObject gameObj = new GameObject(); | ||
mountain = gameObj.AddComponent<Mountain>(); | mountain = gameObj.AddComponent<Mountain>(); | ||
− | mountain. | + | mountain.AddCallback(OnResponse); |
mountain.Exec("やっほー"); | mountain.Exec("やっほー"); | ||
} | } | ||
行55: | 行63: | ||
public void OnResponse(Mountain.ResponseType responseType) | public void OnResponse(Mountain.ResponseType responseType) | ||
{ | { | ||
− | Debug.Log(" | + | Debug.Log("OnResponse=" + responseType); |
if (mountain) | if (mountain) | ||
{ | { | ||
行62: | 行70: | ||
Debug.Log("msg=" + mountain.GetMessage()); | Debug.Log("msg=" + mountain.GetMessage()); | ||
} | } | ||
− | else if (responseType.Equals(mountain.ResponseType. | + | else if (responseType.Equals(mountain.ResponseType.Failure)) |
{ | { | ||
Debug.Log("msg=" + mountain.GetMessage()); | Debug.Log("msg=" + mountain.GetMessage()); | ||
行73: | 行81: | ||
==httpでdelegateを使った例== | ==httpでdelegateを使った例== | ||
[[unity/Csharp/Request]] [ショートカット] | [[unity/Csharp/Request]] [ショートカット] | ||
+ | |||
+ | ==Delegateのイベント削除== | ||
+ | <pre> | ||
+ | public void RemoveAllEvents() | ||
+ | { | ||
+ | if (callbacks != null) | ||
+ | { | ||
+ | foreach (var callback in callbacks.GetInvocationList()) | ||
+ | { | ||
+ | callbacks -= (CustomCallback)callback; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </pre> | ||
+ | 参考:https://teratail.com/questions/325695 |
2023年9月23日 (土) 06:27時点における最新版
delegateとは
独自イベントを生成できるもの。httpのレスポンスなど少し待たないとレスポンスが戻ってこないところで使う。
delegateのサンプル
"やっほー"をリクエストすると1/2の確率で"やっほー"を返してくれるサンプル
delegateを宣言
- Mountain.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Mountain : MonoBehaviour { private string msg; public enum ResponseType { Success, Failure, NetworkError } public delegate void CustomCallback(ResponseType responseType); private CustomCallback callbacks; public void AddCallback(CustomCallback callback) { callbacks += callback; } public void Exec(string msg) { // 1/2の確率で成功 if (Random.Range(-10.0f, 10.0f) > 0) { this.msg = msg; if (callbacks != null) { callbacks(ResponseType.Success); } } else { this.msg = ""; if (callbacks != null) { callbacks(ResponseType.Failure); } } } public string GetMessage() { return msg; } }
呼び出し元
- NewBehaviourScript.cs
public class NewBehaviourScript : MonoBehaviour { Mountain mountain; // 送信 void Start () { GameObject gameObj = new GameObject(); mountain = gameObj.AddComponent<Mountain>(); mountain.AddCallback(OnResponse); mountain.Exec("やっほー"); } // 受信 public void OnResponse(Mountain.ResponseType responseType) { Debug.Log("OnResponse=" + responseType); if (mountain) { if (responseType.Equals(mountain.ResponseType.Success)) { Debug.Log("msg=" + mountain.GetMessage()); } else if (responseType.Equals(mountain.ResponseType.Failure)) { Debug.Log("msg=" + mountain.GetMessage()); } } } }
httpでdelegateを使った例
unity/Csharp/Request [ショートカット]
Delegateのイベント削除
public void RemoveAllEvents() { if (callbacks != null) { foreach (var callback in callbacks.GetInvocationList()) { callbacks -= (CustomCallback)callback; } } }