System
The global system that stores and manages all Flora instances.
The FloraSystem
is a singleton that manages the data and rendering of all Flora instances globally.
It can be used to create, modify, query, and destroy instances at runtime.
You can access it through the static Instance
property, but this may return null
if no instances exist yet.
To guarantee access, use the static method:
FloraSystem.GetOrCreate()
Instance Management
Create or destroy a single instance
FloraSystem.GetOrCreate().CreateInstance(prefab, parent, pos, rot, scale);
FloraSystem.Instance?.DestroyInstance(handle);
Create or destroy a batch of instances
FloraSystem.GetOrCreate().CreateInstances(prefab, parent, handlesArray, transformsArray);
FloraSystem.Instance?.DestroyInstances(handlesArray);
Control instance visibility
FloraSystem.Instance.SetInstanceEnabled(instanceHandle, false);
FloraSystem.Instance.SetInstancesEnabled(instanceHandleArray, false);
Query instances by bounds or sphere
FloraSystem.Instance.FindInstancesInSphereMatching(myInstanceFilter, mySphere, Allocator.Temp);
FloraSystem.Instance.FindInstancesInBoundsMatching(myInstanceFilter, mySphere, Allocator.Temp);
Example
// 1. Ensure system is running
var flora = FloraSystem.GetOrCreate();
// 2. Spawn 100 instances of a grass prefab procedurally
var handles = new NativeArray<FloraInstanceHandle>(100, Allocator.Temp);
var transforms = new NativeArray<FloraInstanceTransform>(100, Allocator.Temp);
for (int i = 0; i < 100; i++)
transforms[i] = new FloraInstanceTransform(RandomPosition(), Quaternion.identity, Vector3.one);
flora.CreateInstances(grassPrefab, parentTransform, handles, transforms);
// 3. Query all grass within a 10m radius of the player
var sphere = new BoundingSphere(playerPos, 10f);
var grassFilter = FloraInstanceFilter.ByPrefab(grassPrefab);
var nearby = flora.FindInstancesInSphereMatching(grassFilter, sphere, Allocator.Temp);
// 4. Disable nearby grass
foreach (var h in nearby)
flora.SetInstanceEnabled(h, false);
// 5. Cleanup
handles.Dispose();
transforms.Dispose();
nearby.Dispose();
Last updated