调用xlua读取lua脚本
调用xlua读取lua脚本
调用xlua读取lua脚本
00 前言
对于一些形如以下格式的数据, 可能需要通过c#去读取这些lua数据.
01 处理方法
首先, 安装xlua, 将Assets和Tools保持路径覆盖到Unity工程中即可. 然后打开工程运行”XLua->Generate Code”.
之后, 编写以下主类脚本
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
using System;
using System.Collections.Generic;
using UnityEngine;
using XLua;
[Serializable]
public class LuaLoader<T>
{
public TextAsset luaScript; // 从Resources加载的Lua脚本
[HideInInspector]
public Dictionary<int, T> DataDictionary;
private LuaEnv _luaEnv;
public Dictionary<int, T> GetData()
{
// TextAsset luaScript = Resources.Load<TextAsset>("Cfg_Effect.lua");
// Debug.Log(luaScript.text);
_luaEnv = _luaEnv ?? new LuaEnv();
// 执行Lua脚本
_luaEnv.DoString(luaScript.text);
// 获取Lua表中的数据
// var data = _luaEnv.Global.Get<Dictionary<int,Dictionary<string,object>>>("data");
var data2 = _luaEnv.Global.Get<Dictionary<int, T>>("data");
// Debug.Log(data2[1].Path);
// 将数组赋值给到CfgEffects以便外部调用.
DataDictionary = data2;
return data2;
}
public void Dispose()
{
_luaEnv.Dispose();
}
}
然后, 根据数据结构编写以下子类和对应的结构体即可:
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
using System;
using UnityEngine;
[Serializable]
public class LuaEffectLoader : LuaLoader<CfgEffect>
{
public void Check()
{
DataDictionary[1].DebugLog();
}
}
public struct CfgEffect
{
public int EffectId;
public string Path;
public string Path2;
public int DestoryTime;
public int PreLoadEff;
public int FlowBones;
public int Direction;
public int FollowScale;
public int Mirror;
public int Scaling;
public void DebugLog()
{
Debug.Log($"EffectId: {EffectId}\nPath: {Path}");
}
}
并用一个统一的单例来管理
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
public class SkillPreviewerDataManager : MonoSingleton<SkillPreviewerDataManager>
{
public LuaEffectLoader luaEffectLoader = new LuaEffectLoader();
public LuaSkillLoader luaSkillLoader = new LuaSkillLoader();
protected override void Awake()
{
base.Awake();
}
void Start()
{
luaEffectLoader.GetData();
luaSkillLoader.GetData();
luaEffectLoader.Check();
luaSkillLoader.Check();
}
// Update is called once per frame
void Update()
{
}
private void OnDestroy()
{
// 释放LuaEnv
luaEffectLoader.Dispose();
luaSkillLoader.Dispose();
}
}
单例的父类
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
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public abstract class MonoSingleton<T> : MonoBehaviour where T : MonoSingleton<T>
{
private static T _instance;
public static T instance
{
get
{
if (_instance==null)
{
Debug.Log(typeof(T).ToString()+"is Null");
// Do Something
}
return _instance;
}
}
protected virtual void Awake()
{
_instance = this as T;
}
}
参考网页
让unity识别.lua文件,不用修改为.lua.txt_Listenlsls的博客-CSDN博客
VS Code隐藏Unity工程中meta文件以及将后缀为lua.txt或者lua.bytes识别为lua的方法_unity自定义后缀名识别-CSDN博客
【精选】XLua系列讲解_C#访问Lua中的table类型 _c# 解析 lua表_DaLiangChen的博客-CSDN博客
[Unity实战]Xlua插件安装步骤(基于2018.3Unity)_mscorlib.dll unity_学生董格的博客-CSDN博客
XLua官方教程目录 总结篇(配置XLua开发环境、安装EmmyLua插件、xLua文档、xLua热补丁、xLua示例教程)_xlua教程-CSDN博客
xLua C#访问lua,获取lua全局变量,获取table并调用lua内函数(四)_xlua 获得lua table-CSDN博客
Unity Lua 之 在 Unity中 通过 文件加载的方式,读取执行文件中的 lua 内容,实现 Hello Lua_unity lua读取文本-CSDN博客
本文由作者按照 CC BY 4.0 进行授权
