First Script

Let’s print Hello UNLang! in Unity console window.

① Create “Hello UNLang!” Script

  • Back to Unity editor, you will see UNLang/IDE... menu.
    unlang-1
  • Open the IDE window.
  • Right click in the IDE window and you will see all operations in the context menu.
    unlang-2
  • Add Entry, Constant and Console modules, then connect them as following:
    unlang-3
  • Select Constant module and in the Inspector window, choose String type and input Hello UNLang!.
    unlang-4
  • Right click to save the script to local project, for example, [UnityProject]/Assets/Resources/1.bytes.
    unlang-5

② Run the Script

  • Create a GameObject in the active scene.
  • Attach an empty MonoBehaviour cs file.
  • Edit the empty MonoBehaviour cs file with the following code:

    using UnityEngine;
    using UNLang;
    using UNode;

    public class NewBehaviourScript : MonoBehaviour
    {
    // UNLang instance to execute the script.
    private LangInstance instance = null;

    void Start()
    {
    // Take over the loader since we put the script file
    // under Resources folder.
    NodeLoader.Load = file =>
    {
    // Remove "Resources" relative path and file extension.
    file = file.Replace("/Resources/", "");
    file = file.Substring(0, file.IndexOf("."));
    return Resources.Load<TextAsset>(file).bytes;
    };

    // Create UNLang instance.
    this.instance = new LangInstance();
    // Load script file "1.bytes".
    this.instance.Load("1.bytes");
    // Start the script from "Entry" module.
    this.instance.Run<Entry>();
    }

    void Update()
    {
    // Update UNLang instance.
    this.instance?.Update();
    }
    }
  • Congrats with Hello UNLang! in Unity console window.