InstantiateGameObjects
Creates a specified number of instances of a GameObject identified by its instance ID and populates NativeArrays with the EntityIds of the new GameObjects and their Transform components.
Read time 2 minutesLast updated 2 days ago
Definition
- Type: Method
- Namespace: UnityEngine
- Assembly: UnityEngine.CoreModule
InstantiateGameObjects(int, int, NativeArray<int>, NativeArray<int>, Scene)
Creates a specified number of instances of a GameObject identified by its instance ID and populates NativeArrays with the EntityIds of the new GameObjects and their Transform components.
public static void InstantiateGameObjects(int sourceInstanceID, int count, NativeArray<int> newInstanceIDs, NativeArray<int> newTransformInstanceIDs, Scene destinationScene = default)
Parameters
The EntityId of the GameObject to create additional instances of.
The number of instances of the GameObject to create.
Pre-allocated NativeArray to populate with the EntityIds of the new GameObjects. Must be the same size as .
countPre-allocated NativeArray to populate with the EntityIds of the Transforms of the new GameObjects. Must be the same size as .
countThe Scene to place the instantiated GameObjects into. If default, then the GameObjects will be added to the currently active Scene.
Remarks
Use to instantiate multiple GameObjects as a batch. An EntityId can be resolved to an object using Resources.EntityIdToObject.
InstantiateGameObjectsusing System;using Unity.Collections;using UnityEngine;public class InstantiateEntityId : MonoBehaviour{public GameObject prefab;public int count = 100; EntityId m_EntityId; NativeArray<EntityId> m_EntityIds; NativeArray<EntityId> m_TransformIds; void Start() { m_EntityId = prefab.GetEntityId(); m_EntityIds = new NativeArray<EntityId>(count, Allocator.Persistent); m_TransformIds = new NativeArray<EntityId>(count, Allocator.Persistent); GameObject.InstantiateGameObjects(m_EntityId, count, m_EntityIds,m_TransformIds ); } void OnDestroy() { m_EntityIds.Dispose(); m_TransformIds.Dispose(); }}
Additional Resources: GameObject.SetGameObjectsActive, Resources.InstanceIDToObject
InstantiateGameObjects(EntityId, int, NativeArray<EntityId>, NativeArray<EntityId>, Scene)
Creates a specified number of instances of a GameObject identified by its instance ID and populates NativeArrays with the EntityIds of the new GameObjects and their Transform components.
public static void InstantiateGameObjects(EntityId sourceEntityId, int count, NativeArray<EntityId> newEntityIds, NativeArray<EntityId> newTransformEntityIds, Scene destinationScene = default)
Parameters
The number of instances of the GameObject to create.
The Scene to place the instantiated GameObjects into. If default, then the GameObjects will be added to the currently active Scene.
Remarks
Use to instantiate multiple GameObjects as a batch. An EntityId can be resolved to an object using Resources.EntityIdToObject.
InstantiateGameObjectsusing System;using Unity.Collections;using UnityEngine;public class InstantiateEntityId : MonoBehaviour{public GameObject prefab;public int count = 100; EntityId m_EntityId; NativeArray<EntityId> m_EntityIds; NativeArray<EntityId> m_TransformIds; void Start() { m_EntityId = prefab.GetEntityId(); m_EntityIds = new NativeArray<EntityId>(count, Allocator.Persistent); m_TransformIds = new NativeArray<EntityId>(count, Allocator.Persistent); GameObject.InstantiateGameObjects(m_EntityId, count, m_EntityIds,m_TransformIds ); } void OnDestroy() { m_EntityIds.Dispose(); m_TransformIds.Dispose(); }}
Additional Resources: GameObject.SetGameObjectsActive, Resources.InstanceIDToObject