Difference between revisions of "GPU Instancer:Features"

From GurBu Wiki
Jump to: navigation, search
(Prefab Instancing Features)
(Automatically Add-Remove prefab instances without any aditional code)
Line 6: Line 6:
  
 
=== Automatically Add-Remove prefab instances without any aditional code ===
 
=== Automatically Add-Remove prefab instances without any aditional code ===
 +
 +
1. Easiest way is to enable Auto. Add/Remove Instances option in the prototype options. When enabled, the Prefab Manager will add a script called "GPUInstancerPrefabRuntimeHandler" to the prefab object and handle all runtime changes automatically.
 +
 +
https://www.gurbu.com/assets/images/tutorials/addremoveruntime/autoaddremoveinstances.png
 +
 +
2. Optionally you can add prefab instances through our API methods with the AddPrefabInstance and RemovePrefabInstance methods. Keep in mind that Add/Remove Instances At Runtime option should be enabled in the prototype definition for this API methods to work. Also Extra Buffer Size should have high enough number to add all the prefab instances generated at runtime.
 +
 +
https://www.gurbu.com/assets/images/tutorials/addremoveruntime/addremoveinstances.png
 +
 +
3. Another option is to register the prefab instances and reinitialize the Prefab Manager with first calling RegisterPrefabInstanceList method and then calling InitializeGPUInstancer method.
 +
<br>
 +
<div class="code" style="border: 1px solid #c8ccd1;padding:5px;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Collections.Generic</span><span style="color: #008000;">;</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">UnityEngine</span><span style="color: #008000;">;</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #0600FF; font-weight: bold;">namespace</span> GPUInstancer<span style="color: #008000;">.</span><span style="color: #0000FF;">Extras</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #008000;">{</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> GPUIRegisterPrefabInstances <span style="color: #008000;">:</span> MonoBehaviour</div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #008000;">{</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Keeping a static reference to the manager so it can be shared between scenes.</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> GPUInstancerPrefabManager prefabManager<span style="color: #008000;">;</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// The list of prefab instances in scene</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> List<span style="color: #008000;">&lt;</span>GPUInstancerPrefab<span style="color: #008000;">&gt;</span> prefabInstanceList<span style="color: #008000;">;</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// In this example we fill the list in the Reset method which works when adding the component for the first time (or using the reset command in the inspector).</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// You can fill the list in any way you want.</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Reset<span style="color: #008000;">(</span><span style="color: #008000;">)</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">{</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Add prefabs in the scene to the list</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prefabInstanceList <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span>GPUInstancerPrefab<span style="color: #008000;">&gt;</span><span style="color: #008000;">(</span>FindObjectsOfType<span style="color: #008000;">&lt;</span>GPUInstancerPrefab<span style="color: #008000;">&gt;</span><span style="color: #008000;">(</span><span style="color: #008000;">)</span><span style="color: #008000;">)</span><span style="color: #008000;">;</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">}</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">void</span> Start<span style="color: #008000;">(</span><span style="color: #008000;">)</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">{</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Find the prefab manager and set it to the static property</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">(</span>prefabManager <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">&amp;&amp;</span> GPUInstancerAPI<span style="color: #008000;">.</span><span style="color: #0000FF;">GetActiveManagers</span><span style="color: #008000;">(</span><span style="color: #008000;">)</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">)</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">{</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GPUInstancerManager manager <span style="color: #008000;">=</span> GPUInstancerAPI<span style="color: #008000;">.</span><span style="color: #0000FF;">GetActiveManagers</span><span style="color: #008000;">(</span><span style="color: #008000;">)</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Find</span><span style="color: #008000;">(</span>m <span style="color: #008000;">=&gt;</span> m <span style="color: #008000;">is</span> GPUInstancerPrefabManager<span style="color: #008000;">)</span><span style="color: #008000;">;</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prefabManager <span style="color: #008000;">=</span> <span style="color: #008000;">(</span>GPUInstancerPrefabManager<span style="color: #008000;">)</span>manager<span style="color: #008000;">;</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">}</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Register the prefab instance list to the manager</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">(</span>prefabManager <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">&amp;&amp;</span> prefabManager<span style="color: #008000;">.</span><span style="color: #0000FF;">isActiveAndEnabled</span><span style="color: #008000;">)</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">{</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GPUInstancerAPI<span style="color: #008000;">.</span><span style="color: #0000FF;">RegisterPrefabInstanceList</span><span style="color: #008000;">(</span>prefabManager, prefabInstanceList<span style="color: #008000;">)</span><span style="color: #008000;">;</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GPUInstancerAPI<span style="color: #008000;">.</span><span style="color: #0000FF;">InitializeGPUInstancer</span><span style="color: #008000;">(</span>prefabManager<span style="color: #008000;">)</span><span style="color: #008000;">;</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">}</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">}</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #008000;">}</span></div>
 +
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #008000;">}</span></div>
 +
</div>
  
 
=== Nested Prefabs support (Unity 2018.3 and later) ===
 
=== Nested Prefabs support (Unity 2018.3 and later) ===

Revision as of 13:39, 5 December 2018

About | Features | Getting Started | Terminology | Best Practices | F.A.Q.

General Features

Prefab Instancing Features

Automatically Add-Remove prefab instances without any aditional code

1. Easiest way is to enable Auto. Add/Remove Instances option in the prototype options. When enabled, the Prefab Manager will add a script called "GPUInstancerPrefabRuntimeHandler" to the prefab object and handle all runtime changes automatically.

autoaddremoveinstances.png

2. Optionally you can add prefab instances through our API methods with the AddPrefabInstance and RemovePrefabInstance methods. Keep in mind that Add/Remove Instances At Runtime option should be enabled in the prototype definition for this API methods to work. Also Extra Buffer Size should have high enough number to add all the prefab instances generated at runtime.

addremoveinstances.png

3. Another option is to register the prefab instances and reinitialize the Prefab Manager with first calling RegisterPrefabInstanceList method and then calling InitializeGPUInstancer method.

using System.Collections.Generic;
using UnityEngine;
 
namespace GPUInstancer.Extras
{
    public class GPUIRegisterPrefabInstances : MonoBehaviour
    {
        // Keeping a static reference to the manager so it can be shared between scenes.
        public static GPUInstancerPrefabManager prefabManager;
        // The list of prefab instances in scene
        public List<GPUInstancerPrefab> prefabInstanceList;
 
 
        // In this example we fill the list in the Reset method which works when adding the component for the first time (or using the reset command in the inspector).
        // You can fill the list in any way you want.
        public void Reset()
        {
            // Add prefabs in the scene to the list
            prefabInstanceList = new List<GPUInstancerPrefab>(FindObjectsOfType<GPUInstancerPrefab>());
        }
 
        void Start()
        {
            // Find the prefab manager and set it to the static property
            if(prefabManager == null && GPUInstancerAPI.GetActiveManagers() != null)
            {
                GPUInstancerManager manager = GPUInstancerAPI.GetActiveManagers().Find(m => m is GPUInstancerPrefabManager);
                prefabManager = (GPUInstancerPrefabManager)manager;
            }
 
            // Register the prefab instance list to the manager
            if (prefabManager != null && prefabManager.isActiveAndEnabled)
            {
                GPUInstancerAPI.RegisterPrefabInstanceList(prefabManager, prefabInstanceList);
                GPUInstancerAPI.InitializeGPUInstancer(prefabManager);
            }
        }
    }
}

Nested Prefabs support (Unity 2018.3 and later)

Detail Instancing Features

Tree Instancing Features

Third Party Integrations