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.

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
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
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.

Below is the final script where you see how to initialize Simplygon inside a Unity C# script. Please proceed to the reduction example for details on how to setup a ReductionPipeline.

csharp
using Simplygon;
using UnityEditor;
using UnityEngine;

public class GettingStartedWithScripting
{
    [MenuItem("Simplygon/GettingStartedScript")]
    static void EntryPoint()
    {
        if (Selection.gameObjects.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!");
                }
            }
        }
    }
}