[Unity technology excerpt] tolua framework access

Posted by robpoe on Wed, 05 Jan 2022 11:31:33 +0100

[reference blog] he le Buwei- Access to the tolua framework of Unity 3D game client basic framework

 

introduction

How to connect the tolua framework to the Unity project? Assuming that we already have a project and have implemented some infrastructure or the project is complete, how to connect the tolua hot update framework without affecting the structure of the original project?

 

tolua introduction

    1. Resource download:
      Since tolua is to be introduced into the project, the first step must be to arrive first tolua's github repository Download the resource package for tolua from
    2. Resource introduction:
      Decompress the downloaded resources, and you can see the general directory structure as follows:
tolua-master
|-- Assets
|-- Luajit
|-- Luajit64
|-- ProjectSettings
|-- Unity5.x

 

With so many folders, those are what we really need. Copy all the files under the Assets folder to the Assets directory of our project, because I use unity 5.5 1F1, so you need to set unity5 The X / Assets / plugins folder is also copied to the Assets directory of our project for replacement and merging.  
3. Others:
After the Unity editor imports the new resources in the directory, a window will pop up with the prompt "click OK to automatically generate the common type registration file, or this function can be completed step by step through the menu". Click OK directly here.

 

lua starting point

Many practical examples are provided in the ToLua\Examples directory, which is very helpful for novices. We won't repeat them one by one here. We will directly enter the application practice of formal projects. A LuaClient is provided in Tolua\Misc CS, this is the encapsulated startup interface provided by tolua. We can write the tolua manager of our project by inheriting this class. Here I create a luaengine CS, inherit from LuaClient and rewrite two interfaces: InitLoader and LoadLuaFiles:

using System.Collections;
using System.Collections.Generic;
using LuaInterface;
using UnityEngine;
/// <summary>
/// tolua Start the entry and dynamically bind this script to a non destructed script GameObject In fact, it's usually related to the game GameManagr Tied to the same object
/// </summary>
public class LuaEngine : LuaClient {

    protected override LuaFileUtils InitLoader()
    {
        return new LuaResLoader();
    }

    /// <summary>
    /// You can add or modify searches lua Directory of files
    /// </summary>
    protected override void LoadLuaFiles()
    {
#if UNITY_EDITOR
        // Get in the add editor environment lua Path to the script( Assets/lua)
        luaState.AddSearchPath(Application.dataPath + "/lua");
#endif
        OnLoadFinished();
    }
}

 

In the wake method of the game control center script GameManager (bound to a GameObject in the game start scene, usually an Empty GameObject), start the script in the following ways:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour {
    private static GameManager _instance = null;
    private LuaEngine _luaEngine;

    public static GameManager Instance
    {
        get { return _instance; }
    }

    void Awake()
    {
        _instance = this;
        // Set to switch the property that the scene will not be destroyed
        GameObject.DontDestroyOnLoad(gameObject);
        Init();
    }

    /// <summary>
    /// Initialization steps of game manager
    /// </summary>
    void Init()
    {
        _luaEngine = gameObject.AddComponent<LuaEngine>();
    }
}

 

After executing the startup scenario, you can see the final output:

11:34:30.215-0: [Main.lua:3]:logic start
UnityEngine.Debug:Log(Object)

 

This is assets / Lua / main The printed content of lua, which is the starting point of all Lua logic:

--Main entry function. Start here lua logic
function Main()                 
    print("logic start")            
end

--Scene switching notification
function OnLevelWasLoaded(level)
    collectgarbage("collect")
    Time.timeSinceLevelLoad = 0
end

 

Directory structure optimization

After importing tolua's resources, the resources in the Assets directory will be a little messy and miscellaneous. Therefore, it is necessary to optimize some directory structures of tolua according to the original structure of the project:

1.*Wrap.cs Directory:
Assets/Source/Generate is a binding class used to save the C# classes generated by tolua that can be called by lua. However, in my project, I habitually put the scripts in the Assets/Scripts directory, so we only need to open assets / Editor / custom / customsettings CS, modify the following:

public static string saveDir = Application.dataPath + "/Scripts/Generate/"; 

 

Create a Generate directory in Scripts, turn off Unity, and restart. A window will pop up like tolua, prompting "click OK to automatically Generate common type registration files, or complete this function step by step through the menu", Here, you can also directly click OK. At this time, a pile of * wrap.cs script files will be generated in Assets/Scripts/Generate, indicating that the directory has been modified successfully. However, there will be duplicate files, and the entire Source directory must be deleted. Then move Source/LuaConst.cs to the Assets/Scripts. /.. directory.

    1. ToLua Directory:
      The Assets/ToLua directory is the core library of the tolua framework. Of course, according to the rules of our project; The externally imported third-party plug-in resources are mainly placed in the Assets/ThirdParty directory. Here, we directly move the entire tolua directory to the third-party plug-in directory, and then need to modify several places:

 

(1) toluaDir in LuaConst.cs:

public static string toluaDir = Application.dataPath + "/ThirdParty/ToLua/Lua";        //tolua lua File directory

 

(2) toluaBaseType in CustomSettings.cs:

public static string toluaBaseType = Application.dataPath + "/ThirdParty/ToLua/BaseType/";

 

Complete the above two settings and modify the directory.

 

 

Reprint address: https://blog.csdn.net/ych1995612/article/details/79624665?utm_medium=distribute.pc_relevant.none -task-blog-2~default~baidujs_ baidulandingword~default-4. queryctr&spm=1001.2101. 3001.4242. 3&utm_ relevant_ index=7

Topics: Unity