Using LUA in Unity3d

Source: Internet
Author: User
Tags lua

Unity3d is a good engine, but one drawback is that iOS cannot be hot updated. If you can use Lua to write Unity3d game logic, then you can bypass the Apple audit random update.

One way to do this is to implement LUA on a C # basis. There are several versions that can be found on this web. But this operating efficiency is not guaranteed, and is said to be 10 times times slower than the average Lua. Another way is to use the C version of Lua, using the plugin feature of Unity3d Pro, to use PInvoke to interact with the native LUA engine. Nlua

Consider using the native LUA engine here. You can find something called Nlua on the Internet. It implements the functionality of Lua interacting with C #. Its structure is divided into 3 levels: Lua: This is a LUA optimized for mobile platform compilation. What's the difference between a standard LUA and I didn't look closely. Keralua: Based on the previous level, the C API is packaged in a simple package, making C # easy to use Lua C APIs. Nlua: Based on the previous level, use reflection to dynamically export classes, functions in C # to Lua.

Nlua also has a Kopilua component, which is a pure C # LUA implementation that can replace Keralua. But I'm not going to use it here. Nlua put it in Unity3d .

Nlua is primarily geared towards the Xamarin development environment. In order to be able to use it in the Unity3d, you need to do some hands and feet. compiling lua into a unity3d Plugin

Unity3d's Plugin function can only be seen by official instructions, and there is little information in other places. But the official document structure does not seem to be well organized, and the key part is easy to miss out. The plugin usage of each platform is a little different: Mac OS X: I'm only trying the plugins under Mac OS X. The main point is to compile the bundle with Xcode, and compile the target to choose Universal, if only X64,unity3d will be prompted to find. Bundle files are placed in the Assets/plugins directory. IOS: I just read the instructions and haven't tried. The main point is to put the source code directly into the Assets/plugins/ios directory. Android: Casually looked at, it seems to be compiled into a jar to put into the assets/plugins/android directory. (Other platform slightly) compiling Keralua and Nlua into DLLs

While the Keralua and Nlua code can be plugged into the Assets, it works fine, but compiling into a DLL can make the Assets structure neat, and importing assembly in Lua does not import too much garbage.

There are some parts of Nlua that use System.Diagnostics.Debug and System.Console output exceptions, and I'll use them instead of unityengin.debug to achieve the same functionality. You can use it like that.

Using System;
public class Test:monobehaviour
{
	void Awake ()
	{
		using (var lua = new Nlua.lua ())
		{
			lua. Dostring ("luanet.load_assembly (' Unityengine ')");
			Lua. Dostring ("application = Luanet.import_type (' unityengine.application ')");
			Lua. Dostring ("Debug = Luanet.import_type (' unityengine.debug ')");
			Lua. Dostring ("Debug.Log (application.unityversion)");}}

Very convenient AH there is no.

Although the usage here is two, it can be seen that the ability to invoke C # in Lua is very easy. It's perfectly possible to write all the logic in pure Lua.

If you want to invoke assets inside, then you need to luanet.load_assembly (' Assembly-csharp '). Unity3d will compile the script inside the Assets into Assembly-csharp. Keep Dreaming . Hot Update

The C # part of the Unity3d project should be as small as possible and irrelevant to the content of the game. Because on the iOS platform it is no way to hot update. So the content of C # code should include: Start Lua when the game starts. When the game crashes, upload the debug information and repair the client. A Monobehaviour component for recording data is used only to allow Prefeb to record additional information for easy editing. A series of callback-called Monobehaviour components that allow LUA to register for some event callbacks such as Update.

In addition to these C # programs, all other game content is dynamically updated. Theoretically a game with a hot update can be turned into any other game. network communication

Since all LUA, network communication does not pass through C # around the circle, directly let Lua call C's network communication module, but with what library has not thought well.

The protocol is best to get a protobuf compatible thing. Generate Lua code with protocol descriptions. Performance Optimization

From PInvoke performance This article can be seen PInvoke efficiency is very poor. If you call Lua in the Update and use the Unityengine function with callbacks, the PInvoke frequency will be quite high. If it is the interface part, there should be little problem. But the combat part (a game is basically a fight.) ) is likely to experience performance bottlenecks. One solution is to have a C # call to Lua once per frame and return all instructions at once for this frame. However, it is not possible to use Nlua, you need to do a C # export operation instruction interface to LUA one thing.

In the case of update and network processing, if you use object-oriented thinking, it is likely that a large number of temporary objects (Table in Lua) will be generated at a high frequency. This may cause the garbage collector to be too busy to produce a Dayton card. High frequency Access table may also have performance problems, see this article about Ulua in Unity3d performance testing. The optimization method is to save protocol data, Gameobject data to the contiguous memory allocated by C, and then use pure functions to process the data in batches, avoiding a large number of LUA Table constructs and operations. The libraries in which Lua operates raw memory data can refer to Lua-mmapfile and lua-void.

The above two optimizations all use the idea of data-oriented programming, that is, when designing a module, we use efficient batch calculation method according to the input and output. Object-oriented programming often makes it meaningless to make the code structure look like a business model, add useless layers, violate the characteristics of a computer, and block the processing of data in batches.

To better manage memory, consider using rings to open multiple LUA states, such as a single Lua state for each battle, one Lua state per interface, one LUA state for the Network Processing Section, and one LUA for the local data cache. State (perhaps too fine ...). )。 This makes it easy to know the amount of memory used for each part, and also to completely free up memory. Other references

Here is a Q-spirit of the three countries use Ulua experience, can see. Ulua is a unity plugin, using Luajit + luainterface, and doing some optimizations yourself, no source.

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.