uLua v3.0.0
A Lua Modding Framework for Unity.
uLua.API Class Reference

This MonoBehaviour sets up the API framework to your Unity scene. Add to an object in your scene and set up for use. 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...
 
LuaAssetBundle GetAssetBundle (string filePath, string packageName="")
 Get a pre-loaded asset bundle from external assets. More...
 
AudioClip GetAudioClip (string filePath, string packageName="")
 Get a pre-loaded audio clip from external assets. More...
 
string[] GetAvailableAssets (string extension="", string packageName="")
 Get a list of available assets in the specified package. More...
 
Sprite GetSprite (string filePath, string packageName="")
 Get a pre-loaded sprite from external assets. More...
 
TextAsset GetTextAsset (string filePath, string packageName="")
 Get a pre-loaded text asset from external assets. More...
 
Texture2D GetTexture2D (string filePath, string packageName="")
 Get a pre-loaded texture2D from external assets. More...
 
LuaAssetBundle LoadAssetBundle (string filePath, string packageName="")
 Loads an asset bundle from the specified external file path. More...
 
Sprite LoadSprite (string filePath, string packageName="")
 Loads a sprite from the specified external file path. More...
 
TextAsset LoadTextAsset (string filePath, string packageName="")
 Loads a text asset from the specified external file path. More...
 
Texture2D LoadTexture2D (string filePath, string packageName="")
 Loads a texture2D from the specified external file path. More...
 

Static Public Member Functions

static void LoadSavedData (LuaMonoBehaviour targetObject, string index="SaveData")
 Loads table data saved for the specified object. More...
 
static void SaveData (LuaMonoBehaviour targetObject, string index="SaveData")
 Saves table data from the specified object. More...
 
static void Invoke (string luaEventName, params object[] args)
 Invokes a game event. More...
 
static void RegisterEvent (string luaEventName, string handlerName="")
 Registers an event to the API. More...
 
static bool RegisterEventHandler (string luaEventName, string handlerName, ILuaObject luaParent=null)
 Registers an event handler to be called when an event is invoked. More...
 
static void RemoveEventHandlers (LuaMonoBehaviour luaParent)
 Removes all event handlers registered in a specific context. More...
 
static void Log (string message)
 Logs a message to the console. More...
 
static void LogError (string message)
 Logs an error to the console. More...
 
static void LogWarning (string message)
 Logs a warning to the console. More...
 
static DynValue CreateLuaTable ()
 Creates a new Lua table.
 
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 DynValue ExecuteScript (string code, string codeFriendlyName="", bool overwriteExisting=false)
 Executes a Lua script. More...
 
static void ExposeToLua< T > (T luaObject, LuaMonoBehaviour luaParent=null)
 Exposes an object to Lua. More...
 
static void ExposeToLua (object targetObject, string luaName, ILuaObject luaParent=null)
 Exposes an object to Lua. 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...
 

Properties

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

Detailed Description

This MonoBehaviour sets up the API framework to your Unity scene. Add to an object in your scene and set up for use.

Includes methods to execute Lua scripts, expose game objects to the API, and invoke events. 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.

◆ ExecuteScript()

static DynValue uLua.API.ExecuteScript ( string  code,
string  codeFriendlyName = "",
bool  overwriteExisting = false 
)
static

Executes a Lua script.

Parameters
codeThe Lua code to execute.
codeFriendlyName(Optional) A friendly name for the code, used for debugging.
overwriteExisting(Optional) If true, the method will overwrite any previous script with the same code.

◆ ExposeToLua()

static void uLua.API.ExposeToLua ( object  targetObject,
string  luaName,
ILuaObject  luaParent = null 
)
inlinestatic

Exposes an object to Lua.

Exposes objects that do not implement the ILuaObject interface, such as MonoBehaviour.

Parameters
targetObjectThe object to be exposed to Lua. The generic type T must be a class.
luaNameThe name of the object in Lua. If the name is already used, a warning will be logged.
luaParent(Optional) The Lua parent of the object. If null, the object will be exposed as a global.

◆ ExposeToLua< T >()

static void uLua.API.ExposeToLua< T > ( luaObject,
LuaMonoBehaviour  luaParent = null 
)
inlinestatic

Exposes an object to Lua.

Called internally by uLua.LuaMonoBehaviour. Can be called manually at any point or for objects which have ExposeTarget set to Manual.

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

◆ GetAssetBundle()

LuaAssetBundle uLua.API.GetAssetBundle ( string  filePath,
string  packageName = "" 
)
inline

Get a pre-loaded asset bundle from external assets.

This method is available as a built-in function for the Lua API.

Parameters
filePathThe path to the asset bundle file
packageNameThe name of the package containing the asset bundle

◆ GetAudioClip()

