.Netコールバック

C# SDKはクラスで実装可能なコールバックを提供しています:

Photon Realtime C# SDKコールバックインターフェース:

  • IConnectionCallbacks: 接続関連のコールバック。
  • IInRoomCallbacks: ルーム内で発生するコールバック。
  • ILobbyCallbacks: ロビー関連のコールバック。
  • IMatchmakingCallbacks: マッチメイキング関連のコールバック。
  • IOnEventCallback: 受信したイベントのシングルコールバック。 C#イベントLoadBalancingClient.EventReceivedと同等です。
  • IWebRpcCallback: 受信中のWebRPCオペレーション応答のシングルコールバック。
  • IOnErrorInfoCallback: ErrorInfoイベントを受信するシングルコールバック。

例えばUnityでは、MonoBehaviourOnEnable()OnDisable()を使用できました。

これらのインターフェースの実装は任意ですが、コードの可読性と管理のしやすさを上げることから推奨しています。 また、ロジックの実行タイミングが的確に提供されるのでPhotonのフローとステートの管理がしやすくなります。 他の代替案では、ステートフラグフィールドの使用やポーリングでクライアントのネットワーク状況を確認したりすべてのネットワーククライアントの変更や受信したイベント、操作方法へのサブスクライビングが必要な場合があります。 これには、Photonの内部や仮想レベルについての精通した知識が必要とされますが、これに頭を悩ませることなく、ゲームに集中することもできます。

If an unhandled (uncaught) exception occurs in one of the implemented interfaces' callbacks' methods, all other implemented ones for the same interface, same signature and not already called, won't be called. This is due to the fact that we call the implemented interface callback methods of the same signature in a loop with by the order of registration (which could be random in Unity if you register in MonoBehaviour methods).

コールバックシステムを実装するよりもインターフェースを選んだほうがいい理由:

  • 確実にコールバックメソッドのシグネチャに注意が払われるようになる。これはインターフェースの実装時コンパイラによって保証される。
  • 論理的に関連のあるコールバックをシングルクラスへグループ化する
  • コールバックを備える他のメソッドと比べて、不要なデータのオーバーヘッドが少なくメモリリークを防ぐことができる

If you happen to have a method that has the exact signature as one of the callbacks' interfaces' methods or you wish to hide the callbacks methods (unless a cast is made) you could choose explicit interface implementation.

Back to top