Runtime Prefab Spawner

Runtime Prefab Spawner

The Runtime Prefab Spawner component is designed to instantiate prefabs related to the trees or objects in Vegetation Studio. It allows you to assign a prefab to a Vegetation Item. When the item is within range of the camera an instance of the prefab is created at the excact location of the item. This item will be removed automatic when item is out of range again.

The use case of this could be to add effects like falling leaves, insects at flowers,  sounds from the old tree, harvesting logic etc. The prefabs can have any scripts attached.

This approach allows you to add special functionality to large amounts of vegetation items with no extra overhead of culling and processing the effect gameobjects in the hierarchy.

You add it to the GameObject with the VegetationSystem component and configure. Multiple components is possible.

Settings
Editor
Debug Info
API

 

In this example video we added a prefab with a falling leaves particle effect to one of the tree models. As any tree of this type gets within range it will instantiate a copy of the effect with the same position as the tree. When it is out of range it is removed automatic. This will allow for effects on huge amounts of trees or plants with no overhead of culling and gameobject hierarchy handling.

Image showing the falling leaves.

Settings

Select vegetation item

Select any of the available Vegetation Items from the list. It is possible to add runtime prefabs to any Plant, Tree, Object or Large Object.

Runtime prefab

Assign any prefab you want to instantiate. They will be created with the same position, rotation and scale as the VegetationItem

Spawn frequency

Spawn frequency is the chance of a Vegetation Item having the effect. A value of 1 will create a object for every Vegetation Item.

Scale multiplier

Scale of the instanced object will be multiplied with this scale. Usefull to set particle system size etc.

Rotation

Instanced object will be rotated with this value.

Offset

Offset is a local space position offset added to the instanced prefab at run-time.

Range

This settings sets the range/distance from camera where you will get the objects created. It set as a factor of the set vegetation distance.

Editor

Hide instanced prefabs in hierarchy

When enabled the instanced objects are hidden in the scene hierarchy.

Debug Info

Show included cells

Enable “Show included cells” to visualize the cells used for selecting what Vegetation Items we will spawn prefabs for.

API

There are 2 events you can connect to to get informed when a run-time prefab is created. Use them to get a reference and process the instanced objects with your gamelogic


public delegate void MultiCreateRuntimePrefabDelegate(GameObject go);
public MultiCreateRuntimePrefabDelegate OnCreateRuntimePrefabDelegate;

public delegate void MultiBeforeDestroyRuntimePrefabDelegate(GameObject go);
public MultiBeforeDestroyRuntimePrefabDelegate OnBeforeDestroyRuntimePrefabDelegate;

To connect to the events.

void Start()
{
runtimePrefabSpawner.OnCreateRuntimePrefabDelegate+= OnCreateRuntimePrefab;
runtimePrefabSpawner.OnBeforeDestroyRuntimePrefabDelegate+= OnBeforeDestroyRuntimePrefab;
}

void OnCreateRuntimePrefab(GameObject go)
{
//Your code here.
}

void OnBeforeDestroyRuntimePrefab(GameObject go)
{
//Your code here.
}