Problem using PUN2 for a VR multiplayer experience in Unity 3D

你说的曾经没有我的故事 提交于 2020-03-05 07:34:11

问题


I'm trying to create a simple VR demo where two users can connect to a room a manipulate objects on a table. I'm currently following this tutorial.

I have followed it step-by-step:

  • Duplicated LocalAvatar and RemoteAvatar prefabs and moved them to Resources

    Created the script PhotonAvatarView Script

    Attached and configured PhotonAvarView and PhotonView to both prefabs

The problem is that I get stuck at the "Instantiate Avatars" section, specifically at the section:

The following code can be placed for example in your already existing Network Manager or any other script that already deals with network logic, it doesn't belong to the PhotonAvatarView script we created earlier. Here we are using the OnJoinedRoom callback.

My assumption is that this is a tutorial from scratch (given the way it starts with a blank Unity project), but I don't have any Network Manager script in place so I'm a bit confused. Putting that code into a new script and just attaching it to an object doesn't seem working.

Am I missing some parts?


回答1:


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
}


来源:https://stackoverflow.com/questions/57851568/problem-using-pun2-for-a-vr-multiplayer-experience-in-unity-3d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!