unity unet tutorial online example español create unity3d unity-networking

unity3d - tutorial - ¿Cómo usar prefabricados de múltiples jugadores para UNET nuevo UNET?



unity online (2)

Cree una nueva clase que se derive del script integrado de NetworkManager. Allí, solo agregue algunos campos de soporte y una anulación para OnServerAddPlayer ().

[SerializeField] Vector3 playerSpawnPos; [SerializeField] GameObject character1; [SerializeField] GameObject character2; // etc. GameObject chosenCharacter; // character1, character2, etc. // Instantiate whichever character the player chose and was assigned to chosenCharacter public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId) { var player = (GameObject)GameObject.Instantiate(chosenCharacter, playerSpawnPos, Quaternion.identity); NetworkServer.AddPlayer(conn, player, playerControllerId); }

Referencia: http://docs.unity3d.com/Manual/UNetManager.html

¿Alguien ha conseguido que varios prefabricados de jugadores funcionen (por ejemplo, diferentes clases de personajes con diferentes prefabricados) en el nuevo Unity UNET Networking?


¡Finalmente lo conseguí trabajando! Muchas gracias a @ClausKleber por su respuesta en http://forum.unity3d.com/threads/how-to-set-individual-playerprefab-form-client-in-the-networkmanger.348337/#post-2256378

Versión de trabajo editada a continuación, Funciona con el HUD de Network Manager para crear y unirse.

using UnityEngine; using System.Collections; using UnityEngine.Networking; using UnityEngine.UI; public class NetManagerCustom : NetworkManager { // in the Network Manager component, you must put your player prefabs // in the Spawn Info -> Registered Spawnable Prefabs section public short playerPrefabIndex; public override void OnStartServer() { NetworkServer.RegisterHandler(MsgTypes.PlayerPrefab, OnResponsePrefab); base.OnStartServer(); } public override void OnClientConnect(NetworkConnection conn) { client.RegisterHandler(MsgTypes.PlayerPrefab, OnRequestPrefab); base.OnClientConnect(conn); } private void OnRequestPrefab(NetworkMessage netMsg) { MsgTypes.PlayerPrefabMsg msg = new MsgTypes.PlayerPrefabMsg(); msg.controllerID = netMsg.ReadMessage<MsgTypes.PlayerPrefabMsg>().controllerID; msg.prefabIndex = playerPrefabIndex; client.Send(MsgTypes.PlayerPrefab, msg); } private void OnResponsePrefab(NetworkMessage netMsg) { MsgTypes.PlayerPrefabMsg msg = netMsg.ReadMessage<MsgTypes.PlayerPrefabMsg>(); playerPrefab = spawnPrefabs[msg.prefabIndex]; base.OnServerAddPlayer(netMsg.conn, msg.controllerID); Debug.Log(playerPrefab.name + " spawned!"); } public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId) { MsgTypes.PlayerPrefabMsg msg = new MsgTypes.PlayerPrefabMsg(); msg.controllerID = playerControllerId; NetworkServer.SendToClient(conn.connectionId, MsgTypes.PlayerPrefab, msg); } // I have put a toggle UI on gameObjects called PC1 and PC2 to select two different character types. // on toggle, this function is called, which updates the playerPrefabIndex // The index will be the number from the registered spawnable prefabs that // you want for your player public void UpdatePC () { if (GameObject.Find("PC1").GetComponent<Toggle>().isOn) { playerPrefabIndex = 3; } else if (GameObject.Find("PC2").GetComponent<Toggle>().isOn) { playerPrefabIndex= 4; } } }