Skip to content
On this page

Getting started - Scripting

This guide shows how to access the Simplygon C# API from a Unity C# script. The entry point in the script will initialize Simplygon and call a DoWork-function for each selected GameObject. The entry point will be exposed as a menu entry in Unity for easy access.

Please make sure that the Simplygon Unity plug-in as well as Unity USD is installed before proceeding, if not see installation instructions.

The first step is to create a C# script that we can extend for our own purposes, and since Simplygon is an Editor plugin only, any script that uses Simplygon needs to sit in a folder called Editor or be part of an assembly definition that only targets the Editor platform. To do that, right-click in the Unity Asset Manager and select Create -> Folder, name it Editor. Go into the Editor folder and right-click in the Unity Asset Manager again and select Create -> C# Script. Let's name the file GettingStarted.cs.

Asset manager

Double-click the script to open it in an editor. We can now see that the script has a basic template, for this specific example we are not interested in that, so let's delete everything.

We'll add a new structure, a class named GettingStartedWithScripting.

csharp
using UnityEngine;

public class GettingStartedWithScripting
{

}

Now we will add the entry point of the script, let's name it EntryPoint. There is an attribute in Unity named MenuItem, which purpose is to expose the given method in the Unity menu bar. Above the EntryPoint function, add the attribute as below. In this example we'll add a Simplygon menu and the menu item will be called GettingStartedScript.

csharp
using UnityEditor;
using UnityEngine;

public class GettingStartedWithScripting
{
    [MenuItem("Simplygon/GettingStartedScript")]
    static void EntryPoint()
    {

    }
}

If we save the script the new Simplygon menu should pop up inside Unity, The entry point for the script should be listed as a menu item.

Simplygon menu

We are now ready to initialize Simplygon, but only if there is a selection in the scene. Let's add a check for selected objects, then use the global Simplygon loader with appropriate parameters to initialize Simplygon. We can also check the error code and output an error to the Unity console if the initialization goes wrong.

csharp
using Simplygon;
using UnityEditor;
using UnityEngine;

public class GettingStartedWithScripting
{
    [MenuItem("Simplygon/GettingStartedScript")]
    static void EntryPoint()
    {
        if (Selection.objects.Length > 0)
        {
            using (ISimplygon simplygon = global::Simplygon.Loader.InitSimplygon
            (out EErrorCodes simplygonErrorCode, out string simplygonErrorMessage))
            {
                if (simplygonErrorCode == Simplygon.EErrorCodes.NoError)
                {
                    // do things with Simplygon and selection here...
                }
                else
                {
                    Debug.LogError("Simplygon initializing failed!");
                }
            }
        }
    }
}

That is how you set up Simplygon inside a Unity C# script. Below is the final script, including comments and function definition ready to be expanded. Please proceed to the reduction example for details on how to setup a ReductionPipeline.

csharp
using Simplygon;
using UnityEditor;
using UnityEngine;

// create a new class that inherits from MonoBehaviour
public class GettingStartedWithScripting
{
    // attribute for exposing this function as menu item
    [MenuItem("Simplygon/GettingStartedScript")]
    static void EntryPoint()
    {
        // are there any objects selected in the scene?
        if (Selection.objects.Length > 0)
        {
            // if so, initialize Simplygon
            using (ISimplygon simplygon = global::Simplygon.Loader.InitSimplygon
            (out EErrorCodes simplygonErrorCode, out string simplygonErrorMessage))
            {
                // if Simplygon handle is valid, loop all selected objects
                // and call dummy function.
                if (simplygonErrorCode == Simplygon.EErrorCodes.NoError)
                {
                    foreach (var o in Selection.objects)
                    {
                        DoThings(o as GameObject, simplygon);
                    }
                }

                // if invalid handle, output error message to the Unity console
                else
                {
                    Debug.LogError("Simplygon initializing failed!");
                }
            }
        }
    }

    public static void DoThings(GameObject gameObject, ISimplygon simplygon)
    {
        // Do things with gameObject and Simplygon
    }
}