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.

Network.AllocateViewID

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 AllocateViewID(): NetworkViewID;
public static NetworkViewID AllocateViewID();

Descripción

Query for the next available network view ID number and allocate it (reserve).

This number can then be assigned to the network view of an instantiated object. The example below demonstrates a simple method to do this. Note that for this to work there must be a NetworkView attached to the object which has this script and it must have the script as its observed property. There must be a Cube prefab present also with a NetworkView which watches something (like the Transform of the Cube). The cubePrefab variable in the script must be set to that cube prefab. This is the simplest method of using AllocateViewID intelligently. This get more complicated if there were more than one NetworkView attached to the Cube which is to be instantiated.

var cubePrefab : Transform;

var nView: NetworkView;

function Start() { nView = GetComponent.<NetworkView>(); }

function OnGUI() { if (GUILayout.Button("SpawnBox")) { var viewID = Network.AllocateViewID(); nView.RPC("SpawnBox", RPCMode.AllBuffered, viewID, transform.position); } }

@RPC function SpawnBox (viewID : NetworkViewID, location : Vector3) { // Instantate the prefab locally var clone : Transform; clone = Instantiate(cubePrefab, location, Quaternion.identity) as Transform; var nView : NetworkView; nView = clone.GetComponent.<NetworkView>(); nView.viewID = viewID; }
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { public Transform cubePrefab; public NetworkView nView; void Start() { nView = GetComponent<NetworkView>(); } void OnGUI() { if (GUILayout.Button("SpawnBox")) { NetworkViewID viewID = Network.AllocateViewID(); nView.RPC("SpawnBox", RPCMode.AllBuffered, viewID, transform.position); } } [RPC] void SpawnBox(NetworkViewID viewID, Vector3 location) { Transform clone; clone = Instantiate(cubePrefab, location, Quaternion.identity) as Transform as Transform; NetworkView nView; nView = clone.GetComponent<NetworkView>(); nView.viewID = viewID; } }