在Unity 3D中使用PUN2进行VR多人游戏体验时遇到问题 [英] Problem using PUN2 for a VR multiplayer experience in Unity 3D

查看:693
本文介绍了在Unity 3D中使用PUN2进行VR多人游戏体验时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个简单的VR演示,其中两个用户可以连接到房间并在桌子上操作对象.我目前正在关注本教程.

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:

  • 复制了LocalAvatarRemoteAvatar预制并将其移至 资源

  • Duplicated LocalAvatar and RemoteAvatar prefabs and moved them to Resources

创建了脚本PhotonAvatarView脚本

PhotonAvarViewPhotonView分别配置并配置到两个预制件

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:

例如,可以将以下代码放置在您已经存在的代码中 网络管理器或任何其他已经处理网络的脚本 逻辑,它不属于我们创建的PhotonAvatarView脚本 较早.在这里,我们使用OnJoinedRoom回调.

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.

我的假设是,这是一个从头开始的教程(考虑到它是从一个空白的Unity项目开始的),但是我没有任何Network Manager脚本,因此有点困惑.将该代码放入新脚本中,然后仅将其附加到对象上似乎无效.

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.

我缺少一些零件吗?

推荐答案

我们收到了您的反馈,本教程将在此基础上进行审核和更新.

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:

在这种情况下需要的NetworkManager以及整个教程都需要实现两个PUN2回调:IMatchmakingCallbacks.OnJoinedRoomIOnEventCallback.OnEvent.虽然本教程可能建议可能有两个单独的类,每个类实现每个回调(NetworkManagerMyClass),但没有任何反对将它们分组在一个位置的建议. 在教程的实例化"部分的第一个代码块中,我们看到OnJoinedRoom方法被覆盖.这可能意味着原始编写者假定NetworkManager必须扩展MonoBehaviourPunCallbacks类. 从MonoBehaviourPunCallbacks类继承是实现PUN2回调的最简单,最快的方法:它是MonoBehaviour,它允许您有选择地覆盖需要的回调,并且仅覆盖需要的回调,它已经代表您处理了回调的注册和注销(分别在OnEnableOnDisable中),不需要记住所有回调的接口,并且还扩展了MonoBehaviourPun,如果将PhotonView附加到相同的GameObject,则该属性很容易将PhotonView公开.但是,您应该小心,因为它不会实现所有回调接口,而是大多数实现.它实现了IConnectionCallbacksIMatchmakingCallbacksIInRoomCallbacksILobbyCallbacksIWebRpcCallback.它不实现IOnEventCallbackIPunInstantiateMagicCallbackIPunObservableIPunOwnershipCallbacks. PUN2的实用程序接口也未实现,例如IPunTurnManagerCallbacks.

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
}

这篇关于在Unity 3D中使用PUN2进行VR多人游戏体验时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