Unity of study notes (XLua beginner usage and use unity periodic function in lua)

Recent studies also own how to use lua control UI, and then looked at the online presentation, decided to use XLua, after all, TX father out, it was maintained, wondering how their own use, so it comes out as a usable recording.

Of course, XLua update is mainly used for heat, bring myself to try it and see how the interactions and C #.

Then download the address XLua: https: //github.com/Tencent/xLua

After the download finished, the things Assect folder under the project on the inside, the introduction of the namespace XLua ready to use.

the using the System.IO;   // introduced into this space in order to enter the path with 
the using UnityEngine;
 the using XLua;    // first introduced Xlua namespace 

public  class CSToLua: {MonoBehaviour 

    // the Use for the this Initialization 
    void the Start () { 
        LuaEnv Lua = new new LuaEnv ();            // we must do this step, similar to the use of a new class, but this replaced Lua 
        lua.AddLoader (CustomLoader);          // this is a custom load path to Lua, so can in this custom paths to find 
        lua.DoString ( " the require 'MainLua' " );    // this is the name of a loaded lua script for the MainLua 
    } 
    
    //Once per Frame IS Called the Update 
    void the Update () { 
        
    } 

    Private  byte [] CustomLoader ( REF  String filepath)       // this is check the Internet, can be used as a down the API, the function returns a custom path 
    {
         // AB may be redirected to read the package can also specify a local path 
        String STR = Application.dataPath + " / the Lua / " + filepath + " .lua " ; 
        Debug.Log (STR); 
        IF (the File.Exists ( STR))
             return File.ReadAllBytes (STR);
         return  null ; 
    } 
}

After this execution, then the Luau is how to perform it? And unity in this life cycle and update functions that should be how to use it? Then realize what

First, we want to Unity, the establishment of a commission for Update to let it stop execution

using UnityEngine;

public delegate void LuaUpdate();

public class LuaBehaviour : MonoBehaviour {

    public LuaUpdate luaUpdate;

    void Start () {
    }

    // The delegate LuaUpdate period in which the function has been performed Update
     voidUpdate () {          
        IF(luaUpdate =!Null)
            luaUpdate ();    

    }
}

Then we go to find this is to use lua mount code, then lua function will need to repeatedly execute the commission which added to LuaUpdate

- I Lua mount ToLuaVoid the object above, we first find the gameobject, with a unity of time, you need to CS (API where the namespace) .Api.
- As for when to use, when to use ":" I checked it relates to whether incoming self, but have not yet understood, after all the wrong change, change with the change here know what the symbols used. "." . (Check all other information it) 
luaBehaviour = CS.UnityEngine.GameObject.Find ( " ToLuaVoid " ): GetComponent (typeof (CS.LuaBehaviour))   - here for assembly, use typeof (type), because there is no lua c # <T> use this generic
 local TimeCount = 0 

local  function UpdataLua () 
    TimeCount = TimeCount + CS.UnityEngine.Time.deltaTime 
    Print (TimeCount) 
End 

luaBehaviour.luaUpdate = UpdataLua    - will function in Lua added to the inside of the delegate (first time only with =, + = can not, because the air will be reported, but in C #, you can first use + =

 

Guess you like

Origin www.cnblogs.com/takanashi/p/11068995.html