AudioClip uLua.API.GetAudioClip ( string  filePath,
string  packageName = "" 
)
inline

Get a pre-loaded audio clip from external assets.

This method is available as a built-in function for the Lua API.

Parameters
filePathThe path to the audio clip file
packageNameThe name of the package containing the audio clip

◆ GetAvailableAssets()

string[] uLua.API.GetAvailableAssets ( string  extension = "",
string  packageName = "" 
)
inline

Get a list of available assets in the specified package.

This method is available as a built-in function for the Lua API.

Parameters
extension(Optional) The file extension to filter assets by (e.g. ".png", ".wav"). If empty, all assets will be returned.
packageName(Optional) The name of the package to get available assets from.
Returns
An array of asset file paths relative to the package directory.

◆ GetPackage()

ScriptPackage uLua.API.GetPackage ( string  packageName)

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.

◆ GetSprite()

Sprite uLua.API.GetSprite ( string  filePath,
string  packageName = "" 
)
inline

Get a pre-loaded sprite from external assets.

This method is available as a built-in function for the Lua API.

Parameters
filePathThe path to the sprite file
packageNameThe name of the package containing the sprite

◆ GetTextAsset()

TextAsset uLua.API.GetTextAsset ( string  filePath,
string  packageName = "" 
)
inline

Get a pre-loaded text asset from external assets.

This method is available as a built-in function for the Lua API.

Parameters
filePathThe path to the text asset file
packageNameThe name of the package containing the text asset

◆ GetTexture2D()

Texture2D uLua.API.GetTexture2D ( string  filePath,
string  packageName = "" 
)
inline

Get a pre-loaded texture2D from external assets.

This method is available as a built-in function for the Lua API.

Parameters
filePathThe path to the texture2D file
packageNameThe name of the package containing the texture2D

◆ Invoke()

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

Invokes a game event.

This method is available as a built-in function for the Lua API. Events should registered AFTER Unity's Awake call to prevent unexpected behaviour (e.g. on Unity's scene loaded or start events).

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

◆ LoadAssetBundle()

LuaAssetBundle uLua.API.LoadAssetBundle ( string  filePath,
string  packageName = "" 
)
inline

Loads an asset bundle from the specified external file path.

This method is available as a built-in function for the Lua API.

Parameters
filePathThe path to the asset bundle file
packageNameThe name of the package containing the asset bundle

◆ 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  targetObject,
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 available as a built-in function for the Lua API.

Parameters
targetObjectThe 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".

◆ LoadSprite()

Sprite uLua.API.LoadSprite ( string  filePath,
string  packageName = "" 
)
inline

Loads a sprite from the specified external file path.

This method is available as a built-in function for the Lua API.

Parameters
filePathThe path to the sprite file
packageNameThe name of the package containing the sprite

◆ LoadTextAsset()

TextAsset uLua.API.LoadTextAsset ( string  filePath,
string  packageName = "" 
)
inline

Loads a text asset from the specified external file path.

This method is available as a built-in function for the Lua API.

Parameters
filePathThe path to the text asset file
packageNameThe name of the package containing the text asset

◆ LoadTexture2D()

Texture2D uLua.API.LoadTexture2D ( string  filePath,
string  packageName = "" 
)
inline

Loads a texture2D from the specified external file path.

This method is available as a built-in function for the Lua API.

Parameters
filePathThe path to the texture2D file
packageNameThe name of the package containing the texture2D

◆ Log()

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

Logs a message to the console.

Invokes the event 'LuaMessageLogged' which may be handled in Lua. This method is available as a built-in function for the Lua API.

◆ LogError()

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

Logs an error to the console.

Invokes the event 'LuaErrorLogged' which may be handled in Lua. This method is available as a built-in function for the Lua API.

◆ LogWarning()

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

Logs a warning to the console.

Invokes the event 'LuaWarningLogged' which may be handled in Lua. This method is available as a built-in function for the Lua API.

◆ RegisterEvent()

static void uLua.API.RegisterEvent ( string  luaEventName,
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).

Parameters
luaEventNameThe 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  luaEventName,
string  handlerName,
ILuaObject  luaParent = null 
)
inlinestatic

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

This method is available as a built-in function for the Lua API.

Parameters
luaEventNameThe name of the event.
handlerNameThe name of the event handler function.
luaParent(Optional) The parent 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.

◆ 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  luaParent)
inlinestatic

Removes all event handlers registered in a specific context.

This method is available as a built-in function for the Lua API.

Parameters
luaParentThe parent object that contains the event handler implementation.

◆ SaveData()

static void uLua.API.SaveData ( LuaMonoBehaviour  targetObject,
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 available as a built-in function for the Lua API.

Parameters
targetObjectThe 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".