Version: 5.3 (switch to 5.4b)
IdiomaEnglish
  • C#
  • JS

Idioma de script

Selecciona tu lenguaje de programación favorito. Todos los fragmentos de código serán mostrados en este lenguaje.

ClientScene.RegisterPrefab

Sugiere un cambio

¡Éxito!

Gracias por ayudarnos a mejorar la calidad de la documentación de Unity. A pesar de que no podemos aceptar todas las sugerencias, leemos cada cambio propuesto por nuestros usuarios y actualizaremos los que sean aplicables.

Cerrar

No se puedo enviar

Por alguna razón su cambio sugerido no pudo ser enviado. Por favor <a>intente nuevamente</a> en unos minutos. Gracias por tomarse un tiempo para ayudarnos a mejorar la calidad de la documentación de Unity.

Cerrar

Cancelar

Cambiar al 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);
public static function RegisterPrefab(prefab: GameObject, newAssetId: Networking.NetworkHash128): void;
public static void RegisterPrefab(GameObject prefab, Networking.NetworkHash128 newAssetId);

Parámetros

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.

Descripción

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 instantiate a corresponding client object with the same netId.

The NetworkManager has a list of spawnable prefabs, it uses this function to register those prefabs with the ClientScene.

The set of current spawnable object is available in the ClientScene static member variable ClientScene.prefabs, which is a dictionary of NetworkAssetIds and prefab references.


        
using UnityEngine;
using UnityEngine.Networking;

public class PlantSpawner : NetworkBehaviour {

public GameObject plantPrefab;

public void OnStartClient() { ClientScene.RegisterPrefab(plantPrefab); }

[Server] public ServerSpawnPlant(Vector3 pos, Quaternion rot) { var plant = (GameObject)Instantiate(plantPrefab, pos, rot); NetworkServer.Spawn(plant); } }

The optional custom spawn and un-spawn handler functions can be used to implement more advanced spawning strategies such as pbject pools.