preface
There are few articles on the web about Unity using Lua. Slua is another niche Lua library.. Fewer articles..
Existing articles and a little pit… For example, the method name keyword is wrong.. It’s really a pit
So a summary of super simple teaching = =
This article is intended for those who are just learning Lua and want to use the sLua library in unity projects..
The body of the
Import sLua
- 1. Directly download the latest version :sLua library download address
- 2. Then copy the decompressed Assets file to your project’s Assets directory. Do not put it anywhere else.
- 3. After Unity compilation is completed, SLua menu will appear as shown below. Then select the slUa-all-make command to manually generate the Unity interface file for the current version
At this point you can view the official example of Slua in Assets/Slua/ Example. Each scene shows a way to use it.
Use Lua scripts yourself
Create a new Scene and create CreateEmpty to create a C# file to mount to. Then write the following code in a C# file:
using UnityEngine;
using SLua;
public class Test : MonoBehaviour{
private static LuaState ls_state;
void Start()
{
ls_state = new LuaState();
ls_state.doString("print(\"Hello Lua! \ ")"); }}Copy the code
This is what success looks like:
Note that in some tutorials LuaState is initialized when it is declared as a property, but an error is reported. Be sure to initialize LuaState when Awake or Start is needed…
Read lua files
C # code
using UnityEngine; using System.Collections; using SLua; using System.IO; public class OpenLuaFile : MonoBehaviour{ public LuaState state; // sLua script proxy voidStart()
{
state = new LuaState();
state.loaderDelegate = ((string fn) =>
{
string file_Path = Directory.GetCurrentDirectory() + "/Assets/Script/Lua/" + fn;
Debug.Log(file_Path);
returnFile.ReadAllBytes(file_Path); }); // Execute the script state.dofile ("HelloLua.lua"); }}Copy the code
Create the Lua file in the path specified above
Code:
print("Hello, I'm Lua from the file!")
Copy the code
“/Assets/Script/Lua/” is the directory for storing UA files, which can be changed according to your needs.
other
This tutorial is very basic, if you have the energy to learn the knowledge will be organized into a blog to share with you ~