Example #1
0
        private IEnumerator AddInstancesAtRuntime()
        {
            for (int i = 0; i < instanceCount; i++)
            {
                GPUInstancerPrefab prefabInstance = Instantiate(prefab);
                prefabInstance.transform.localPosition = Random.insideUnitSphere * 10;
                prefabInstance.transform.SetParent(parentTransform);
                if (!prefabInstance.prefabPrototype.addRuntimeHandlerScript)
                {
                    GPUInstancerAPI.AddPrefabInstance(prefabManager, prefabInstance);
                }
                instancesList.Add(prefabInstance);
                if (!addRemoveInstantlyToggle.isOn)
                {
                    yield return(new WaitForSeconds(0.001f));
                }
            }

            EnableButton("RemoveInstancesButton");
            if (extraInstancesList.Count == 0)
            {
                EnableButton("AddExtraInstancesButton");
            }
            else
            {
                EnableButton("RemoveExtraInstancesButton");
            }
        }
Example #2
0
        private IEnumerator AddExtraInstancesAtRuntime()
        {
            // Extra instance count that can be added at runtime is limited by the "Extra buffer size" property defined for this prefab prototype in the manager.
            for (int i = 0; i < prefab.prefabPrototype.extraBufferSize; i++)
            {
                GPUInstancerPrefab prefabInstance = Instantiate(prefab);
                prefabInstance.transform.localPosition = Random.insideUnitSphere * 5;

                // Move the extra spheres to the outer sphere
                prefabInstance.transform.localPosition = prefabInstance.transform.localPosition.normalized * (prefabInstance.transform.localPosition.magnitude + 10f);

                prefabInstance.transform.SetParent(parentTransform);
                if (!prefabInstance.prefabPrototype.addRuntimeHandlerScript)
                {
                    GPUInstancerAPI.AddPrefabInstance(prefabManager, prefabInstance);
                }
                extraInstancesList.Add(prefabInstance);
                if (!addRemoveInstantlyToggle.isOn)
                {
                    yield return(new WaitForSeconds(0.001f));
                }
            }

            EnableButton("RemoveExtraInstancesButton");
            if (instancesList.Count == 0)
            {
                EnableButton("AddInstancesButton");
            }
            else
            {
                EnableButton("RemoveInstancesButton");
            }
        }