c0d3n4m 发表于 2021-8-11 13:20

Unity实验室之XLua调用C#程序

介绍

xLua是腾讯在github上的一个开源项目(下载链接),主要解决热更的问题,是和C#(Unity,.Net,Mono)结合的解决方案。支持android,ios,windows,linux,osx等平台。目前已经有许多成熟产品应用案例使用了xLua.本文主要介绍xLua如何调用C#脚本。
创建游戏对象

-example.lua.txt
local obj1 = CS.UnityEngine.GameObject()local obj2 = CS.UnityEngine.GameObject('Hello')print(obj1,obj2)省略命名空间

-example.lua.txt
local GameObject = CS.UnityEngine.GameObjectlocal obj1 = GameObject();local obj2 = GameObject('Hello')print(obj1,obj2)访问Unity的静态类

-example.lua.txt
local GameObject = CS.UnityEngine.GameObjectlocal Time = CS.UnityEngine.Timeprint('deltaTime:',Time.deltaTime)Time.timeScale = 0.5print('hello',GameObject.Find('hello'))创建自定义类的实例

//Example.cspublicclass Character{    publicint m_id;    publicstring m_name;    publicvoidLog(){      Debug.Log(m_id+":"+m_name);    }}- example.lua.txtlocal Character = CS.Characterlocal character = Character()character.m_id = 25character.m_name = 'hello'print(character.m_id)print(character.m_name)character:Log()调用静态变量和方法

//Example.cspublicclass Manager{    publicstaticint m_score;    publicstaticvoidLog(){      Debug.Log(m_score);    }}-example.lua.txtlocal Manager = CS.ManagerManager.m_score = 100;Manager.Log();print(Manager.m_score)调用重载函数

//Example.cspublicclass Character{    publicvoidLog(int id){      Debug.Log(id);    }    publicvoidLog(string name){      Debug.Log(name);    }}-example.lua.txtlocal Character = CS.Characterlocal character = Character()character:Log(25)character:Log('hello')使用默认参数调用方法

//Example.cspublicclass Character{    publicvoidLog(int id=1,string name="hello"){      Debug.Log(id+":"+name);    }}-example.lua.txtlocal Character = CS.Characterlocal character = Character()character:Log()character:Log(25)character:Log(25,'hello')使用变长参数调用方法

//Example.cspublicclass Character{    publicvoidLog(paramsstring[] names){      foreach(var n in names){            Debug.Log(n);      }    }}-example.lua.txtlocal Character = CS.Characterlocal character = Character()character:Log()character:Log('hello')character:Log('hello','world')调用扩展方法

//Example.csusing XLua;publicclass Character{}publicstaticclass CharacterExt{    publicstaticvoidLog(this Character self){      Debug.Log("ext method");    }}-example.lua.txtlocal Character = CS.Characterlocal character = Character()character:Log()使用ref 和out关键字

//Example.cspublicclass Character{    publicvoidAttack(refint hp,outint exp){      hp = 25;      exp = 100;    }}-example.lua.txtlocal Character = CS.Characterlocalcharacter = Character()local hp,exp = character:Attack()print(hp,exp)使用枚举类型

//Example.cspublicenum ParamType{    FIRE,    WATER,    GRASS}-exmaple.lua.txtlocal ParamType = CS.ParamType;local ParamType = ParamType.FIREprint(ParamType)print(ParamType.__CastFrom(1))print(ParamType.__CastFrom('GRASS'))使用delegate

//Example.csusing System;publicclass Character{    public Action<string> OnLog = str=>Debug.Log("C#: "+str);}--example.lua.txtlocal Character = CS.Characterlocalcharacter = Character()character.OnLog('hello')localfunctionlua_delegate(str)    print('Lua:',str)endcharacter.OnLog = character.OnLog + lua_delegatecharacter.OnLog('hello')character.OnLog = character.OnLog - lua_delegatecharacter.OnLog('hello')事件的使用

//Example.csusing System;publicclass Character{    publicevent Action<string> OnLog;    publicvoidLog(string str){      if(OnLog == null)return;      OnLog(str);    }}--example.lua.txtlocal Character = CS.Characterlocal character = Character()local functionlua_callback(str)print(str)endcharacter:OnLog('+',lua_callback)character:Log('hello')character:OnLog('-',lua_callback)character:Log('hello')typeof的使用

--example.lua.txtlocal GameObject = CS.UnityEngine.GameObjectlocal ParticleSystem = CS.UnityEngine.ParticleSystemlocal gameObject = GameObject()gameObject.AddComponent(typeof(ParticleSystem))接口

//Example.cspublicinterface ICharacter{    void Log();}publicclass Character:ICharacter{    publicvoidLog(){      Debug.Log('hello')    }}--example.lua.txtlocal Character = CS.Characterlocalcharacter = Character()local ICharacter = CS.ICharactercharacter:Log()cast(character,typeof(ICharacter))character:Log()
页: [1]
查看完整版本: Unity实验室之XLua调用C#程序