本文介绍了Unity 5.1 网络 - 为主机和所有客户端生成一个对象作为子对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以装备多种武器的玩家对象.装备武器时,其变形的父级设置为其手.我已经弄乱了一段时间,无法让它对主机和客户端都起作用.现在我正在尝试在服务器上装备武器,并告诉所有客户端设置他们的父母变换.

I have a player object who can equip multiple weapons. When a weapon is equipped, its transform's parent is set to its hand. I have messed around with this for some time and cannot get this to work for both the host and the client. Right now I am trying to equip the weapon on the server, and tell all the clients to set their parents transforms.

public NetworkInstanceId weaponNetId; 

   [Command]
    void Cmd_EquipWeapon()
    {
        var weaponObject = Instantiate (Resources.Load ("Gun"),
                                        hand.position,
                                        Quaternion.Euler (0f, 0f, 0f)) as GameObject;

        weaponObject.transform.parent = hand;

        NetworkServer.Spawn (weaponObject);

        //set equipped weapon
        var weapon = weaponObject.GetComponent<Weapon> () as Weapon;

        weaponNetId = weaponObject.GetComponent<NetworkIdentity> ().netId;
        Rpc_SetParentGameobject (weaponNetId);
    }

    [ClientRpc]
    public void Rpc_SetParentGameobject(NetworkInstanceId netID)
    {   
        weaponNetId = netId;
    }

在更新中我正在更新武器变换

And in the update I am updating the weapons transform

    void Update () {

    // set child weapon tranform on clients
    if (!isServer) {
        if (weaponNetId.Value != 0 && !armed) {
            GameObject child = NetworkServer.FindLocalObject (weaponNetId);


            if (child != null) {
                child.transform.parent = hand;
            }
        }
    }

我知道这不是最优化的方法.但现在我只是试图让它以任何可能的方式工作,然后努力调整它.看起来应该是一个简单的任务.

I know this isn't the most optimized way to do this..but right now I am just trying to get this to work any way possible and then work on tweaking it. Seems like it should be a simple task.

推荐答案

我们在多人游戏中做了类似的事情.您需要做一些事情才能使其正常工作.一、概念:

We do a similar thing in our multiplayer game. There are a few things you need to do to get this working. Firstly, the concept:

如您所见,在服务器上设置武器的父级是微不足道的.只需像在 Unity 中一样设置转换的父级即可.但是,在使用 NetworkServer.Spawn 在服务器上生成此对象后,它将稍后在场景根中的客户端上生成(生成的预制件之外的层次结构不同步).

Setting the weapon's parent on the server is trivial, as you have found. Simply set the transform's parent as you would normally in Unity. However, after spawning this object on the server with NetworkServer.Spawn, it will later be spawned on clients in the root of the scene (hierarchy outside of the spawned prefab is not synchronised).

因此,为了调整客户端的层次结构,我建议您:

So in order to adjust the hierarchy on the client I would suggest that you:

  • 使用 SyncVar 在服务器和客户端之间同步父对象的 netID.
  • 在客户端生成对象时,使用同步的 netID 查找父对象并将其设置为转换的父对象.

因此,我会将您的代码调整为如下所示.首先,在生成武器之前设置武器的父 netId.这将确保当它在客户端上生成时,将设置 netId.

Therefore, I would adjust your code to look something like this. Firstly, set the weapon's parent netId before you spawn it. This will ensure that when it is spawned on clients, the netId will be set.

[Command]
void Cmd_EquipWeapon()
{
    var weaponObject = Instantiate (Resources.Load ("Gun"),
                                    hand.position,
                                    Quaternion.Euler (0f, 0f, 0f)) as GameObject;
    weaponObject.parentNetId = hand.netId; // Set the parent network ID
    weaponObject.transform.parent = hand; // Set the parent transform on the server

    NetworkServer.Spawn (weaponObject); // Spawn the object
}


然后在你的武器类中:


And then in your weapon class:

  • 添加 parentNetId 属性.
  • 将其标记为 [SyncVar] 以便它在服务器和客户端副本之间同步.
  • 在客户端上生成时,使用 netId 查找父对象并将其设置为我们变换的父对象.
  • Add a parentNetId property.
  • Mark it as [SyncVar] so that it synchronises between server and client copies.
  • When spawned on a client, find the parent object using the netId and set it to our transform's parent.

也许是这样的:

[SyncVar]
public NetworkInstanceId parentNetId;

public override void OnStartClient()
{
    // When we are spawned on the client,
    // find the parent object using its ID,
    // and set it to be our transform's parent.
    GameObject parentObject = ClientScene.FindLocalObject(parentNetId);
    transform.SetParent(parentObject.transform);
}

这篇关于Unity 5.1 网络 - 为主机和所有客户端生成一个对象作为子对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-19 06:50