Tutorial
In this tutorial, we create a simple script that only outputs logs and run it from a component in the scene. Complete Installation beforehand.
Creating a script
Create hello.rs under Assets with the following content.
hello.rs
use wukuro::entry;
use wukuro::functions::*;
use wukuro::types::Context;
#[entry]
fn hello(cx: &Context, name: String, count: i32) {
for i in 0..count {
log(&format!("Hello, {name}! ({i})"));
delay(1.0, UpdateModeKind::Scaled);
}
}
- The function
hellowith#[entry]becomes an entry point. - The first parameter
&Contextis the information of the running script. It is used to get the arguments, variables and modules. nameandcountare arguments whose values are set in the inspector.loganddelayare functions provided bywukuro:core.
In the import settings, add wukuro and wit-bindgen to Dependencies, enable Componentize, clear WIT Directory and apply. The WIT of a Wukuro script is embedded by #[entry], so WIT Directory must be empty (Componentization). When the import succeeds, the script becomes a component asset (ComponentAsset).
Creating a component to run the script
Create the following C# script.
HelloEvent.cs
using Cysharp.Threading.Tasks;
using UnityEngine;
using Wukuro;
public class HelloEvent : MonoBehaviour
{
[SerializeField] private WukuroEntryReference entry = new();
// where the variables of the script are stored
private readonly MemoryVariables variables = new();
private void Start()
{
PlayAsync().Forget();
}
private async UniTask PlayAsync()
{
var runner = new WukuroRunner();
await runner.RunAsync(entry, variables);
}
}
WukuroEntryReference holds a script, an entry point and the values of the arguments together.
Setting the arguments and running
- Add
HelloEventto a GameObject in the scene. - Set
hello.rsto Script of Entry in the inspector. - Select
hellofrom the list of entry points. The fields ofnameandcountare shown according to the parameters of the entry point. - Enter the values of
nameandcount, and enter the play mode.
It succeeds if a log is output to the console every second.
Next steps
- See Writing Scripts for the features available in scripts.
- See Running Scripts for the details of running scripts from C#.
- Showing dialogs and calling game-specific features from scripts require host implementations. See Extending.