Convert PUN 2's callback to UniRx's Operator.
Photon Unity Networking 2 has many callbacks.
Since each event is not independent, it needs to be handled in order.
By treating it as an operator of UniRx, Using PUN2Rx, handling of the event becomes easy.
Also, in order to receive PUN 2 's event callback you need to inherit MonoBehaviourPunCallbacks or inherit each interface.
You can freely change the base class by using PUN2Rx.
PUN2Rx classified them into two types - OnNext only, OnError only.
Most importantly, the normal event operator notifies OnNext only and the error operator notifies OnError only.
With this, it is possible to handle normal events and errors with one Subscribe.
See XMLdoc or table.
WARN: Task isn't supported yet.
Release
download .unitypackage
-
.NET 4.6
-
PUN2 v2.7
-
UniRx 6.2.2
NOTE: Even different versions, it works. Maybe I think...
var successStream = this.OnCreateRoomAsObservable();
var failureStream = this.OnCreateRoomFailedAsObservable();
successStream.Merge(failureStream)
.Subscribe(
unit => { Debug.Log("success!"); },
exception => { Debug.Log("failed..."); }
);
// room name you want to join
var roomName = "roomName";
// OnNext --- (ConnectedToMaster => OnJoinedRoom)
var successStream =
this.OnConnectedToMasterAsObservable().Take(1).IgnoreElements()
.DoOnCompleted(() =>
{
Debug.Log("connect finish!");
Debug.Log("join room!");
var roomOptions = new RoomOptions();
PhotonNetwork.JoinOrCreateRoom(roomName, roomOptions, TypedLobby.Default);
}
).Concat(this.OnJoinedRoomAsObservable());
// OnError --- (OnDisconnected or OnJoinRoomFailed)
var failureStream = this.OnDisconnectedAsObservable()
.Where(cause => cause != DisconnectCause.None && cause != DisconnectCause.DisconnectByClientLogic)
.Select(cause =>
{
throw PUN2Exception.Create((short) cause, cause.ToString());
return Unit.Default;
}).Merge(this.OnCreateRoomFailedAsObservable(), this.OnJoinRoomFailedAsObservable());
Observable.Amb(successStream, failureStream)
.Subscribe(
unit => { Debug.Log("join room finish!"); },
exception => { Debug.Log(exception); })
.AddTo(this);
Debug.Log("connect!");
PhotonNetwork.ConnectUsingSettings();
Operator | Call | Type |
---|---|---|
Photon.Realtime.IConnectionCallbacks |
- | - |
OnConnectedAsObservable | OnNext | Unit |
OnConnectedToMasterAsObservable | OnNext | Unit |
OnDisconnectedAsObservable | OnNext | DisconnectCause |
OnRegionListReceivedAsObservable | OnNext | RegionHandler |
OnCustomAuthenticationResponseAsObservable | OnNext | Tuple<string, object> |
OnCustomAuthenticationFailedAsObservable | OnError | Unit |
Photon.Realtime.IInRoomCallbacks |
- | - |
OnPlayerEnteredRoomAsObservable | OnNext | Player |
OnPlayerLeftRoomAsObservable | OnNext | Player |
OnRoomPropertiesUpdateAsObservable | OnNext | HashTable |
OnPlayerPropertiesUpdateAsObservable | OnNext | Tuple<PLayer, Hashtable> |
OnMasterClientSwitchedAsObservable | OnNext | Player |
Photon.Realtime.ILobbyCallbacks |
||
OnJoinedLobbyAsObservable | OnNext | Unit |
OnLeftLobbyAsObservable | OnNext | Unit |
OnRoomListUpdateAsObservable | OnNext | List<RoomInfo> |
OnLobbyStatisticsUpdateAsObservable | OnNext | List<TypedLobbyInfo> |
Photon.Realtime.IMatchmakingCallbacks |
||
OnFriendListUpdateAsObservable | OnNext | List<FriendInfo> |
OnCreateRoomAsObservable | OnNext | Unit |
OnCreateRoomFailedAsObservable | OnError | Unit |
OnJoinedRoomAsObservable | OnNext | Unit |
OnJoinRoomFailedAsObservable | OnError | Unit |
OnJoinRandomFailedAsObservable | OnError | Unit |
OnLeftRoomAsObservable | OnNext | Unit |
Photon.Pun.IPunOwnershipCallbacks |
||
OnOwnershipRequestAsObservable | OnNext | Tuple<PhotonView, Player> |
OnOwnershipTransferredAsObservable | OnNext | Tuple<PhotonView, Player> |