Skip to content
On this page

Add SimplygonSelectionSet component to GameObject - Scripting

Several Simplygon features require some form of selection set to function properly, for example for selective processing, assignment of clipping geometries, cameras /visibility volumes for visibility culling, bone locks etc. As Unity does not have any official support for selection sets we've added an extension in the form of a SimplygonSelectionSet component.

The SimplygonSelectionSet component can be added to most scene nodes, such as transformations, meshes, bones and cameras. Upon export (to Simplygon) these components will be identified and related nodes placed in selection sets accordingly. A set has two properties, the first is the name of the selection set the node should be part of, the second is include children, which when specified will add all children of the node to the set.

To add a selection set component to a GameObject, simply call AddComponent for the desired GameObjects. AddComponent will return a reference to the new component, which can be used to modify the properties of the selection set, for example the Name of the set and the Include Children option.

The example script below will register a menu item (Simplygon -> AddCamerasToSelectionSet) so that we easily can access the script from Unity. The script will loop through all cameras in the scene and check if there already is a SimplygonSelectionSet component with the target set name 'CameraSet', if not then we simply add a new component to the camera and assign the target name.

csharp
using Simplygon.Unity.EditorPlugin;
using UnityEditor;
using UnityEngine;

public class AddSelectionSetComponentToCameras : MonoBehaviour
{
    [MenuItem("Simplygon/AddCamerasToSelectionSet")]
    static void EntryPoint()
    {
        // the name of the selection set to place the Cameras in
        const string targetSetName = "CameraSet";

        // find all cameras in scene
        Camera[] cameras = FindObjectsOfType<Camera>();

        // for each camera, see if there is an existing SimplygonSelectionSet component with the same properties,
        // if not, add new SimplygonSelectionSet component.
        foreach (Camera camera in cameras)
        {
            Debug.Log("Found camera: " + camera.name);
            bool hasIdenticalSetName = false;

            // see if there already is a SimplygonSelectionSet component with the same selection set name
            Component[] existingComponents = camera.gameObject.GetComponents(typeof(SimplygonSelectionSet));
            foreach (Component component in existingComponents)
            {
                SimplygonSelectionSet existingSelectionSetComponent = (component as SimplygonSelectionSet);
                if (existingSelectionSetComponent == null)
                    continue;

                hasIdenticalSetName = existingSelectionSetComponent.SelectionSetName == targetSetName;
                if (hasIdenticalSetName)
                    break;
            }

            // if there is no identical SimplygonSelectionSet component, 
            // add new SimplygonSelectionSet component and assign target selection set name.
            if (!hasIdenticalSetName)
            {
                Debug.Log("Adding selection set component (name: " + targetSetName + ")  to " + camera.name);
                SimplygonSelectionSet selectionSetComponent = camera.gameObject.AddComponent<SimplygonSelectionSet>();
                selectionSetComponent.SelectionSetName = targetSetName;
            }
            else
            {
                Debug.Log("Selection set component (name: " + targetSetName + ")  already exists in " + camera.name);
            }
        }
    }
}

Let's give this script a try. Create a new Unity project that only contains the Main Camera, feel free to add additional cameras. Now execute the script by clicking Simplygon -> AddCamerasToSelectionSet.

Run script
Run script

If everything goes as expected you should now see at least two lines in the output console stating the name of the camera(s) found and that a selection set component was added to each one of them. You can also verify that the SimplygonSelectionSetComponent exist on all cameras in the scene, the 'Selection Set Name' should be set to 'CameraSet'.

Result
Result

That is it! You can now use the specified selection set name 'CameraSet' for visibility culling. The SimplygonSelectionSet component can be applied to various nodes for different purposes, cameras or meshes for visibility culling, meshes for geometry clipping and so on. Please see the documentation for the specific Processor or Pipeline for more information.