Skip to main content

Code Blocks

...and in the render scope

As templates are executed it can be helpful to inject C# logic in some places, directly in the template. This can be easily achieved using a code block in a template.

There are three types of code block

  • run which is executed every frame
  • create which is executed in the frame the containing scope was created
  • enable which is executed in the frame the containing scope was enabled

All blocks have the same syntax

template BlockSample render {

run Debug.Log("As a statement without a block");
run {
int x = 4;
x *= 10;
Debug.Log("with a block multiple statements will be executed");
}

create Debug.Log("Runs when the scope gets created");
create {
Debug.Log("Runs when the scope gets created");
Debug.Log("Runs when the scope gets created");
}

enable Debug.Log("Runs when the scope gets enabled");
enable {
Debug.Log("Runs when the scope gets enabled");
Debug.Log("Runs when the scope gets enabled");
}

}

There are no disable or destroy blocks because destruction is handled asynchronously.

Outside of the render scope

There are no code blocks outside of the render scope. Instead you'll be able to define life cycle and other local methods. Be sure not to confuse the enable and create code block with the similarly named life cycle methods. Read more about those in the life cycle documentation

template BlockSample {

// parameters, attributes, styles and life cycle methods go here

before:enable => Debug.Log("this runs before the enable code block runs");

render {
enable {
// ...
}
}
}