uLua v2.2.0
A Lua Modding Framework for Unity.
uLua.API Class Reference

This script sets up the API framework to your Unity scene. More...

Public Member Functions

ScriptPackage GetPackage (string PackageName)
 Returns the ScriptPackage object for the specified package name. If the package is not found, returns a null value. More...
 
int GetPackageCount ()
 Returns the number of packages found under the script package path.
 
string GetPackageName (int i)
 Returns the name of the package at the specified index.
 
bool LoadPackage (string PackageName)
 Public function used to load a ScriptPackage on demand. More...
 

Static Public Member Functions

static bool ExecuteExternalFile (string File, bool overwriteExisting=false)
 Executes a script from the external directory. More...
 
static bool ExecuteFile (string File, bool overwriteExisting=false)
 Executes a script from the resource directory. More...
 
static void Expose< T > (T Object, LuaMonoBehaviour Context=null)
 Exposes an object to Lua. More...
 
static void Invoke (string Event, params object[] args)
 Invokes a game event. More...
 
static void Log (string String)
 Logs a message to the console. More...
 
static void LogError (string String)
 Logs an error to the console. More...
 
static void LogWarning (string String)
 Logs a warning to the console. More...
 
static void LoadSavedData (LuaMonoBehaviour Object, string Index="SaveData")
 Loads table data saved for the specified object. More...
 
static void RegisterEvent (string Event, string HandlerName="")
 Registers an event to the API. More...
 
static bool RegisterEventHandler (string Event, string HandlerName, ILuaObject Context=null)
 Registers an event handler to be called when an event is invoked. More...
 
static void RemoveEventHandlers (LuaMonoBehaviour Context)
 Removes all event handlers registered in a specific context. More...
 
static void RegisterIndexedType (Type Type)
 Registers a type that implements the ILuaObject interface to Lua. More...
 
static void RegisterType< T > ()
 Registers a type to Lua. More...
 
static void SaveData (LuaMonoBehaviour Object, string Index="SaveData")
 Saves table data from the specified object. More...
 
static bool TryRegisterEventHandler (string HandlerName, ILuaObject Context=null)
 Attempts to register an event handler for a registered event. More...
 

Static Public Attributes

static string ExternalDirectory = ""
 Directory for external Lua scripts. The path is determined at run-time.
 
static string SaveDataPath = ""
 Path for user save data under the external directory.
 
static string ScriptsPath = ""
 Path for user scripts under the external directory.
 
static string MainResourcesFolder = ""
 Path to the main resources folder for scripts. Used by the VS Code debugger to find the Lua scripts.
 
static string ScriptPackagesPath = ""
 Path for script packages under the external directory.
 
static string LuaScriptsExtension = ""
 File extension for all external Lua scripts.
 

Detailed Description

This script sets up the API framework to your Unity scene.

Includes methods to execute Lua scripts, expose game objects to the API, and invoke events. Inherits from MonoBehaviour. Intended use is to add to an object in the scene and set up for use.

This script invokes the SceneLoaded event when the scene is loaded.

Member Function Documentation

◆ ExecuteExternalFile()

static bool uLua.API.ExecuteExternalFile ( string  File,
bool  overwriteExisting = false 
)
inlinestatic

Executes a script from the external directory.

Parameters
FileThe Lua file to execute. The file extension must be omitted.
overwriteExisting(Optional) If true, the method will overwrite any previous script with the same filename. Otherwise, any previous script will be called from a hash table.

◆ ExecuteFile()

static bool uLua.API.ExecuteFile ( string  File,
bool  overwriteExisting = false 
)
inlinestatic

Executes a script from the resource directory.

Parameters
FileThe Lua file to execute. The file extension must be omitted.
overwriteExisting(Optional) If true, the method will overwrite any previous script with the same filename. Otherwise, any previous script will be called from a hash table.

◆ Expose< T >()

static void uLua.API.Expose< T > ( Object,
LuaMonoBehaviour  Context = null 
)
inlinestatic

Exposes an object to Lua.

Called internally by uLua.ExposedMonoBehaviour.Start(). Can be called manually at an earlier point or for objects which have ExposeOnStart disabled.

Parameters
ObjectThe object to be exposed to Lua. The generic type T must implement the ILuaObject interface.
ContextThe Lua context of the object. If null, the object will be exposed as a global.
Type Constraints
T :class 
T :ILuaObject 

◆ GetPackage()

ScriptPackage uLua.API.GetPackage ( string  PackageName)
inline

Returns the ScriptPackage object for the specified package name. If the package is not found, returns a null value.

Parameters
PackageNameThe name of the package.

◆ Invoke()

