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.GameObject
local obj1 = GameObject();
local obj2 = GameObject('Hello')
print(obj1,obj2)
-example.lua.txt
local GameObject = CS.UnityEngine.GameObject
local Time = CS.UnityEngine.Time
print('deltaTime:',Time.deltaTime)
Time.timeScale = 0.5
print('hello',GameObject.Find('hello'))
//Example.cs
public class Character{
public int m_id;
public string m_name;
public void Log(){
Debug.Log(m_id+":"+m_name);
}
}
- example.lua.txt
local Character = CS.Character
local character = Character()
character.m_id = 25
character.m_name = 'hello'
print(character.m_id)
print(character.m_name)
character:Log()
//Example.cs
public class Manager{
public static int m_score;
public static void Log(){
Debug.Log(m_score);
}
}
-example.lua.txt
local Manager = CS.Manager
Manager.m_score = 100;
Manager.Log();
print(Manager.m_score)
//Example.cs
public class Character{
public void Log(int id){
Debug.Log(id);
}
public void Log(string name){
Debug.Log(name);
}
}
-example.lua.txt
local Character = CS.Character
local character = Character()
character:Log(25)
character:Log('hello')
//Example.cs
public class Character{
public void Log(int id=1,string name="hello"){
Debug.Log(id+":"+name);
}
}
-example.lua.txt
local Character = CS.Character
local character = Character()
character:Log()
character:Log(25)
character:Log(25,'hello')
//Example.cs
public class Character{
public void Log(params string[] names){
foreach(var n in names){
Debug.Log(n);
}
}
}
-example.lua.txt
local Character = CS.Character
local character = Character()
character:Log()
character:Log('hello')
character:Log('hello','world')
//Example.cs
using XLua;
public class Character{
}
[LuaCallCSharp]
public static class CharacterExt{
public static void Log(this Character self){
Debug.Log("ext method");
}
}
-example.lua.txt
local Character = CS.Character
local character = Character()
character:Log()
//Example.cs
public class Character{
public void Attack(ref int hp,out int exp){
hp = 25;
exp = 100;
}
}
-example.lua.txt
local Character = CS.Character
local character = Character()
local hp,exp = character:Attack()
print(hp,exp)
//Example.cs
public enum ParamType{
FIRE,
WATER,
GRASS
}
-exmaple.lua.txt
local ParamType = CS.ParamType;
local ParamType = ParamType.FIRE
print(ParamType)
print(ParamType.__CastFrom(1))
print(ParamType.__CastFrom('GRASS'))
//Example.cs
using System;
public class Character{
public Action<string> OnLog = str=>Debug.Log("C#: "+str);
}
--example.lua.txt
local Character = CS.Character
local character = Character()
character.OnLog('hello')
local function lua_delegate(str)
print('Lua:',str)
end
character.OnLog = character.OnLog + lua_delegate
character.OnLog('hello')
character.OnLog = character.OnLog - lua_delegate
character.OnLog('hello')
//Example.cs
using System;
public class Character{
public event Action<string> OnLog;
public void Log(string str){
if(OnLog == null)return;
OnLog(str);
}
}
--example.lua.txt
local Character = CS.Character
local character = Character()
local function lua_callback(str)
print(str)
end
character:OnLog('+',lua_callback)
character:Log('hello')
character:OnLog('-',lua_callback)
character:Log('hello')
--example.lua.txt
local GameObject = CS.UnityEngine.GameObject
local ParticleSystem = CS.UnityEngine.ParticleSystem
local gameObject = GameObject()
gameObject.AddComponent(typeof(ParticleSystem))
//Example.cs
public interface ICharacter{
void Log();
}
public class Character:ICharacter{
public void Log(){
Debug.Log('hello')
}
}
--example.lua.txt
local Character = CS.Character
local character = Character()
local ICharacter = CS.ICharacter
character:Log()
cast(character,typeof(ICharacter))
character:Log()