简体   繁体   English

Unity Networking:无法在所有客户端上生成场景对象

[英]Unity Networking: can't spawn scene objects on all clients

I can't really figure out how to solve my problem. 我真的不知道如何解决我的问题。 I have been looking for an answer but I couldn't find anything. 我一直在寻找答案,但找不到任何东西。

I have a button in my scene that can be pressed both by client and host. 我的场景中有一个按钮,客户端和主机都可以按下。 When the button is pressed, it creates a cube in the scene. 当按下按钮时,它将在场景中创建一个立方体。 The problem is that: the cube can be created only by the host and the host is the only user that can see it and manipulate it. 问题在于:只能由主机创建多维数据集,并且主机是唯一可以看到和操作它的用户。 My code is: 我的代码是:

public class CreateCube : NetworkBehaviour {


        GameObject cubo;
        float lastCollisionTime=0;
        float collisionTime=0;

        void OnCollisionExit(Collision other) {

                collisionTime = Time.time;
                if (collisionTime - lastCollisionTime >1.5) {
                    CmdCreaCubo ();
                    lastCollisionTime = collisionTime;
                }
            }
            }
        }
        [Command]
        void CmdCreaCubo(){
            GameObject cubo=Instantiate(Resources.Load("MyPrefabs\\Oggetti\\CubeGrasp")) as GameObject;
            cubo.transform.position = new Vector3 (-5.88f, 7.51f, -19f);
            cubo.name = "CubeGrasp";
            NetworkServer.Spawn (cubo);

        }
}

Could anyone help me please? 有人可以帮我吗? Thank you so much 非常感谢

Instead using simple Instantiate you should need to use Network.Instantiate 而不是使用简单的实例化,您应该使用Network.Instantiate

The given prefab will be instanted on all clients in the game. 给定的预制件将在游戏中的所有客户端上实例化。 Synchronization is automatically set up so there is no extra work involved. 同步是自动设置的,因此不涉及额外的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM