「Unity/Csharp/コールバック」の版間の差分
提供: 初心者エンジニアの簡易メモ
行19: | 行19: | ||
CommentAdded, | CommentAdded, | ||
} | } | ||
− | public delegate void CustomCallback(ResponseType responseType); | + | public delegate void CustomCallback(ResponseType responseType, string comment); |
private CustomCallback callbacks; | private CustomCallback callbacks; | ||
public void AddCallback(CustomCallback callback) | public void AddCallback(CustomCallback callback) | ||
行30: | 行30: | ||
if (callbacks != null) | if (callbacks != null) | ||
{ | { | ||
− | callbacks(ResponseType.CommentAdded, comment); | + | callbacks(ResponseType.CommentAdded, "comment!!"); |
} | } | ||
} | } | ||
行42: | 行42: | ||
manager.AddCallback(OnResponse); | manager.AddCallback(OnResponse); | ||
} | } | ||
− | void OnResponse(SampleManager.ResponseType responseType) | + | void OnResponse(SampleManager.ResponseType responseType, string comment) |
{ | { | ||
− | Debug.Log("OnResponse=" + responseType); | + | Debug.Log("OnResponse=" + responseType + " comment=" + comment); |
} | } | ||
</pre> | </pre> | ||
==実装例== | ==実装例== | ||
[[Unity/Csharp/Request]] [ショートカット] | [[Unity/Csharp/Request]] [ショートカット] |
2021年11月11日 (木) 04:41時点における版
サンプル
public class SampleManager : MonoBehaviour { private static SampleManager mInstance; private SampleManager() { } public static SampleManager Instance { get { if (mInstance == null) mInstance = new SampleManager(); return mInstance; } } public enum ResponseType { CommentAdded, } public delegate void CustomCallback(ResponseType responseType, string comment); private CustomCallback callbacks; public void AddCallback(CustomCallback callback) { callbacks += callback; } public void CommentAdded() { // 色々処理 if (callbacks != null) { callbacks(ResponseType.CommentAdded, "comment!!"); } } }
Main.cs
void Start() { manager = SampleManager.Instance; manager.AddCallback(OnResponse); } void OnResponse(SampleManager.ResponseType responseType, string comment) { Debug.Log("OnResponse=" + responseType + " comment=" + comment); }
実装例
Unity/Csharp/Request [ショートカット]