static void uLua.API.Invoke ( string  Event,
params object[]  args 
)
inlinestatic

Invokes a game event.

This method is exposed to Lua as a global and may be called from a Lua script or within C#. Events should registered AFTER Unity's Awake call to prevent unexpected behaviour (e.g. on Unity's scene loaded or start events). Events may also be registered when an object is exposed to Lua by overriding uLua.ExposedMonoBehaviour.OnExpose().

Parameters
EventThe name of the event to be invoked.
args(Optional) Parameters for the event handler.

◆ LoadPackage()

bool uLua.API.LoadPackage ( string  PackageName)
inline

Public function used to load a ScriptPackage on demand.

Parameters
PackageNameThe name of the ScriptPackage to be loaded.

◆ LoadSavedData()

static void uLua.API.LoadSavedData ( LuaMonoBehaviour  Object,
string  Index = "SaveData" 
)
inlinestatic

Loads table data saved for the specified object.

Saved data will be loaded from the SaveDataPath under the external directory. This method is exposed to Lua as a global and may be called from a Lua script or within C#.

Parameters
ObjectThe object of which the saved data will be loaded.
Index(Optional) The index of Object which contains the table data to be saved. Default value is "SaveData".

◆ Log()

static void uLua.API.Log ( string  String)
inlinestatic

Logs a message to the console.

Invokes the event 'LuaMessageLogged' which may be handled in Lua. This method is exposed to Lua as a global and may be called from a Lua script or within C#.

◆ LogError()

static void uLua.API.LogError ( string  String)
inlinestatic

Logs an error to the console.

Invokes the event 'LuaErrorLogged' which may be handled in Lua. This method is exposed to Lua as a global and may be called from a Lua script or within C#.

◆ LogWarning()

static void uLua.API.LogWarning ( string  String)
inlinestatic

Logs a warning to the console.

Invokes the event 'LuaWarningLogged' which may be handled in Lua. This method is exposed to Lua as a global and may be called from a Lua script or within C#.

◆ RegisterEvent()

static void uLua.API.RegisterEvent ( string  Event,
string  HandlerName = "" 
)
inlinestatic

Registers an event to the API.

Events should registered AFTER Unity's Awake call to prevent unexpected behaviour (e.g. on Unity's scene loaded or start events). Events may also be registered when an object is exposed to Lua by overriding uLua.ExposedMonoBehaviour.OnExpose().

Parameters
EventThe name of the event to be registered.
HandlerName(Optional) The name of the event handler function. Defaults to 'On{Event}', e.g. the default even thandler for an event named 'LoadingFinished' would be 'OnLoadingFinished',

◆ RegisterEventHandler()

static bool uLua.API.RegisterEventHandler ( string  Event,
string  HandlerName,
ILuaObject  Context = null 
)
inlinestatic

Registers an event handler to be called when an event is invoked.

This method is exposed to Lua as a global and may be called from a Lua script or within C#.

Parameters
EventThe name of the event.
HandlerNameThe name of the event handler function.
Context(Optional) The object that contains the event handler implementation. If not specified, the event handler will be global.

◆ RegisterIndexedType()

static void uLua.API.RegisterIndexedType ( Type  Type)
inlinestatic

Registers a type that implements the ILuaObject interface to Lua.

Intended for use with LuaMonoBehaviour and LuaClass.

◆ RegisterType< T >()

static void uLua.API.RegisterType< T > ( )
inlinestatic

Registers a type to Lua.

Any C# or Unity type may be registered to use within the API. Use with caution.

◆ RemoveEventHandlers()

static void uLua.API.RemoveEventHandlers ( LuaMonoBehaviour  Context)
inlinestatic

Removes all event handlers registered in a specific context.

This method is exposed to Lua as a global and may be called from a Lua script or within C#.

Parameters
ContextThe object that contains the event handler implementation.

◆ SaveData()

static void uLua.API.SaveData ( LuaMonoBehaviour  Object,
string  Index = "SaveData" 
)
inlinestatic

Saves table data from the specified object.

Data will be saved to the SaveDataPath under the external directory. This method is exposed to Lua as a global and may be called from a Lua script or within C#.

Parameters
ObjectThe object for which the data will be saved.
Index(Optional) The index of Object which contains the table data to be saved. Default value is "SaveData".

◆ TryRegisterEventHandler()

static bool uLua.API.TryRegisterEventHandler ( string  HandlerName,
ILuaObject  Context = null 
)
inlinestatic

Attempts to register an event handler for a registered event.

Parameters
HandlerNameThe name of the event handler function. If a matching registered event is not found, this function has no effect.
(Optional)Context The object that contains the event handler implementation. If not specified, the event handler will be global.