提交 fba1615d 编写于 作者: Emo_Tiny's avatar Emo_Tiny

【lua框架】lua和C#交互代码提交

上级 2bcb3b3e
--- ---
--- Generated by EmmyLua(https://github.com/EmmyLua) --- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by zhengxiaofeng. --- Created by zhengxiaofeng.
--- DateTime: 2021/8/17 15:22 --- DateTime: 2021/8/17 15:22
......
--- ---
--- Generated by EmmyLua(https://github.com/EmmyLua) --- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by tiny. --- Created by tiny.
--- DateTime: 2021/8/12 21:00 --- DateTime: 2021/8/12 21:00
...@@ -10,47 +10,6 @@ local _registry = ...@@ -10,47 +10,6 @@ local _registry =
object =setmetatable({},{__mode = "k"}) object =setmetatable({},{__mode = "k"})
} }
local _luaClass =
{
IsA = _IsA,
NormalClass = _CreateClass,
AbstractClass = _AbstractClass;
FinalClass = _FinalClass,
DumpClass = _DumpClass,
DumpObject = _DumpObject
}
--实例化
local function _Instantiate(self, ...)
assert(_IsA(self,"class"),"Class must be called from a class")
assert(not _registry.class[self].__abstract,"Can not Instantiate from an abstract class")
local instance = _DeepCopy[self]
_registry.object[instance] =
{
__type = "object",
__superClass = self,
__addr = tostring(instance)
}
setmetatable(instance, { __index = self,
-- 提供gc时的析构调用,由于gc时机不定,所以不要依赖此方法做资源释放
__gc = function(obj)
if _IsA(obj, "object") and obj.__dtor then
obj:__dtor()
end
end,
__tostring = self.__tostring,
--__newindex = function(t, k, v) logerror(string.format("'%s' is not exist in %s", k, t)) end
})
-- 构造调用
if self.__ctor then
self.__ctor(instance, ...)
end
return instance
end
--用于判断一个数据的类型,一般用于判断一个table是否是class还是object --用于判断一个数据的类型,一般用于判断一个table是否是class还是object
local function _IsA(thing,kind) local function _IsA(thing,kind)
if kind then if kind then
...@@ -68,7 +27,7 @@ local function _IsA(thing,kind) ...@@ -68,7 +27,7 @@ local function _IsA(thing,kind)
else else
return _registry.object[thing].__type return _registry.object[thing].__type
end end
end end
return false return false
end end
...@@ -90,6 +49,58 @@ local function _DeepCopy(t) ...@@ -90,6 +49,58 @@ local function _DeepCopy(t)
end end
end end
--实例化
local function _Instantiate(self, ...)
assert(_IsA(self,"class"),"Class must be called from a class")
assert(not _registry.class[self].__abstract,"Can not Instantiate from an abstract class")
local instance = _DeepCopy[self]
_registry.object[instance] =
{
__type = "object",
__superClass = self,
__addr = tostring(instance)
}
setmetatable(instance, { __index = self,
-- 提供gc时的析构调用,由于gc时机不定,所以不要依赖此方法做资源释放
__gc = function(obj)
if _IsA(obj, "object") and obj.__dtor then
obj:__dtor()
end
end,
__tostring = self.__tostring,
--__newindex = function(t, k, v) logerror(string.format("'%s' is not exist in %s", k, t)) end
})
-- 构造调用
if self.__ctor then
self.__ctor(instance, ...)
end
return instance
end
--类或对象的字符串化
local function _ToString(self)
local is = _IsA(self)
if is then
local name = nil
if is == "class" then
name = _registry.class[self].__name
elseif is == "object" then
name = _registry.class[_registry.object[self].__index].__name
end
return ("%s: %s"):format(is, name)
else
return tostring(self)
end
end
local _mtBaseClass =
{
__call = function(self,...) return self:New() end,
__tostring = _ToString
}
local _CreateClass
--普通继承 --普通继承
local function _Extends(self,name ,flags) local function _Extends(self,name ,flags)
assert(_IsA(self,"class"),"Inheritance must be called from a class") assert(_IsA(self,"class"),"Inheritance must be called from a class")
...@@ -192,22 +203,6 @@ local function _GetSubClasses(self) ...@@ -192,22 +203,6 @@ local function _GetSubClasses(self)
return _registry.class[self].__subClass or {} return _registry.class[self].__subClass or {}
end end
--类或对象的字符串化
local function _ToString(self)
local is = _IsA(self)
if is then
local name = nil
if is == "class" then
name = _registry.class[self].__name
elseif is == "object" then
name = _registry.class[_registry.object[self].__index].__name
end
return ("%s: %s"):format(is, name)
else
return tostring(self)
end
end
--对象或者类的类型判断(即判断self 是不是参数class的类型),如果是,返回true,不是返回false,如果没有填class类型,则直接返回self的类型 --对象或者类的类型判断(即判断self 是不是参数class的类型),如果是,返回true,不是返回false,如果没有填class类型,则直接返回self的类型
local function _TypeOf(self,class,shallow) local function _TypeOf(self,class,shallow)
if class then if class then
...@@ -226,11 +221,7 @@ local function _TypeOf(self,class,shallow) ...@@ -226,11 +221,7 @@ local function _TypeOf(self,class,shallow)
end end
end end
local _mtBaseClass =
{
__call = function(self,...) return self:New() end,
__tostring = _Tostring
}
--内部的创建类的方法 --内部的创建类的方法
local function _CreateClass(name,flags) local function _CreateClass(name,flags)
local newClass = {} local newClass = {}
...@@ -264,5 +255,11 @@ local function _CreateClass(name,flags) ...@@ -264,5 +255,11 @@ local function _CreateClass(name,flags)
return setmetatable(newClass,_mtBaseClass); return setmetatable(newClass,_mtBaseClass);
end end
local _luaClass =
{
IsA = _IsA,
NormalClass = _CreateClass
}
setmetatable(_luaClass,{__call = function(self,name,flags) return _CreateClass(name,flags) end}) setmetatable(_luaClass,{__call = function(self,name,flags) return _CreateClass(name,flags) end})
return _luaClass return _luaClass
\ No newline at end of file
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by zhengxiaofeng.
--- DateTime: 2021/8/24 10:57
--- Description:用于测试C#和lua之间的交互
local LuaClass = require("LuaClass")
local LuaTest = LuaClass("LuaTest",{abstract = false,final = true})
print("开始加载-------->>")
--全局变量
GlobalVariant_1 = 1
GlobalVariant_2 = 2.0
GlobalVariant_3 = "全局变量"
localClass = {
GlobalVariant_4 = 1,
GlobalVariant_5 = 2.0,
GlobalVariant_6 = "全局变量"
}
--全局函数
function CSCallLuaFunc()
print("全局函数")
end
function LuaTest:LocalTest()
print("公有函数")
end
return LuaTest
fileFormatVersion: 2
guid: aad13aaac92c5b54696fac130022c344
ScriptedImporter:
internalIDToNameTable: []
externalObjects: {}
serializedVersion: 2
userData:
assetBundleName:
assetBundleVariant:
script: {fileID: 11500000, guid: 3b8b241bab4a4ac9a22fcce9c64f1242, type: 3}
--- -- Generated by EmmyLua(https://github.com/EmmyLua)
--- Generated by EmmyLua(https://github.com/EmmyLua) -- Created by zhengxiaofeng.
--- Created by zhengxiaofeng. -- DateTime: 2021/8/17 14:52
--- DateTime: 2021/8/17 14:52
---
require("FrameworkGlobal") require("FrameworkGlobal")
local ServiceModuleManager = require("ServiceModuleManager") local ServiceModuleManager = require("ServiceModuleManager")
ServiceModuleManager:Init( ServiceModuleManager:Init(
{ {
{ "Proto.ProtocolManager", true } ,--网络协议 --{ "Proto.ProtocolManager", true } ,--网络协议
{ "CoroutineManager", true } , --协程管理 --{ "CoroutineManager", true } , --协程管理
{ "EventDispatcher", true } ,--事件系统 --{ "EventDispatcher", true } ,--事件系统
{ "UnitTest.CharacterManager", false } , --{ "UnitTest.CharacterManager", false } ,
{ "UnitTest.Test_RpcQueue", false } , --{ "UnitTest.Test_RpcQueue", false } ,
{"Localize.LocalizeMgr",false} --国际化 --{"Localize.LocalizeMgr",false} --国际化
} }
) )
...@@ -22,3 +20,4 @@ ServiceModuleManager:LoadCoreModule() ...@@ -22,3 +20,4 @@ ServiceModuleManager:LoadCoreModule()
ServiceModuleManager:LoadExtendModule() ServiceModuleManager:LoadExtendModule()
--- ---
--- Generated by EmmyLua(https://github.com/EmmyLua) --- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by zhengxiaofeng. --- Created by zhengxiaofeng.
--- DateTime: 2021/8/17 14:35 --- DateTime: 2021/8/17 14:35
...@@ -60,12 +60,12 @@ end ...@@ -60,12 +60,12 @@ end
--Lua中所有模块的Tick(Update)的能力入口 --Lua中所有模块的Tick(Update)的能力入口
function ServiceModuleManager:Tick(delta, unscaledDeltaTime) function ServiceModuleManager:Tick(delta, unscaledDeltaTime)
for _v, Service in ipairs(self.CoreModules) do for _v, Service in ipairs(ServiceModuleManager.CoreModules) do
if Service.Instance ~= nil and Service.Instance:Tick() ~= nil then if Service.Instance ~= nil and Service.Instance:Tick() ~= nil then
Service.Instance:Tick() Service.Instance:Tick()
end end
end end
for _v, Service in ipairs(self.ExtendModules) do for _v, Service in ipairs(ServiceModuleManager.ExtendModules) do
if Service.Instance ~= nil and Service.Instance:Tick() ~= nil then if Service.Instance ~= nil and Service.Instance:Tick() ~= nil then
Service.Instance:Tick() Service.Instance:Tick()
end end
......
...@@ -19,11 +19,14 @@ namespace ZFramework.XLua ...@@ -19,11 +19,14 @@ namespace ZFramework.XLua
private const string MainScriptName = "Main"; private const string MainScriptName = "Main";
private const string EventDispatcherScriptName = "EventDispatcher"; private const string EventDispatcherScriptName = "EventDispatcher";
public LuaEnv LuaVM => _luaEnv;
//Lua脚本所在的文件夹 //Lua脚本所在的文件夹
public static readonly string[] ScriptsFolders = public static readonly string[] ScriptsFolders =
{ {
$"Assets/Game/LuaScripts", $"Assets/Game/LuaScripts/",
"Assets/LuaFramework/LuaScript" "Assets/LuaFramework/LuaScript/"
}; };
private void Start() private void Start()
...@@ -42,7 +45,6 @@ namespace ZFramework.XLua ...@@ -42,7 +45,6 @@ namespace ZFramework.XLua
if (_luaEnv!=null) if (_luaEnv!=null)
{ {
_luaEnv.Global.SetInPath($"package.loaded.{AssetManager.LuaPackageName}",AssetManager.Instance); _luaEnv.Global.SetInPath($"package.loaded.{AssetManager.LuaPackageName}",AssetManager.Instance);
//_luaEnv.Global.SetInPath($"package.loaded.{}",);
_luaEnv.AddLoader(CustomLoad);//为了方便lua加密,在这个加载的接口里可以做解密 _luaEnv.AddLoader(CustomLoad);//为了方便lua加密,在这个加载的接口里可以做解密
} }
else else
...@@ -63,22 +65,22 @@ namespace ZFramework.XLua ...@@ -63,22 +65,22 @@ namespace ZFramework.XLua
int readCount = 0; //不同文件夹的lua脚本不能同名 int readCount = 0; //不同文件夹的lua脚本不能同名
for (int i = 0; i < ScriptsFolders.Length; i++) for (int i = 0; i < ScriptsFolders.Length; i++)
{ {
string filePath = $"{ScriptsFolders[i]}.{path}.lua"; string filePath = $"{ScriptsFolders[i]}{path}.lua";
if (File.Exists(ScriptPath)) if (File.Exists(filePath))
{ {
ScriptPath = filePath; ScriptPath = filePath;
readCount++; readCount++;
} }
} }
if (readCount>0) if (readCount>1)
{ {
Debug.LogError($"reduplicated script name: {path}"); Debug.LogError($"reduplicated script name: {path}");
return null; return null;
} }
else else
{ {
byte[] bytes = FileSystem.ReadAllBytes(path); byte[] bytes = FileSystem.ReadAllBytes(ScriptPath);
return bytes; return bytes;
} }
......
...@@ -368,6 +368,7 @@ GameObject: ...@@ -368,6 +368,7 @@ GameObject:
- component: {fileID: 1748644602} - component: {fileID: 1748644602}
- component: {fileID: 1748644601} - component: {fileID: 1748644601}
- component: {fileID: 1748644600} - component: {fileID: 1748644600}
- component: {fileID: 1748644603}
m_Layer: 0 m_Layer: 0
m_Name: Main Camera m_Name: Main Camera
m_TagString: MainCamera m_TagString: MainCamera
...@@ -440,6 +441,18 @@ Transform: ...@@ -440,6 +441,18 @@ Transform:
m_Father: {fileID: 0} m_Father: {fileID: 0}
m_RootOrder: 0 m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1748644603
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1748644599}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 6fdc40a0979640546af7897295f66c9c, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &1993561609 --- !u!1 &1993561609
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
......
...@@ -19,6 +19,7 @@ namespace ZFramework.Core ...@@ -19,6 +19,7 @@ namespace ZFramework.Core
//读取bin文件,建立起ab包的依赖拓扑结构 //读取bin文件,建立起ab包的依赖拓扑结构
public void Load() public void Load()
{ {
#if ASSETBUNDLE_ENABLE
Stream stream = FileSystem.OpenRead(MetaFileName); Stream stream = FileSystem.OpenRead(MetaFileName);
BinaryReader reader = new BinaryReader(stream); BinaryReader reader = new BinaryReader(stream);
int bundleCount = reader.ReadInt32(); int bundleCount = reader.ReadInt32();
...@@ -72,6 +73,8 @@ namespace ZFramework.Core ...@@ -72,6 +73,8 @@ namespace ZFramework.Core
} }
} }
stream.Close();; stream.Close();;
#endif
} }
public void Update(float deltaTime) public void Update(float deltaTime)
......
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using XLua;
using ZFramework.XLua;
public class TEST_Xlua_CSharpCallLua : MonoBehaviour public class TEST_Xlua_CSharpCallLua : MonoBehaviour
{ {
// Start is called before the first frame update // Start is called before the first frame update
void Start() void Start()
{ {
//先加载脚本
StartCoroutine(LoadXlua());
} }
// Update is called once per frame [CSharpCallLua]
void Update() class LuaRefTest
{ {
public int GlobalVariant_1;
public float GlobalVariant_2;
public string GlobalVariant_3;
}
private IEnumerator LoadXlua()
{
//Lua虚拟机初始化
XLuaManager.Active();
yield return null;
//加载测试lua脚本
XLuaManager.Instance.LoadScript("LuaTest");
//获取lua的基本类型的全局变量
string test = XLuaManager.Instance.LuaVM.Global.Get<string>("GlobalVariant_3");
Debug.Log("C#调用lua的基本类型全局变量-------->>" + test);
//映射到普通class
LuaRefTest luaRefTest = XLuaManager.Instance.LuaVM.Global.Get<LuaRefTest>("localClass");
Debug.Log("映射到普通class----->>" + luaRefTest.GlobalVariant_1);
//映射到luaFunction
LuaFunction luaFunction = XLuaManager.Instance.LuaVM.Global.Get<LuaFunction>("CSCallLuaFunc");
luaFunction.Call();
LuaFunction luaFunction1 = XLuaManager.Instance.LuaVM.Global.GetInPath<LuaFunction>("package.loaded.LuaTest.LocalTest");
luaFunction1.Call();
} }
} }
fileFormatVersion: 2
guid: 7fd92acba61afb4448f6a0f7250878c7
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册