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.GameObject
local obj1 = GameObject();
local obj2 = GameObject('Hello')
print(obj1,obj2)

访问Unity的静态类

-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()

使用ref 和out关键字

//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'))

使用delegate

//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')

typeof的使用

--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()

你可能感兴趣的:(Unity3D)