public static void RegisterPrefab (GameObject prefab);
public static void RegisterPrefab (GameObject prefab, Networking.SpawnDelegate spawnHandler, Networking.UnSpawnDelegate unspawnHandler);
public static void RegisterPrefab (GameObject prefab, Networking.NetworkHash128 newAssetId);

パラメーター

prefab生成を行うオブジェクトのプレハブ
spawnHandlerクライアント上で使用する spawnhandler メソッド
unspawnHandlerクライアント上で使用する un-spawnhandler メソッド
newAssetIdAn assetId to be assigned to this prefab. This allows a dynamically created game object to be registered for an already known asset Id.

説明

UNET のオブジェクト生成システムにプレハブを登録します。

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 override void OnStartClient() { ClientScene.RegisterPrefab(plantPrefab); }

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

引数の spawnHandler と unspawnHandler の機能は、オブジェクトプールのように、高度なオブジェクト生成機能を実装するときに便利です。