Unity-xlua C#侧将byte[]传递lua侧table

一、 Xlua C#侧与lua侧的类型映射关系

Unity-xlua C#侧将byte[]传递lua侧table

  • 基本类型中C#侧 byte[] 对应lua侧的string;
  • 复杂类型中LuaTable 对应lua侧的table;

二、 实现

  • 思路:在c#侧将byte[]转LuaTable即可

  • C#侧:

    	[LuaCallCSharp]
        public LuaTable TestBytes2()
        {
            byte[] testbyte = System.Text.Encoding.Default.GetBytes("ab");
            luaTable = luaEnv.NewTable();
            for (int i = 0; i < testbyte.Length; i++)
            {
            	//lua table 下标从1开始的
                luaTable.Set(i + 1, testbyte[i]);
            }
            return luaTable;
        }
    
        [LuaCallCSharp]
        public byte[] TestBytes1()
        {
            byte[] testbyte = System.Text.Encoding.Default.GetBytes("ab");
            return testbyte;
        }
        
    
  • lua侧

    function start()
    	print("lua start...")	
    	local cube = CS.UnityEngine.GameObject.Find("Cube")
    	cubeBehavior = cube:GetComponent(typeof(CS.XLuaTest.LuaBehaviour))
    	local tmp1 = cubeBehavior:TestBytes1()
    	print("tmp1 : ", tmp1)
    	local tmp2 = cubeBehavior:TestBytes2()
    	print("tmp2 : ", tmp2[1], tmp2[2])	
    end
    
  • 输出结果截图:
    Unity-xlua C#侧将byte[]传递lua侧table