【问题标题】:Unity Netcode Networked GameObject not Appearing on ClientUnity Netcode Networked GameObject 未出现在客户端
【发布时间】:2022-10-02 04:23:39
【问题描述】:

我正在尝试构建一个基于图块的多人策略游戏。根据我的理解,我应该创建一个“播放器”GameObject,然后将地图存储为带有NetworkObject 组件的GameObjects 的集合,以便从客户端同步到服务器。

但是,我在主机和客户端之间同步游戏对象时遇到问题。创建服务器时,只有服务器应该生成地图(地图生成代码标记)。当玩家连接时,他们应该获得自己的游戏组件。

namespace HelloWorld
{
    public class HelloWorldManager : MonoBehaviour
    {
        public GameObject networkedPrefab;

        void OnGUI()
        {
            GUILayout.BeginArea(new Rect(10, 10, 300, 300));
            if (!NetworkManager.Singleton.IsClient && !NetworkManager.Singleton.IsServer)
            {
                if (GUILayout.Button(\"Client\")) NetworkManager.Singleton.StartClient();
                if (GUILayout.Button(\"Server\")) {
                    // Generate \'map\' here of 10 items
                    for (int i = 0; i < 10; i++) {
                        var go = Instantiate(networkedPrefab);
                        go.transform.position = new Vector2((i - 5), 0);
                        go.gameObject.name = \"Circle-\" + i;
                    }
                    NetworkManager.Singleton.StartServer();
                } 
            }
            else
            {
                StatusLabels(); 
                SubmitNewPosition(); // Adds button for player to randomly move their position
            }

            GUILayout.EndArea();
        }

static void SubmitNewPosition()
        {
            if (GUILayout.Button(NetworkManager.Singleton.IsServer ? \"Move\" : \"Request Position Change\"))
            {
                if (NetworkManager.Singleton.IsServer && !NetworkManager.Singleton.IsClient )
                {
                    foreach (ulong uid in NetworkManager.Singleton.ConnectedClientsIds)
                        NetworkManager.Singleton.SpawnManager.GetPlayerNetworkObject(uid).GetComponent<HelloWorldPlayer>().Move();
                }
                else
                {
                    var playerObject = NetworkManager.Singleton.SpawnManager.GetLocalPlayerObject();
                    var player = playerObject.GetComponent<HelloWorldPlayer>();
                    player.Move();
                }
            }
        }
    }
}

这是我在 0 个客户中得到的结果(地图由圆圈表示):

添加客户端后,这是我在服务器端得到的:

...在客户端我什么也没得到:

这是我使用的预制件:

任何帮助让事情在服务器和客户端之间同步将不胜感激。

    标签: unity3d unity-networking


    【解决方案1】:

    由于您的对象是在运行时生成的(而不是保存在场景中),因此您需要在网络对象上调用 Spawn()。如果您的对象保存在场景中,但在运行时动态实例化它们需要 spawn 调用,则添加 NetworkObject 就足够了。

    对于您的地图:

       for (int i = 0; i < 10; i++) {
           var go = Instantiate(networkedPrefab);
           go.transform.position = new Vector2((i - 5), 0);
           go.gameObject.name = "Circle-" + i;
           go.GetComponent<NetworkObject>().Spawn();
       }
    

    顺便说一句,我思考您需要在进行生成之前启动服务器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多