Lua--------------------unity3D与Slua融合使用

下载与安装

  • 下载地址 GitHub
  • 安装过程

    1.下载最新版,这里, 解压缩,将Assets目录里的所有内容复制到你的工程中,对于最终产品,可以删除slua_src,例子,文档等内容,如果是开发阶段则无所谓。

    2.等待unity编译完毕,如果一切顺利的话,将出现slua菜单, 点击slua菜单中 All->Make 命令 手动生成针对当前版本的U3d接口文件。

    3.每次更新slua版本,务必记得clear all,然后make all,否则可能运行不正确

主要的内容包括

  • LuaState状态机对象执行Lua字符串
  • LuaState状态机对象执行Lua脚本
  • LuaState状态机对象调用Lua脚本内的自定义函数
  • LuaState状态机对象注册C#自定义类
  • Lua脚本中调用C#中的自定义类

创建第一个可以使用Slua框架的Unity项目

  • MainCamera对象上创建AppDelegate.cs组件

      using UnityEngine;
      using System.Collections;
    
      public class AppDelegate : MonoBehaviour 
      {
          void Start () 
          {
              //在下方添加初始化代码
          }
      }
  • 使用LuaState状态机对象执行Lua字符串

      using UnityEngine;
      using System.Collections;
      using SLua;
    
      public class AppDelegate : MonoBehaviour 
      {
          private static LuaState  ls_state = new LuaState();
          void Start () 
          {
              //在下方添加初始化代码
              ls_state.doString("print(\"Hello Lua!\")");
          }
      }
  • 使用LuaState状态机对象执行Lua脚本文件HelloLua.lua

    • Resources文件夹下添加HelloLua.lua文件

        print("Lua Scripts:Hello");
    • AppDelegate.cs中设置LuaState.loaderDelegate启动文件委托代理

        using UnityEngine;
        using System.Collections;
        using SLua;
        using System.IO;
      
        public class AppDelegate : MonoBehaviour 
        {
        void Start () 
        {
            //设置脚本启动代理
            LuaState.loaderDelegate = ((string fn) => {
                //获取Lua文件执行目录
                string file_path = Directory.GetCurrentDirectory() + "/Assets/Resources/" + fn;
      
                Debug.Log(file_path);
      
                return File.ReadAllBytes(file_path);
            });
        }
        }
    • AppDelegate.cs中通过LuaState对象执行HelloLua.lua脚本

        using UnityEngine;
        using System.Collections;
        using SLua;
        using System.IO;
      
        public class AppDelegate : MonoBehaviour 
        {
      
            void Start () 
            {
                //设置脚本启动代理
                LuaState.loaderDelegate = ((string fn) => {
                    //获取Lua文件执行目录
                    string file_path = Directory.GetCurrentDirectory() + "/Assets/Resources/" + fn;
      
                    Debug.Log(file_path);
      
                    return File.ReadAllBytes(file_path);
                });
      
                //设置执行脚本
                LuaState ls_state = new LuaState ();
                ls_state.doFile ("HelloLua.lua");
            }
        }
  • 通过LuaState对象获取并执行HelloLua.lua脚本中的一个函数

    • HelloLua.lua

        function sum( v1,v2 )
            -- body
            return v1 + v2
        end
      
        function mul( v1,v2 )
            -- body
            return v1 * v2
        end
    • AppDelegate.cs

        using UnityEngine;
        using System.Collections;
        using SLua;
        using System.IO;
        using LuaInterface;
        using System;
      
        public class AppDelegate : MonoBehaviour 
        {
            //添加LuaState初始化时的回调函数特性函数
            [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
            static int init(IntPtr L)
            {
                //设置初始化LuaObject对象
                LuaObject.init(L);
                return 0;
            }
      
            void Start () 
            {
                //创建状态机对象
                LuaState ls_state = new LuaState ();
      
                //设置脚本启动代理
                LuaState.loaderDelegate = ((string fn) => {
                    //获取Lua文件执行目录
                    string file_path = Directory.GetCurrentDirectory() + "/Assets/Resources/" + fn;
      
                    Debug.Log(file_path);
      
                    return File.ReadAllBytes(file_path);
                });
      
                //初始化LuaState状态机与C#的转换对象
                LuaState.pcall (ls_state.L, init);
      
                //设置状态机对象的执行脚本
                ls_state.doFile ("HelloLua.lua");
      
                //获取脚本中的mul函数
                LuaFunction mul = ls_state.getFunction ("mul");
      
                //调用该函数并且接收返回值
                double result = (double)mul.call (-2, 3);
      
                Debug.Log(result);
            }
        }
  • 自定义C#对象LOHuman.cs,在HelloLua.lua

    • LOHuman.cs

      using System;
      using LuaInterface;
      using SLua;
      //该特性可以修饰以下类将会注册到Slua执行环境中
      [CustomLuaClass]
      public class HHHuman
      {
        //年龄成员
        protected int age = 0;
        //姓名成员
        protected string name = "";
      
        //添加Lua代码中的静态函数
        [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
        [StaticExport]
        public static int CreateHuman(IntPtr l)
        {
            HHHuman item = new HHHuman ();
            LuaObject.pushObject (l, item);
            //只执行了1次LuaObject.push,返回值写1
            return 1;
        }
      
        public int Age {
            set;
            get;
        }
      
        public string Name{ set; get; }
      }
    • 通过点击菜单栏中的SLua->Custom->Clear将旧版本的自定义类删除

    • 通过点击菜单栏中的SLua->Custom->Make重新制作适用于SLua的新的自定义类Lua_HHHuman.cs
      • 默认存放目录Assets->SLua->LuaObject->Custom->
    • 同时会自动生成一个BindCustom.cs类,代码如下:
      using System;
      namespace SLua {
        [LuaBinder(3)]
        public class BindCustom {
            public static void Bind(IntPtr l) {
                Lua_HHHuman.reg(l);
                Lua_System_Collections_Generic_List_1_int.reg(l);
                Lua_System_Collections_Generic_Dictionary_2_int_string.reg(l);
                Lua_System_String.reg(l);
            }
        }
      }
    • AppDelegate.csinit函数中,绑定自定义类HHHuman.cs
      //添加LuaState初始化时的回调函数特性函数
      [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
      static int init(IntPtr L)
      {
        LuaObject.init(L);
        BindCustom.Bind (L);
        return 0;
      }
    • HelloLua.lua脚本中,调用C#中的自定义类的静态函数CreateHuman()
      function testHuman()
        -- body
        local human = HHHuman.CreateHuman()
        -- local list = human:getList()
        print(human.Age)
      end
    • AppDelegate.csStart函数中,调用HelloLua.lua脚本中的testHuman函数

      static LuaState ls_state;
      void Start () 
      {
        //创建状态机对象
        ls_state = new LuaState ();
      
        //设置脚本启动代理
        LuaState.loaderDelegate = ((string fn) => {
            //获取Lua文件执行目录
            string file_path = Directory.GetCurrentDirectory() + "/Assets/Resources/" + fn;
      
            Debug.Log(file_path);
      
            return File.ReadAllBytes(file_path);
        });
      
        //初始化LuaState状态机与C#的转换对象
        LuaState.pcall (ls_state.L, init);
      
        //设置执行脚本
        ls_state.doFile ("HelloLua.lua");
      
        //获取testHuman函数
        LuaFunction testHuman = ls_state.getFunction ("testHuman");
      
        //无参函数的调用
        testHuman.call ();
      }

以上主要的内容包括:

  • LuaState状态机对象执行Lua字符串
  • LuaState状态机对象执行Lua脚本
  • LuaState状态机对象调用Lua脚本内的自定义函数
  • LuaState状态机对象注册C#自定义类
  • Lua脚本中调用C#中的自定义类

今天先写到这里,明天继续

创建第一个可以使用Slua框架调用UnityEngine的Unity项目

作者:肖浩呗
链接:http://www.jianshu.com/p/2dc2b816f1a4
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。