Unity 5.1 Networking - Spawn an object as a child for the host and all clients

安稳与你 提交于 2019-12-01 06:38:48

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:

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:

  • Use a SyncVar to synchronise the netID of the parent object between the server and client.
  • When the object is spawned on the client, find the parent using the synchronised netID and set it as your transform's parent.

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:

  • 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.

Perhaps something like:

[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);
}
JonJon

I found this post really useful but I have a small addendum.

The netId will be on the root of the hierarchy so its useful to know that you can traverse down the hierarchy using transform.Find.

Something like this ..

GameObject parentObject = ClientScene.FindLocalObject(parentNetId);
    string pathToWeaponHolder = "Obj/targetObj";

    transform.SetParent(parentObject.transform.Find(pathToWeaponHolder));
rodamn

After reading Andy Barnard's solution, I came up with this slightly modified solution. Instead of a SyncVar, there is a Client RPC for the server to call any time a NetworkIdentity needs to change parents. This does require the parent to also have a NetworkIdentity (though it need not be a registered prefab).

public void Server_SetParent (NetworkIdentity parentNetworkIdentity) {
    if (parentNetworkIdentity != null) {
        // Set locally on server
        transform.SetParent (parentNetworkIdentity.transform);
        // Set remotely on clients
        RpcClient_SetParent (parentNetworkIdentity.netId, resetTransform);
    }
    else {
        // Set locally on server
        transform.SetParent (null);
        // Set remotely on clients
        RpcClient_SetParent (NetworkInstanceId.Invalid, resetTransform);
    }
}

[ClientRpc]
void RpcClient_SetParent (NetworkInstanceId newParentNetId) {
    Transform parentTransform = null;
    if (newParentNetId != NetworkInstanceId.Invalid) {
        // Find the parent by netid and set self as child
        var parentGobj = ClientScene.FindLocalObject (newParentNetId);
        if (parentGobj != null) {
            parentTransform = parentGobj.transform;
        }
        else {
            Debug.LogWarningFormat ("{0} Could not find NetworkIdentity '{1}'.", gameObject.name, newParentNetId.Value);
        }
    }
    transform.SetParent (parentTransform);
}

These two are part of a NetworkBehavior, which obviously RequiresComponent(typeof(NetworkIdentity)). If you really need this behavior on a client to server, I'd suggest creating a command that passed the NetworkIdentity to the server, which would just call the server's public method. It's one set of network messages more than optimal, but meh.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!