对于lua的简单调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;

//引用命名空间

public class CsCallLua : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
//Lua解析器 能够让我们在unity中执行lua
//一遍情况下保持他的唯一性
LuaEnv env = new LuaEnv();
//执行Lua语言
env.DoString("print('Hello World')","CsCallLua");

//执行一个lua脚本 lua知识点:多脚本执行 reqire
//默认寻找脚本的路径 实在Resources下 并且因为在这里
//估计是通过resource.load去加载lua脚本 txt bytes等
//所以lua脚本 后缀要加一个txt
env.DoString("require('Main')");

//帮助我们清除lua中我们没有手动释放的对象 垃圾回收
//帧更新中定时执行 或者切场景的时候执行
env.Tick();

//销毁lua解析器
env.Dispose();
}

// Update is called once per frame
void Update()
{

}
}

文件加载重定向

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using XLua;

public class CsCallLua2 : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
LuaEnv env = new LuaEnv();

//Xlua提供的一个路径重定向的方法
//允许我们自定义加载LUA文件得到规则
//当我们执行Lua语言 require是 相当于执行一个lua脚本
//他就会执行我们指定以传入的这个函数
env.AddLoader(MyCusomLoader);
env.DoString("require('Main')");


}

// Update is called once per frame
void Update()
{

}

//自动执行
private byte[] MyCusomLoader(ref string filePath)
{
//通过函数中的逻辑去加载lua文件
//传入的参数 是requi执行的lua脚本文件名
//拼接一个lua文件所在路径
string path = Application.dataPath + "/Lua/" + filePath + ".lua";
Debug.Log(path);
//有路径就去加载文件
//file知识点 c#提供的文件读写的类
if (File.Exists(path))
{
return File.ReadAllBytes(path);
}
else
{
Debug.Log("重定向失败,文件名为"+ filePath);
}

return null;
}
}

解析器管理器

luamgr:构建一个唯一的lua解析器luaenv提供很多外部使用的方法操作lua相关的内容
ab包加载lua脚本重定向
注意:平时不用,只有测试热更新或者最终打包时才会使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using XLua;

/// <summary>
/// Lua管理器
/// 提供 lua解析器
/// 保证解析器的唯一性
/// </summary>
public class LuaMgr : BaseManager<LuaMgr>
{
//执行Lua语言的函数
//释放垃圾
//销毁
//重定向
private LuaEnv luaEnv;


/// <summary>
/// 得到Lua中的_G
/// </summary>
public LuaTable Global
{
get
{
return luaEnv.Global;
}
}


/// <summary>
/// 初始化解析器
/// </summary>
public void Init()
{
//已经初始化了 别初始化 直接返回
if (luaEnv != null)
return;
//初始化
luaEnv = new LuaEnv();
//加载lua脚本 重定向
luaEnv.AddLoader(MyCustomLoader);
luaEnv.AddLoader(MyCustomABLoader);
}

//自动执行
private byte[] MyCustomLoader(ref string filePath)
{
//通过函数中的逻辑 去加载 Lua文件
//传入的参数 是 require执行的lua脚本文件名
//拼接一个Lua文件所在路径
string path = Application.dataPath + "/Lua/" + filePath + ".lua";

//有路径 就去加载文件
//File知识点 C#提供的文件读写的类
//判断文件是否存在
if (File.Exists(path))
{
return File.ReadAllBytes(path);
}
else
{
Debug.Log("MyCustomLoader重定向失败,文件名为" + filePath);
}


return null;
}


//Lua脚本会放在AB包
//最终我们会通过加载AB包再加载其中的Lua脚本资源 来执行它
//重定向加载AB包中的LUa脚本
private byte[] MyCustomABLoader(ref string filePath)
{
//Debug.Log("进入AB包加载 重定向函数");
////从AB包中加载lua文件
////加载AB包
//string path = Application.streamingAssetsPath + "/lua";
//AssetBundle ab = AssetBundle.LoadFromFile(path);

////加载Lua文件 返回
//TextAsset tx = ab.LoadAsset<TextAsset>(filePath + ".lua");
////加载Lua文件 byte数组
//return tx.bytes;

//通过我们的AB包管理器 加载的lua脚本资源
TextAsset lua = ABMgr.instance.LoadRes<TextAsset>("lua", filePath + ".lua");
if (lua != null)
return lua.bytes;
else
Debug.Log("MyCustomABLoader重定向失败,文件名为:" + filePath);

return null;
}


/// <summary>
/// 传入lua文件名 执行lua脚本
/// </summary>
/// <param name="fileName"></param>
public void DoLuaFile(string fileName)
{
string str = string.Format("require('{0}')", fileName);
DoString(str);
}

/// <summary>
/// 执行Lua语言
/// </summary>
/// <param name="str"></param>
public void DoString(string str)
{
if(luaEnv == null)
{
Debug.Log("解析器为初始化");
return;
}
luaEnv.DoString(str);
}

/// <summary>
/// 释放lua 垃圾
/// </summary>
public void Tick()
{
if (luaEnv == null)
{
Debug.Log("解析器为初始化");
return;
}
luaEnv.Tick();
}

/// <summary>
/// 销毁解析器
/// </summary>
public void Dispose()
{
if (luaEnv == null)
{
Debug.Log("解析器为初始化");
return;
}
luaEnv.Dispose();
luaEnv = null;
}
}