zoukankan      html  css  js  c++  java
  • SLua

    安装

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

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

      3.每次更新SLua版本,务必记得All->Clear,然后All->Make,否则可能运行不正确。

    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("SLua Hello!")");
        }
    
    }

    使用LuaState状态机对象执行Lua脚本文件HelloLua.lua

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

    print("Lua Script:HelloLua")  

      设置LuaState.loaderDelegate启动文件委托代理

    using UnityEngine;
    using System.Collections;
    using SLua;
    using System.IO;
    
    public class AppDelegate : MonoBehaviour {
    
        void Start () {
    
           //设置脚本启动代理
            LuaState.loaderDelegate = ((string fn)=>{
    
                //获取脚本启动代理
                string file_path = Directory.GetCurrentDirectory() + "/Assets/Resources/" + fn;
    
                Debug.Log("file_path:" + file_path);
    
                return File.ReadAllBytes(file_path);
            });
    
        }
    
    }

      通过LuaState对象执行HelloLua.lua脚本

    using UnityEngine;
    using System.Collections;
    using SLua;
    using System.IO;
    
    public class AppDelegate : MonoBehaviour {
    
        void Start () {
    
           //设置脚本启动代理
            LuaState.loaderDelegate = ((string fn)=>{
    
                //获取脚本启动代理
                string file_path = Directory.GetCurrentDirectory() + "/Assets/Resources/" + fn;
    
                Debug.Log("file_path:" + 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)
    
        return v1 + v2
    end
    
    function mul( v1, v2)
    
        return v1 * v2
    end

      AppDelegate.cs

    using UnityEngine;
    using System.Collections;
    using SLua;
    using System.IO;
    using System;
    using LuaInterface;
    
    public class AppDelegate : MonoBehaviour {
    
        //添加LuaState初始化时的回调函数特性函数
        [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
        protected static int init(IntPtr L)
        {
            //设置初始化LuaObject对象
            LuaObject.init(L);
            return 0;
        }
    
        void Start () {
    
            //创建状态机对象 
            LuaState ls_state = new LuaState();
    
           //设置脚本启动代理
            LuaState.loaderDelegate = ((string fn)=>{
    
                //获取脚本启动代理
                string file_path = Directory.GetCurrentDirectory() + "/Assets/Resources/" + fn;
    
                Debug.Log("file_path:" + 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);
        }
    
    }

       注:如出现`SLua.LuaState.pcall(System.IntPtr, LuaInterface.LuaCSFunction)' is inaccessible due to its protection level异常,进去看下pcall的定义,把internal修改为public即可。

    自定义c#队形LOHuman.cs,在Hello.lua中

      LOHuman.cs

    using LuaInterface;
    using SLua;
    using System;
    using UnityEngine;
    using System.Collections;
    
    [CustomLuaClassAttribute]
    public class HHHuman : MonoBehaviour
    {
        //年龄成员
        protected int age = 0;
    
        //姓名成员
        protected string name = "";
    
        //添加Lua代码中的静态函数
        [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
        [StaticExport]
        static public int CreateHuman(IntPtr l)
        {
            HHHuman item = new HHHuman();
            LuaObject.pushObject(l, item);
    
            return 1;
        }
    
        public string speak(string str)
        {
            string speakStr = "speak:";
            if (str!=null)
                speakStr += str;
            return speakStr;
        }
    
        public int Age
        {
            set;
            get;
        }
    
        public string Name
        {
            set;
            get;
        }
    }

       通过点击菜单栏中的SLua->Custom->Clear将旧版本的自定义类删除。

      通过点击菜单栏中的SLua->Custom->Make重新制作适用于SLua的新的自定义类Lua_HHHuman.cs。

      默认存放目录Assets->SLua->LuaObject->Custom->。

      在HelloLua.lua脚本中,调用c#中的自定义类的静态函数CreateHuman()。

    function main()
    
        local human = HHHuman.CreateHuman()
    
        print("human.Age: ",human.Age)
        print("human.speak: ",human:speak("hi"))
    end

      在AppDelegate.cs的Start函数中,调用HelloLua.lua脚本中的main函数

    using UnityEngine;
    using System.Collections;
    using SLua;
    using System.IO;
    using System;
    using LuaInterface;
    
    public class AppDelegate : MonoBehaviour {
    
     
        LuaSvr l;
        void Start () {
    
            l = new LuaSvr();
            l.start("HelloLua");
    
        }
    
    }
  • 相关阅读:
    Windows Server 2012配置开机启动项
    Windows Server 2019 SSH Server
    NOIP2017 senior A 模拟赛 7.7 T1 棋盘
    Noip 2015 senior 复赛 Day2 子串
    Noip 2015 senior复赛 题解
    Noip 2014 senior Day2 解方程(equation)
    Noip 2014 senior Day2 寻找道路(road)
    Noip 2014 senior Day2 无线网络发射器选址(wireless)
    Noip2014senior复赛 飞扬的小鸟
    Noip 2014 senior 复赛 联合权值(link)
  • 原文地址:https://www.cnblogs.com/-soy/p/5724827.html
Copyright © 2011-2022 走看看