Summary of personal experiences using Ulua in unity

Source: Internet
Author: User
Tags lua

Heat6052 2015-3-15 14:32 | Personal classification: Technology | Lua

LUA code is compiled at run time, not run as a picture, a piece of audio, is a file, so the update logic only need to update the script, no need to compile, so Lua can easily implement "hot update." Ulua is a very useful unity plugin that allows unity to support the Lua language, and it works well. Let's talk about some of my experiences with Ulua.

The process of using Ulua is generally:

Instantiates the Luastate object (new Luastate ()) è loads the LUA code (luastate. Dostring (string)) È calls a method in Lua code (getfunction (String), Luafunction.callfunction (String)). Where the LUA code is loaded, you can assign a LUA code string directly, or you can specify a LUA script file. For hot updates, you should use the second loading method, which is to create a LUA script file. Since unity does not support files with the extension Lua, the Lua script extension can be named txt (plain text file) and all script files are recorded with Unity's Textasset list. Each script is given a string ID in the recommendation list so that the correct LUA script can be loaded with this ID, and a new public member of type string in the Luastate class is assigned the ID. This way, once a LUA script runs the wrong times, it is possible to determine which Lua script has errors based on the ID value of the output.

About the preprocessing in Lua:

luanet.load_assembly ("Assembly-csharp")

luanet.load_assembly (' Unityengine ')

Vector2 = Luanet.import_type (' Unityengine.vector2 ')

Vector3 = Luanet.import_type (' Unityengine.vector3 ')

Gameobject = Luanet.import_type (' Unityengine.gameobject ')

Luanet.import_type (' System.Collections.Generic.List ')

Debug = Luanet.import_type (' Unityengine.debug ')

These are common Lua preprocessing, and it is recommended that you write a LUA script to record these, and then load the script before loading other LUA scripts. Custom C # classes can also be import, and the import operation is a precondition for Lua to call C #. Of course the luastate instantiated in C # can also predefine some LUA global variables, which are done in C #, such as:

[Code]csharpcode:
void Setluadata (Luastate lua) {       lua["transform"] = transform;       lua["Gameobject"] = gameobject;}

Two variables are predefined, one is transform, and the other is gameobject.

About the global variables in LUA:

All of the strings in Lua, if not keywords or operators, are variables, and in those variables, the global variable, or the local variable, is the one that does not use the local keyword modifier. Each Luastate object, when it has loaded the LUA code, defines the global variables that persist throughout the lifetime of the object. If two calls to this luastate method, the first time a global variable is modified, then the second time, the global variable will be modified on the basis of the first modification.

About the scope of LUA's application in unity:

While LUA can be responsible for any of the Unity engineering modules, for game performance considerations, it is possible to call Lua with as little frequency as possible, such as minimizing the call to LUA in the update function, and recycling the luastate that have already been instantiated to avoid wasting resources. For those modules that do not require efficient operations, such as the UI part, you can use LUA with confidence.

About the adaptation of Lua to Ngui:

The main way to call Lua is CallFunction, and for Ngui, which is generally a function of a C # script triggered by a button, how do you use a button to trigger a LUA function? This requires a C # script as a "mediator", which needs to have its own luastate instance, which is called by the method when executing a method of "mediation". CallFunction As to which method of invoking Lua script specifically, you can tell the "mediation" by means of the parameter-modifying the Buttonmessage class of Ngui, adding the new public string Luafunctionname member, It is good to make the Lua method name to invoke later. And through the Ngui UIButton, such as calling C # method, but also the same (NGUI3.5.6 version, you can give UIButton and so on components of the trigger method to add parameters).

About the adaptation of Lua to Simplejson:

Simplejson is an open source JSON library, which is described in the previous blog. Because the Lua language is unable to understand the properties in C #, the public a{get,set} in general Simplejson to write new interface functions to return their values for Lua to invoke. And Lua calls a lot of overloaded functions in C #, often judging errors (this is a bug?). ), so simply get some more function names and avoid calling errors.

About the call between Lua and Lua:

Using Ulua in unity, it is difficult to get two LUA scripts to call each other, you need to use C # as a "mediator", and Lua does not support C # generics, so you cannot use the getcomponnet< class name > () in the form of Getcomponnet (" Class name ") to get the component. Two LUA scripts are cumbersome to communicate with each other, because C # script as a "mediator", so I can not pass the parameters of the Tabel type, I would like to put a number of parameters into a JSON string passed past, the other side to solve the JSON package. I feel a bit of egg ache. However, this is not uncommon and can be avoided (for example, a module uses only one large LUA script, each independently reducing communication).

About the LUA editor:

Personal use is notepad++, there are many people with sublime, code folding this piece notepad++ better, and sublime in the function seems more powerful.

Unity Programming Pro, did you change and save the script in unity, and then cut back the painful experience of Unity's direct death? Using LUA, you will say goodbye to this phenomenon, there is a wooden heart?

Summary of personal experiences using Ulua in unity

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.