Legacy Documentation: Version 5.2
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

ClientScene.RegisterPrefab

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Sumbission failed

For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Switch to Manual
public static function RegisterPrefab(prefab: GameObject): void;
public static void RegisterPrefab(GameObject prefab);
public static function RegisterPrefab(prefab: GameObject, spawnHandler: Networking.SpawnDelegate, unspawnHandler: Networking.UnSpawnDelegate): void;
public static void RegisterPrefab(GameObject prefab, Networking.SpawnDelegate spawnHandler, Networking.UnSpawnDelegate unspawnHandler);

Parameters

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.

Description

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.

Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961