Xlua与C#(Unity)的相互读取

**

一.读取lua文件

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;
using System.IO;

public class AutoLoader : MonoBehaviour {

	// Use this for initialization
	void Start () {
        LuaEnv env = new LuaEnv();
    //输出lua语句
        ////方法一(不建议)
        env.DoString("CS.UnityEngine.Debug.Log('HelloWorld')");

        ////方法二(Text01文件必须是在Resources下)
        env.DoString("require 'Text01' ");


        //方法三(法二的自定义路径读取lua文件)
        env.AddLoader(MyLoader);

        env.DoString("require 'XluaText01' ");

        env.Dispose();
	}
	public byte[] MyLoader(ref string filePath)//该filePath  为文件名对应env.DoString("require 'XluaText01(这个文件)' ")
    {
        //路径
        string path = Application.streamingAssetsPath + "/" + filePath + ".lua.txt";
        return System.Text.Encoding.UTF8.GetBytes(File.ReadAllText(path));
    }
	
}


**
**

二.lua中的table映射到c#

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using XLua;

public class Lua_CustomLoader : MonoBehaviour {

	// Use this for initialization
	void Start () {
        LuaEnv env = new LuaEnv();

        env.AddLoader(MyLoader);

        //env.DoString("require 'xluaTest'");
        env.DoString("require 'xluaTest001'");

        //访问lua中的全局变量
        int playerLv = env.Global.Get<int>("lv");
        string playerName = env.Global.Get<string>("playerName");
        bool isDie = env.Global.Get<bool>("isDie");
        Debug.Log(playerLv + "=====" + playerName + "=========" + isDie);

        //1.映射到类 获取全局table表
        Person LLWH = env.Global.Get<Person>("Person");
        Debug.Log(LLWH.name + "  " + LLWH.age + " " + LLWH.isSingle);

        //2.映射到interface
        IPerson LLWH = env.Global.Get<IPerson>("Person");
        Debug.Log(LLWH.name + "  " + LLWH.age + " " + LLWH.isSingle);

        //3.映射到字典中
        Dictionary<string, object> dict = env.Global.Get<Dictionary<string, object>>("Person");
        foreach (string key in dict.Keys)
        {
            Debug.Log(key + "+======>" + dict[key]);
        }

        //4.映射到list种
        List<object> list = env.Global.Get<List<object>>("Person");
        foreach (object item in list)
        {
            Debug.Log(item + "=====>");
        }

        //5.映射到luatable
        LuaTable table = env.Global.Get<LuaTable>("Person");

        print(table.Get<string>("name"));
        print(table.Get<int>("age"));
        print(table.Get<bool>("isSingle"));


        //1.C#访问lua方法  无参
        Action printFunc = env.Global.Get<Action>("PrintFunc");
        printFunc();
        //访问方法都需要这步操作
        printFunc = null;

        //2.C#访问lua方法 有参
        Add printFunc = env.Global.Get<Add>("AddFunc");
        int result = printFunc(10, 5);
        Debug.Log(result);
        printFunc = null;

        ///C#调用lua方法 使用luafaction
        LuaFunction func = env.Global.Get<LuaFunction>("AddFunc");

        object[] res =  func.Call(1,23);

        foreach (object item in res)
        {
            Debug.Log(item);
        }

        env.Dispose();
	}

    /// 
    /// 
    /// 
    /// 文件路径
    /// 加载器加载lua文件
    public byte[] MyLoader(ref string filepath)
    {
        //print(filepath);
        //string str = "print('hello zhengke')";

        //steamingasset下的lua文件路径
        string path =  Application.streamingAssetsPath + "/" + filepath + ".lua.txt";
        //lua中代码文本转换字节数组
        return System.Text.Encoding.UTF8.GetBytes(File.ReadAllText(path));
       
    }
    // Update is called once per frame
    void Update () {
	
	}
}

class Person
{
    public string name;
    public int age;
    public bool isSingle;
}

[CSharpCallLua]
interface IPerson
{
    string name { get; set; }
    int age { get; set; }
    bool isSingle { get; set; }
}
[CSharpCallLua]
delegate int Add(int a,int b);

**
**

三.lua访问C#(Unity)

1.C#脚本中 与lua脚本链接

//默认路径为Resources下,想自定义 看上面
LuaEnv env = new LuaEnv();
        env.DoString("require 'Text01'");

        env.Dispose();

2.lua脚本中调用Unity中的方法

--调用Unity中的方法 
--CS.命名空间.类+方法
UnityEngine = CS.UnityEngine
--lua中的new 对象
UnityEngine.GameObject('HelloWorld')
--访问C#中静态属性  调用用"."
print( UnityEngine.Time.deltaTime )
--访问C#中静态方法  调用用"."
local cameraGo = UnityEngine.GameObject.Find('Main Camera')
--访问C#中成员属性  调用用"."
local cameraCom = cameraGo:GetComponent("BoxCollider")
UnityEngine.GameObject.Destroy(cameraCom)
--☆访问C#中成员方法  调用用":"
cameraGo:setActive(false)

**

你可能感兴趣的:(Unity)