c# - Unity Networking-UNet 中的附加场景加载

标签 c# unity3d multiplayer unity-networking unity3d-unet

简短描述:我正在加载一个主场景中的附加场景,它加载正常但附加场景的游戏对象(包含网络标识组件)正在禁用

详细信息: 我正在通过此代码加载附加场景,以便附加场景加载到我的服务器和所有工作正常的客户端中:

  [ClientRpc]
    public void RpcLoadLevelAcrossNetwork() {
        SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);    
    }

虽然它已将我的场景加载到客户端/服务器中,但带有 network identity 的对象被禁用(这是默认行为,因为我检查了 Unity UNetSceneObjects 指出:

All objects in the scene with a NetworkIdentity component will be disabled when the scene is loaded; on both the client and the server. Then, when the scene is fully loaded, NetworkServer.SpawnObjects() is called to activate these networked scene objects. This will be done automatically by the NetworkManager when the server scene finishes loading - or can be called directly by user code.

正如文档所述,它将自动激活网络身份对象,但在我的上下文中并没有发生。我的场景加载正常,但它的网络身份对象未激活。

我做错了什么?

我还尝试通过调用 NetworkServer.SpawnObjects() 来激活我自己的对象 在我新加载的场景中,但它只在服务器端生成对象,同时在客户端显示错误

Spawn scene object not found for 1 Spawn scene object not found for 2 Spawn scene object not found for 3..... . . .

任何 Unet Champion 都可以帮助我吗?

编辑/更新方法:

我已经根据 unity forum discussion 更改了我的代码对于附加场景加载,它在服务器上加载我的附加场景时出现错误(如下所示)并且我客户端的场景网络身份对象仍然被禁用:

错误:

Ready() called with invalid connection object: conn=null

另一个代码尝试:

 #region AdditiveSceneLoadingSetup

    [Command]//calling this for additive scene load- its start
    public void CmdLoadAdditiveSceneMainCall() {

        RpcLoadAdditiveScene();
        StartCoroutine(LoadAdditiveSceneAtServer());

    }

    [ClientRpc]
    public void RpcLoadAdditiveScene() {
        Debug.Log("RpcLoadAdditiveScene");
        StartCoroutine(LoadAdditiveSceneAtClient());
    }

    public IEnumerator LoadAdditiveSceneAtClient() {
        Debug.Log("LoadAdditiveSceneAtClient");
        yield return SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);
        List<NetworkConnection> networkConnectionList = FindObjectOfType<MyNetworkManagerWithVR>().networkConnectionList;
        for (int i = 0; i < networkConnectionList.Count; i++)
        {
            ClientScene.Ready(networkConnectionList[i]);
        }
        //ClientScene.Ready(this.connectionToServer);//connectionToServer
    }

    public IEnumerator LoadAdditiveSceneAtServer() {
        Debug.Log("LoadAdditiveSceneAtServer");
        NetworkServer.SetAllClientsNotReady();
        yield return SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);
        NetworkServer.SpawnObjects();
    }


    #endregion AdditiveSceneLoadingSetup

最佳答案

来晚了,但我也遇到了这个问题并在这里提出了我自己的解决方案:

https://gist.github.com/kristianpd/485fd7d78512a22a80117a2d22664185

问题的核心是 SceneId 在 NetworkIdentity 上重复使用,当您加载另一个场景时,它将与当前的 NetworkIdentity.sceneId 冲突。

要了解有关该解决方案的更多背景信息,我在这篇论坛帖子中介绍了一些不同的尝试,然后才找到该解决方案。

https://forum.unity.com/threads/additive-scene-loading-with-unet-conflicting-sceneids-on-scene-objects.525410/#post-3545410

关于c# - Unity Networking-UNet 中的附加场景加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41909305/

相关文章:

c# - Unity Accord.net 加博

ios - 尝试编写 Unity iOS 插件时出现 Malloc 错误

.net - 无法加载调试符号 VS2017 - Unity .NET 4.X

python - Twisted Spread 适合多人赛车模拟游戏吗?

android - 没有互联网的本地网络多人游戏

c# - "is"运算符(operator)行为有点奇怪

c# - LINQ 使用左外连接对多个字段进行连接查询

c# - 解析JSON POST请求C#

c# - 如何在没有用户名和密码但仅使用 token 的情况下访问 REST API 方法?

asp.net - 回合制游戏 : Use SignalR or both SignalR and REST API?