简体   繁体   English

Unity,销毁客户端对象

[英]Unity, Client Object Destroying

hey guys so i got a problem with destroying objects over the network as client right so ive got a pickup system where you walk up to objects and pick them up, when you pick them up they are added to your inventory and the item on the ground is destroyed. 嗨,大家好,我是通过客户端销毁网络上的物品的问题,所以我有一个提货系统,您可以走到这些物品上并捡起它们,当您捡起它们时,它们会被添加到库存中,并将其放入地面被摧毁。 now this all works fine when your the server picking the items up but when your the client it just simplily doesnt work all it does is add the item to your inventory. 现在,当您的服务器提货时,这一切都很好,但是当您的客户只是简单地不起作用时,将货品添加到库存中就可以了。 now i see things all the time about how just make it a command it is a command and it doesnt seem to work, the items you are interacting with have local player authority and are registered and the player has local player athority but it just doesnt work. 现在我一直在查看有关如何将其变成命令的命令,它似乎不起作用,您正在与之交互的项目具有本地玩家权限并已注册,并且玩家具有本地玩家的态度,但这只是行不通。 this is my script 这是我的剧本

[Command]
public void CmdAddItem(GameObject hitcollider, int i)

{
    ClientScene.RegisterPrefab (hitcollider);
    NetworkServer.Destroy (hitcollider);
}

i also have done the same thing with a ClientRPC (it is the exact same code and is called the same time as the command) but it still doesnt work thanks 我也用ClientRPC做过同样的事情(它是完全相同的代码,并且与命令被调用的时间相同),但仍然无法正常工作,谢谢

If you want the server to destroy given object it first has to be registered as spawnable prefab on NetworkManager of all clients, it has to have NetworkIdentity and it has to be spawned by server. 如果要让服务器销毁给定的对象,则首先必须在所有客户端的NetworkManager上将其注册为可生成的预制件,它必须具有NetworkIdentity并且必须由服务器生成。

If you've already got this then simply call this function: 如果您已经有了此功能,则只需调用此函数:

    [Command]
    public void CmdDestroyObject(NetworkInstanceId netID)
    {
        GameObject theObject = NetworkServer.FindLocalObject (netID);
        NetworkServer.Destroy (theObject);
    }

Where netID is the NetworkInstanceId of the object you want to destroy. 其中netID是您要销毁的对象的NetworkInstanceId。

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

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