facebook twitter hatena line email

Unity/Csharp/コールバック

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

サンプル

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);
    manager.CommentAdded();
}
void OnResponse(SampleManager.ResponseType responseType, string comment)
{
    Debug.Log("OnResponse=" + responseType + " comment=" + comment);
}

実装例

Unity/Csharp/Request [ショートカット]