Lua--------------------Unity3d and Slua fusion use

Source: Internet
Author: User
Tags lua mul

Download and install
    • GitHub
    • installation process
      1. Download the latest version, here, unzip, copy all the contents of the assets directory into your project, for the final product, you can delete slua_src, examples, documents and other content, if it is the development stage does not matter.
      2. Wait for unity to compile, if all goes well, the Slua menu will appear, click the All->make command in the Slua menu to manually generate the U3d interface file for the current version.
      3. Every time you update the Slua version, be sure to remember clear all, then make all, or it may not run correctly
The main content includes
    • LuaStateState machine Object Execution Lua string
    • LuaStateState machine Object Execution Lua script
    • LuaStateA state machine object invokes Lua a custom function within a script
    • LuaStateState machine Object Registration C# custom class
    • LuaC#custom classes in a call in a script
Create the first unity project that can use the Slua framework
  • To MainCamera create a component on an object AppDelegate.cs

      using UnityEngine;  using System.Collections;  public class AppDelegate : MonoBehaviour   {      void Start ()       {          //在下方添加初始化代码      }  }
  • LuaStateexecuting LUA strings using state machine objects

      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!\")");      }  }
  • LuaStateexecuting a LUA script file with a state machine objectHelloLua.lua

    • Resourcesadd a file under a folder HelloLua.lua

        print("Lua Scripts:Hello");
    • Set luastate.loaderdelegate start file delegate

      in AppDelegate.cs
        using Unityengine;  Using System.Collections;  Using Slua;  Using System.IO; public class Appdelegate:monobehaviour {void Start () {//Set script start Agent Luastate.loaderdelegate = (string fn          ) = = {//Get Lua File execution directory string file_path = Directory.GetCurrentDirectory () + "/assets/resources/" + fn;          Debug.Log (File_path);      Return File.readallbytes (File_path);  }); }  }
    • AppDelegate.cs LuaState scripting through objects HelloLua.lua in

        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");      }  }
  • LuaStateget and execute HelloLua.lua a function in a script from an object

    • 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 {//Add luastate when initializing the callback Function property function [Monopinvokecallbackattribute (typeof (Luac          sfunction)] static int init (INTPTR l) {//Set initialization Luaobject object Luaobject.init (L);      return 0;          } void Start () {//Create state machine object Luastate ls_state = new Luastate (); Set Script startup Agent Luastate.loaderdelegate = ((string fn) = = {//Get Lua File execution directory string File_pat              h = directory.getcurrentdirectory () + "/assets/resources/" + fn;              Debug.Log (File_path);          Return File.readallbytes (File_path);          }); Initializes the Luastate state machine with the C # conversion object Luastate.pcall (ls_state.          L, Init);          Sets the execution script for the state machine object Ls_state.dofile ("Hellolua.lua");          Gets the Mul function in the script luafunction mul = ls_state.getfunction ("Mul"); Call the function andReceive return value double result = (double) Mul.call (-2, 3);      Debug.Log (result); }  }
  • Custom C # objects LOHuman.cs , in HelloLua.lua the

    • 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->ClearDelete the old version of the custom class by clicking on the menu bar

    • SLua->Custom->Makere-create a new custom class for Slua by clicking on the menu barLua_HHHuman.cs
      • Default Storage DirectoryAssets->SLua->LuaObject->Custom->
    • A class is automatically generated BindCustom.cs with the following code:
      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);      }  }}
    • In AppDelegate.cs the init function, bind the custom classHHHuman.cs
      //添加LuaState初始化时的回调函数特性函数[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]static int init(IntPtr L){  LuaObject.init(L);  BindCustom.Bind (L);  return 0;}
    • In the HelloLua.lua script, call the static function of the custom class in C #CreateHuman()
      function testHuman()  -- body  local human = HHHuman.CreateHuman()  -- local list = human:getList()  print(human.Age)end
    • In AppDelegate.cs the Start function, call the HelloLua.lua function in the script 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 ();}

The above main contents include:

    • LuaStateState machine Object Execution Lua string
    • LuaStateState machine Object Execution Lua script
    • LuaStateA state machine object invokes Lua a custom function within a script
    • LuaStateState machine Object Registration C# custom class
    • LuaC#custom classes in a call in a script

I'll write it here today and continue tomorrow.

Create the first unity project that can call Unityengine using the Slua framework


Xiaohao Bai
Links: http://www.jianshu.com/p/2dc2b816f1a4
Source: Pinterest
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.

Lua--------------------Unity3d and Slua fusion use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.