Udon Program Source "Hello World"

IMPORTANT NOTE: If you’re just getting started with Udon, click away now. This is purely just for those who want to create Program Source and need a small reference.

Last Updated (MM/DD/YY) : 16/12/19

Introduction

In this small guide we will be creating and Udon Program Source for my BRAND NEW LANGUAGE “HelloWorld Script”. You get 0 control of anything and the only options you have is to print “Hello World” to the console or to leave. Included below the guide is the complete HelloWorld example AND some editor extensions for easily creating ScriptAssets.

Overview

To create a basic Program Source we need a few scripts (though, this guide will go over the top a bit)
You need to create a ProgramAsset and a ScriptImporter. The job of the ProgramAsset is to actually hold any information (like the script’s code), the generated assembly, and to generate the assembly to be assembled.

Program Asset

When creating the ProgramAsset it is easiest to just inherit from UdonAssemblyProgramAsset and to just feed it the uassembly code. You can use the [assembly: UdonProgramSourceMenu(typeof(UdonProgramAsset), “NAME”] to have it show up on the UdonBehaviour create program list. PLEASE NOTE: From what I can tell RefreshProgram() is not called anywhere, you will need to create the ScriptImporter to refresh your program or have a way to call RefreshProgram() on your ProgramAssets. Override DoRefreshProgramActions() to put your generated assembly in the correct place and call AssembleProgram(), DoRefreshProgramActions() is called from RefreshProgram() and has some sanity checks.

Scripted Importer

Note: ScriptedImporters are an experimental addition to the UnityEditor, so things might change or be out-flat removed in newer Unity versions. To establish your script importer you will need to inherit from ScriptedImporter found from UnityEditor.Experimental.AssetImporters, you will also need to give your class the [ScriptedImporter(int version, string extension)] attribute. You override the OnImportAsset(AssetImportContext ctx) to import the text from the file into a ScriptableObject. You will also need to call RefreshProgram() here. You can find the UdonHelloWorldScriptImporter for a reference on overriding DoRefreshProgramActions().

Scripted Importer Editor

Included with the HelloWorld example is a ScriptedImporterEditor example. It includes a way to render a box just like you see with C# scripts in Unity. Feel free to use it if you would like this functionality. A good way might to call RenderScriptView(string text) twice, once for the code from the script and another to show the generated assembly from the script for debugging.

Editor Extensions

The second package listed below includes several helper functions for creating a script asset from a template file. It is possible to do this in Unity 2019, but in Unity 2018 the functionality has not been exposed, so feel free to use the package. It is also included in the HelloWorld example.

// NOTE : To create a HelloWorld script, right click>Create>HelloWorld Script (Under C# Script)

Changelog:

(DD,MM,YY)

16/12/19:
-Added UdonProgramSourceNewMenu attribute to show up on UdonBehaviour’s New Program Menu.
-Now overrides DoRefreshProgramActions() as RefreshProgram() is now sealed with sanity checks inside.

Files

HelloWorld Example
ScriptAsset Editor Additions

4 Likes