private GameObject SpawnPedestrian() { GameObject ped = Instantiate(pedPrefab, Vector3.zero, Quaternion.identity, transform); //Ensure sure all pedestrians get unique names HierarchyUtility.ChangeToUniqueName(ped); var pedController = ped.GetComponent <PedestrianController>(); pedController.SetGroundTruthBox(); var modelIndex = RandomGenerator.Next(pedModels.Count); var model = pedModels[modelIndex]; Instantiate(model, ped.transform); ped.SetActive(false); SimulatorManager.Instance.UpdateSemanticTags(ped); currentPedPool.Add(pedController); //Add required components for distributing rigidbody from master to clients if (SimulatorManager.Instance.Network.IsMaster) { if (ped.GetComponent <DistributedObject>() == null) { ped.AddComponent <DistributedObject>(); } if (ped.GetComponent <DistributedRigidbody>() == null) { ped.AddComponent <DistributedRigidbody>(); } BroadcastMessage(new Message(Key, GetSpawnMessage(ped.name, modelIndex, ped.transform.position, ped.transform.rotation), MessageType.ReliableUnordered)); } return(ped); }
/// <summary> /// Instantiates selected prefab in the unique relative path /// </summary> /// <param name="prefabId">Id of instantiated prefab in <see cref="NetworkSettings.DistributedObjectPrefabs"/></param> /// <param name="relativePath">Relative path to the root where object will be created, can be changed if object name is not unique</param> /// <returns>Instantiated new distributed object</returns> /// <exception cref="ArgumentException">Invalid configuration of the instantiated prefab</exception> private DistributedObject InstantiatePrefab(int prefabId, string relativePath) { if (prefabId < 0 || prefabId >= Settings.DistributedObjectPrefabs.Length) { throw new ArgumentException( $"Prefab of distributed object with id {prefabId} is not defined in {typeof(NetworkSettings).Name}."); } var distributedObjectParent = HierarchyUtility.GetOrCreateChild(transform, relativePath); var newGameObject = Instantiate(Settings.DistributedObjectPrefabs[prefabId], distributedObjectParent); HierarchyUtility.ChangeToUniqueName(newGameObject); var distributedObject = newGameObject.GetComponent <DistributedObject>(); if (distributedObject == null) { throw new ArgumentException( $"Prefab of distributed object with id {prefabId} has no {typeof(DistributedObject).Name} component in the root game object."); } instantiatedObjects.Add(new InstantiatedObjectData(prefabId, distributedObject)); return(distributedObject); }