facebook twitter hatena line email

「Unity/Csharp/コールバック」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
 
行41: 行41:
 
     manager = SampleManager.Instance;
 
     manager = SampleManager.Instance;
 
     manager.AddCallback(OnResponse);
 
     manager.AddCallback(OnResponse);
 +
    manager.CommentAdded();
 
}
 
}
 
void OnResponse(SampleManager.ResponseType responseType, string comment)
 
void OnResponse(SampleManager.ResponseType responseType, string comment)

2021年11月11日 (木) 04:44時点における最新版

サンプル

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 [ショートカット]