The method for parsing Lua scripts in Unity3d, and unity3d for parsing lua scripts

Source: Internet
Author: User

The method for parsing Lua scripts in Unity3d, and unity3d for parsing lua scripts

Because the project has been proposed recentlyHot updateSo I spent the weekend with Lua. The combination of Lua and Unity3d only reaches a glimpse of the door path. The level is limited. You are welcome to correct your criticism.

 

There is not much information on the use of Lua scripts and Unity3d on the network. Most of the examples are the same. Some of the targeted plug-ins are availableULua, UniLua, KopiLuaThree. I tried the first two methods. I don't want to worry about efficiency and security.ULuaThis article only introducesULua.

The principle of uLua is to parse the string-form Lua script in Unity3d, so that Lua and C # Can PASS Parameters and methods to each other, so that the Lua script can be recorded in the form of text, after the game starts, it can dynamically run some external logic in the game.

If the content of the loaded resource is recorded in the external Lua script, you can load different resources by updating the file with the Lua script to achieve hot game updates.

(Of course, the number of Lua scripts used and the parts used are worth considering for each game, such as worrying about low efficiency, it may not be suitable for the part that requires the running speed. Once an error occurs in the Lua script, it will only report the location where it is resolved in C, therefore, the difficulty of troubleshooting may also be a problem)

 

1]Download to the resource package of ulua, import the package to the project, and copy the contents in the Plugins folder to the Plugins folder of the project according to the instructions;

(The dll cannot be found in this step. Restart Unity here)

The root directory of the imported project contains the following:


 

2]Write some Lua scripts in the. txt file, for example:


(Note: The suffix of the txt script is ". txt ")

 

3]Load the TXT file with Lua script to the game. The resource type is "TextAsset ":

Optional methods, such:

① Put it in the Resources folder, and Resources. Load () to Load;

② Place it anywhere in the project and load it using Resoueces. LoadAtPath;

③ Compress into AssetBundle and place it on the file server. Use WWW for asynchronous loading.

(If ③ is used, it is recommended that a Virtual File Server tool named "HFS" be very convenient. Just click it to simulate a file server:

, Very easy to use)

 

4]Parsing Lua script:

1 LuaState ls = new LuaState ();
2 ls.DoString (luaString); // luaString is the character in the loaded document, the type is string
3 LuaFunction lf = ls.GetFunction ("luaFunc"); // luaFunc is the method name written in the Lua script
4 object [] r = lf.Call ("2"); // "2" in brackets is the value of the integer operation to be passed to the method in Lua
5 return r [0] .ToString (); // r [0] is the value returned after the operation, and the type is object. Before use, you need to change the type 

It is very simple. In summary, the connection between Lua and C # In Unity3d, and the mutual transmission of methods and parameters will be OK. You will see that there is really external logic that plays a role in a running game.


What software environment should a beginner learn about LUA's actual operations and applications? How should we develop LUA to script planning?

Lua depends on the usage of different companies
The general usage is that the functional logic implemented by the lua script on the server is not complicated. Some games tend to be used as game configurations.
In fact, there is no need to deliberately pursue any environment. It is enough to take a closer look at how the execution efficiency will be high.
In addition, when your environment is well set up, it will change as soon as you go to planning and enter the game. The interfaces are all different, so the only useful thing is the logic, habits, and methods for solving the problem.

How can I call a function in another lua script in the lua script ??

In addition to the answer from the upstairs, dofile ("B. lua") can then call the function in B. lua in the. lua script.

Related Article

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.