Unity ToLua hot update framework usage tutorial (1)

Starting from this article, I will explain to you the tutorial on using ToLua in unity.

Tolua's framework is called LuaFramework. First, the download link is attached:

https://github.com/jarjin/LuaFramework_UGUI_V2

This address belongs to UGUI.

After downloading, import the project. First, we need to get the project running.


First click Lua->Clear wrap files to clear all wrap files.


Then click LuaFramework->Build Windows Resource to package ab resources.


Finally, click Lua->Generate All to regenerate the wrap file.

But when I generated the Wrap file, an error occurred.


After debugging, it was found that the problem occurred when the ParticleSystem class generated the wrap file.


So just comment out this line and do it again.

Then click Run.


Until here, our project is finally running.

Next, let's take a look at how this framework works.


Before running, we can see that there is only one GameManager mounted with a Main script in the scene. As the name suggests, this script is the entry script.


When the game starts, a StartUp function is called.


In StartUp, another SendMessageCommand is called.


This function calls the ExecuteCommand method of m_controller.


This m_controller is a singleton, created when the Facade is constructed, and the AppFacade class in our entry script inherits from the Facade. We see that when the Facade class is constructed, the InitFramework method is called.


So when AppFacade is constructed, a StartUp type is registered.


Register the type corresponding to the enumeration into the dictionary.

We called ExecuteCommand of m_controller above. We can see that this ExecuteCommand function obtains the corresponding type based on the enumeration passed in, and then uses Activator.CreateInstance(commandType) to create an instance of this type and call its Execute method.


When we registered above, the corresponding type of this enumeration was StartUpCommand.

 

See his Execute method.

Many managers have been added here.


How did he add it? In fact, these managers are inherited from Monobehaviour. In fact, they are added to the GameManager object and stored in the dictionary. So since it is Monobehaiour, these managers must have life cycle functions. We first see GameManager.


The initialization method Init is called in Awake. Then release the resource. Then hot update, file comparison and a series of operations, you can read down, the code is too long to take a screenshot, and finally we will see that he executed this method.


OnInitialize method, LuaManager.DoFile("Logic/Game"); This code loads the Game lua file in the Logic folder. This Game file can be said to be the entry script of the Lua script. You can see the above code, which calls the OnInitOK function of the Game script to drive the lua script.


In the OnInitOK function, he initializes some View scripts and Ctrl scripts. It can be seen here that Tolua actually adopts the MVC idea.

Seeing the InitViewPanels method, he loads all scripts for the PanelNames table. In fact, it is to load the view layer script under the View folder.

Then the code of the view layer script can be used. Anyway, the Game script is just a Lua entry script. You can do whatever you want. That's the process. That’s all for this article.

Guess you like

Origin blog.csdn.net/m0_69824302/article/details/133712582
Recommended