目录
1.导入xlua插件(下载地址:https://github.com/Tencent/xLua)
2.通过xlua插件运行lua程序
3.加载运行Lua源文件
4.添加自定义的Loader方法
5.通过自定义Loader加载指定目录的Lua脚本
6.C#访问Lua中的全局变量
7.C#访问Lua中的table(映射到Class,不推荐使用此方式)
8.C#访问Lua中的table(推荐使用此方式,即映射到interface)
9.C#访问Lua中的全局函数(推荐使用此方式,即映射到delegate)
10.在Lua中new C#对象(创建一个游戏物体gameobject)
11.Lua访问C#静态属性和静态方法
12.Lua访问C#成员属性和成员方法
using UnityEngine;
using XLua;
public class helloworld01 : MonoBehaviour {
private LuaEnv luaenv;
void Start () {
luaenv = new LuaEnv();
luaenv.DoString("print('hello world')");
luaenv.DoString("CS.UnityEngine.Debug.Log('你好')");
}
private void OnDestroy()
{
luaenv.Dispose();
}
}
using UnityEngine;
public class Helloworld02 : MonoBehaviour {
void Start () {
TextAsset ta = Resources.Load("HelloWorld.lua");
print(ta);
}
}
using UnityEngine;
using XLua;
public class Helloworld02 : MonoBehaviour {
void Start () {
TextAsset ta = Resources.Load("HelloWorld.lua");
LuaEnv env = new LuaEnv();
env.DoString(ta.text);
env.Dispose();
}
}
using UnityEngine;
using XLua;
public class CreateLoader : MonoBehaviour {
void Start () {
LuaEnv env = new LuaEnv();
env.AddLoader(MyLoader);
env.DoString("require 'HelloWorld'");
env.Dispose();
}
private byte [] MyLoader(ref string filePath)
{
return null;
}
}
using UnityEngine;
using XLua;
public class CreateLoader : MonoBehaviour {
void Start () {
LuaEnv env = new LuaEnv();
env.AddLoader(MyLoader);
env.DoString("require 'HelloWorld'");
env.Dispose();
}
private byte [] MyLoader(ref string filePath)
{
string s = "print(123)";
return System.Text.Encoding.UTF8.GetBytes(s);
}
}
using UnityEngine;
using XLua;
public class CreateLoader : MonoBehaviour {
void Start () {
LuaEnv env = new LuaEnv();
env.DoString("require 'HelloWorld'");
env.AddLoader(MyLoader);
env.Dispose();
}
private byte [] MyLoader(ref string filePath)
{
string s = "print(123)";
return System.Text.Encoding.UTF8.GetBytes(s);
}
}
using System.IO;
using UnityEngine;
using XLua;
public class CreateLoader : MonoBehaviour {
void Start () {
LuaEnv env = new LuaEnv();
env.AddLoader(MyLoader);
env.DoString("require 'test007'");
env.Dispose();
}
private byte [] MyLoader(ref string filePath)
{
print(filePath);//test007
print(Application.streamingAssetsPath);//E:/unityPractice/XluaPractice/Assets/StreamingAssets
string absPath = Application.streamingAssetsPath + "/" + filePath + ".lua.txt";
return System .Text .Encoding .UTF8 .GetBytes (File.ReadAllText (absPath ));
}
}
using UnityEngine;
using XLua;
public class CSharpCallLua : MonoBehaviour {
void Start () {
LuaEnv env = new LuaEnv();
env.DoString("require 'CSharpCallLua'");
int a = env.Global.Get("a");
print(a);
string str = env.Global.Get("str");
print(str );
bool isDie = env.Global.Get("isDie");
print(isDie );
env.Dispose();
}
}
using UnityEngine;
using XLua;
public class CSharpCallLua : MonoBehaviour {
void Start () {
LuaEnv env = new LuaEnv();
env.DoString("require 'CSharpCallLua'");
Person p = env.Global.Get("person");
print(p.name + ":" + p.age);
p.name = "nihao";
env.DoString("print(person.name)");//LUA:lijiang
env.Dispose();
}
class Person
{
public string name;
public int age;
}
}
using UnityEngine;
using XLua;
public class CSharpCallLua : MonoBehaviour {
void Start () {
LuaEnv env = new LuaEnv();
env.DoString("require 'CSharpCallLua'");
IPerson p = env.Global.Get("person");
print(p.name + ":" + p.age);
p.name = "nihao";
env.DoString("print(person.name)");//LUA:lijiang
p.Eat(12, 34);
env.Dispose();
}
[CSharpCallLua ]
interface IPerson
{
string name { get; set; }
int age { get; set; }
void Eat(int a, int b);
}
}
person={name="lijiang",age=20}
function person.Eat(self,a,b)
print(a+b)
print("I am eating")
end
--[[function person:Eat(a,b) 这种方式也是可以的,默认带一个self参数,代表当前table
print(a+b)
print("I am eating")
end
--]]
不含参函数
using UnityEngine;
using XLua;
using System;
public class CSharpCallLua : MonoBehaviour {
void Start () {
LuaEnv env = new LuaEnv();
env.DoString("require 'CSharpCallLua'");
Action A= env.Global.Get("Add");
A ();
A = null;
env.Dispose();
}
}
function Add() --不含参函数
print("A")
end
using UnityEngine;
using XLua;
public class CSharpCallLua : MonoBehaviour {
void Start () {
LuaEnv env = new LuaEnv();
env.DoString("require 'CSharpCallLua'");
Add A= env.Global.Get("Add");
A (1,2);
A = null;
env.Dispose();
}
[CSharpCallLua]
delegate void Add(int a, int b);
}
function Add(a,b) --含参函数
print(a+b)
end
using UnityEngine;
using XLua;
public class CSharpCallLua : MonoBehaviour {
void Start()
{
LuaEnv env = new LuaEnv();
env.DoString("require 'CSharpCallLua'");
Add A = env.Global.Get("Add");
int resa; int resb;
int a = A(1, 2, out resa, out resb);
print(a);
print(resa);
print(resb);
A = null;
env.Dispose();
}
[CSharpCallLua]
delegate int Add(int a, int b, out int resa, out int resb);
//void Start()
//{
// LuaEnv env = new LuaEnv();
// env.DoString("require 'CSharpCallLua'");
// Add A = env.Global.Get("Add");
// int resa=0; int resb=0;
// int a = A(1, 2, ref resa, ref resb);//用ref也可以,不过上面的resa要赋值,这里赋值为0
// print(a);
// print(resa);
// print(resb);
// A = null;
// env.Dispose();
//}
//[CSharpCallLua]
//delegate int Add(int a, int b, ref int resa, ref int resb);
}
function Add(a,b) --含参函数
print(a+b)
return a+b,a,b
end
using UnityEngine;
using XLua;
public class LuaCallCSharp : MonoBehaviour {
void Start()
{
LuaEnv env = new LuaEnv();
env.DoString("require 'LuaCallCSharp'");
env.Dispose();
}
}
using UnityEngine;
using XLua;
public class LuaCallCSharp : MonoBehaviour {
void Start()
{
LuaEnv env = new LuaEnv();
env.DoString("require 'LuaCallCSharp'");
env.Dispose();
}
}
print(CS.UnityEngine.Time .deltaTime ) --读静态属性
CS.UnityEngine.Time.timeScale=0.5 --写静态属性
local gameObject=CS.UnityEngine.GameObject --调用静态方法
local camera=gameObject.Find("Main Camera")
camera.name="update by lua"
using UnityEngine;
using XLua;
public class LuaCallCSharp : MonoBehaviour {
void Start()
{
LuaEnv env = new LuaEnv();
env.DoString("require 'LuaCallCSharp'");
env.Dispose();
}
}
local gameObject=CS.UnityEngine.GameObject
local camera=gameObject.Find("Main Camera")
local cameraCom=camera:GetComponent("Camera")
--用local cameraCom=camera.GetComponent(camera,"Camera")也是可以的,注意和上面用冒号的区别
cameraCom.enabled = false