We got your feedback and the tutorial will be reviewed and updated based on this.
To help you with your issue and answer your question:
The NetworkManager you need in this case and following the tutorials in whole needs to implement two PUN2 callbacks: IMatchmakingCallbacks.OnJoinedRoom
and IOnEventCallback.OnEvent
. While the tutorial may suggest that there could be two separate classes one implementing each callback (NetworkManager
and MyClass
), there is nothing against grouping them in one place.
In the tutorial's "Instantiating Avatars" section's first code block, we see that the OnJoinedRoom
method is overridden. This probably means that the original writer assumed that the NetworkManager must be extending MonoBehaviourPunCallbacks
class.
Inheriting from MonoBehaviourPunCallbacks
class is the easiest and fastest way to implement PUN2 callbacks: it's a MonoBehaviour
that allows you to selectively override the callbacks you need and only those you need, it already handles callbacks registration and deregistration on your behalf (respectively in OnEnable
and OnDisable
) it does not require having to remember all the callbacks' interfaces and it also extends MonoBehaviourPun
which exposes the PhotonView
easily in a property if the latter is attached to the same GameObject. However, you should be careful as it does not implement all callbacks interfaces but most. It implements IConnectionCallbacks
, IMatchmakingCallbacks
, IInRoomCallbacks
, ILobbyCallbacks
and IWebRpcCallback
. It does not implement IOnEventCallback
, IPunInstantiateMagicCallback
, IPunObservable
and IPunOwnershipCallbacks
. PUN2's utility interfaces are also not implemented e.g. IPunTurnManagerCallbacks
.
Talk is cheap, show me the code:
public class NetworkManager : MonoBehaviour, IMatchmakingCallbacks, IOnEventCallback
{
public const byte InstantiateVrAvatarEventCode = 1;
public void OnJoinedRoom() // w/o override
{
// code from the tutorial here
}
public void OnEvent(EventData photonEvent)
{
// code from the tutorial here
}
private void OnEnable()
{
PhotonNetwork.AddCallbackTarget(this);
}
private void OnDisable()
{
PhotonNetwork.RemoveCallbackTarget(this);
}
#region Unused IMatchmakingCallbacks
public void OnFriendListUpdate(List<FriendInfo> friendList)
{
}
public void OnCreatedRoom()
{
}
public void OnCreateRoomFailed(short returnCode, string message)
{
}
public void OnJoinRoomFailed(short returnCode, string message)
{
}
public void OnJoinRandomFailed(short returnCode, string message)
{
}
public void OnLeftRoom()
{
}
#endregion
}