prefab | A Prefab that will be spawned. |
spawnHandler | A method to use as a custom spawnhandler on clients. |
unspawnHandler | A method to use as a custom un-spawnhandler on clients. |
newAssetId | An assetId to be assigned to this prefab. This allows a dynamically created game object to be registered for an already known asset Id. |
Registers a prefab with the UNET spawning system.
When a NetworkIdentity object is spawned on a server with NetworkServer.SpawnObject(), and the prefab that the object was created from was registered with RegisterPrefab(), the client will use that prefab to instanciate a corresponding client object with the same netId.
no example available in JavaScript
using UnityEngine; using UnityEngine.Networking;
public class Manager : MonoBehaviour {
bool isAtStartup = true; NetworkClient myClient; NetworkServer myServer; public GameObject playerPrefab; static Manager singleton; void Awake() { singleton = this; } void StartServer() { myServer = NetworkServer.Instance; myServer.Listen(4444); myServer.RegisterHandler(MsgType.SYSTEM_READY, OnPlayerReadyMessage); } void Update () { if (Input.GetKeyDown(KeyCode.S)) { StartServer(); isAtStartup = false; } if (Input.GetKeyDown(KeyCode.C)) { myClient = new NetworkClient(); myClient.Connect("127.0.0.1", 4444); myClient.RegisterHandler(MsgType.SYSTEM_CONNECT, OnConnected); myClient.RegisterPrefab(playerPrefab);
isAtStartup = false; } if (myServer != null) { myServer.Update (); } if (myClient != null) { myClient.Update (); } } void OnGUI() { if (isAtStartup) { GUI.Label(new Rect(2, 10, 150, 100), "Press S for server"); GUI.Label(new Rect(2, 50, 150, 100), "Press C for client"); } } // client function public void OnConnected(NetworkConnection conn, NetworkReader reader) { myClient.Ready(); } // server function public void OnPlayerReadyMessage(NetworkConnection conn, NetworkReader reader) { GameObject thePlayer = (GameObject)Instantiate(singleton.playerPrefab, Vector3.zero, Quaternion.identity); myServer.PlayerIsReady(conn, thePlayer); } }
The optional custom spawn and un-spawn handler functions can be used to implement more advanced spawning strategies such as pbject pools.