xlua 基础知识

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


public class XLuaSprite : MonoBehaviour {
    //   public LuaEnv xluaenv;  //创建XLua运行环境
    //void Start () {
    //       xluaenv = new LuaEnv(); //创建完场
    //       xluaenv.DoString("CS.UnityEngine.Debug.Log('Hello Wroid')");//调用c#  输出Hello Worid
    //   }
    //   private void OnDisable()
    //   {


    //       xluaenv.Dispose();   //卸载lua  
    //   }






    //public LuaEnv xluaenv;  //创建XLua运行环境
    //void Start()  //调用内置的加载
    //{
    //    xluaenv = new LuaEnv(); //创建完场
    //    xluaenv.DoString("require'XLuaHelloWorid'");//调用lua脚本  输出Hello Worid(不需要加后缀)
    //}
    //private void OnDisable()
    //{
    //    xluaenv.Dispose();   //卸载lua  
    //}






    //自定义lua loader 记载文件


    //private void Start()
    //{
    //    LuaEnv xluaenv = new LuaEnv();
    //    xluaenv.AddLoader(AddLoader);             //加入自定义loader文件
    //    xluaenv.DoString("require'hellde'");      //加载lua读取
    //    xluaenv.Dispose();
    //}


    //public byte[] AddLoader(ref string addloader) {   //定义loader
    //    print(addloader);
    //    string s = "print('12345')";
    //    return System.Text.Encoding.UTF8.GetBytes(s);
    //}




    private void Start()
    {
        LuaEnv luaEnv = new LuaEnv();
        luaEnv.AddLoader(AddLoader);
        luaEnv.DoString("require'AddLoader'"); 
        luaEnv.Dispose();
    }


    public byte[]  AddLoader(ref string loadername) {
      string ads=  Application.streamingAssetsPath + "/" + loadername + ".lua.txt";   //加载lu文件   特文件夹加载  获取字节流
      return  System.Text.Encoding.UTF8.GetBytes(File.ReadAllText(ads));              //字节流输出  引入using system.io 命名空间




    }

}


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;
public class CsharpCalllua : MonoBehaviour {


    void Start()
    {
        LuaEnv xluaenv = new LuaEnv();
        xluaenv.DoString("require'CSharpCalllua'");
        person p = xluaenv.Global.Get<person>("person");
        print(p);
        //接口修改值
        print(p.name+p.age);
        p.name = "hello ok";
        print(p.name);
        p.eat(20,30);
         
    }
 //       //int a= xluaenv.Global.Get<int>("a");   //1.获取lua基本数据  global.get<>()方法 
 //       // print(a);
 //       // int b=  xluaenv.Global.Get<int>("b");
 //       // print(b);
 //       // string str= xluaenv.Global.Get<string>("str");
 //       // print(str);
 //       // double  c= xluaenv.Global.Get<double>("c");
 //       // print(c);
 //       perison p = xluaenv.Global.Get<perison>("perison");  //2.获取全局table 定义一个class 或者struct
 //       print(p.name+" +"+p.age);
 //       xluaenv.Dispose();
 //}
 //   public class perison  //定义获取数据 对应lua里的字段    
 //   {
 //      public string name;
 //      public int age;}
 
         
    [CSharpCallLua]   //接口调用数据 得加上CSharpCallLua属性   接口和方法的区别  接口可以修改lua内部的参数   方法不可以  
    public interface person
    {  //字段得有get set 访问器
        int age { get; set; }
        string name { get; set; }
        void eat(int a,int b);  //获取方法  名称得一样   
    }

}

可以修改lua中的值了

3、更轻量级的by value方式:映射到Dictionary<>,List<>


  void Start()
    {
        LuaEnv xluaenv = new LuaEnv();
        xluaenv.DoString("require'CSharpCalllua'");
        //List<int> listp = xluaenv.Global.Get<List<int>>("person");  //list结合获取int类型数据
        //foreach (int item in listp)
        //{
        //    print(item);
        //}
        Dictionary<string, object> dictionary = xluaenv.Global.Get<Dictionary<string, object>>("person");//字典key 和获取数数据 可以根据获取数据的类型  在dictionary中定义值类型
        foreach  (string item in dictionary.Keys)
        {
            print( item +dictionary[item]);
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值