Unity3d game development of Lua and game of the indissoluble end: Unilua Hot Update full interpretation

Source: Internet
Author: User
Tags lua object object

--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------

Like my blog Please remember my name: Qin Yuanpei , my blog address is Blog.csdn.net/qinyuanpei.

Reprint please indicate the source, this article Qin Yuanpei , this article source: http://blog.csdn.net/qinyuanpei/article/details/40213439

--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------

  

Hello everyone, I am Qin Yuanpei, welcome everyone to pay attention to my blog, my blog address is Blog.csdn.net/qinyuanpei. In the previous three series, "The Unity3d game development Lua and the game's indissoluble Bond", the blogger led everyone to appreciate the powerful and fascinating role of LUA in game development, through Unilua This open source project we introduced LUA into the Unity3d world, And on this basis we have written the first example program that LUA interacts with Unity3d. Today, we say that Unity3d cooperate with Assetbundle and; LUA implements hot updates.

        First, let's look at what a hot update is! The so-called hot update refers to the system changes in a non-stop state, such as our windows can be done without restarting the update of the patch, the Web server does not restart the premise of the completion of the data and file replacement is a classic example of hot updates. So what is a hot update for Unity3d? If our final release of the Unity3d game is a web game, then every time the game is loaded the implementation of the resource code update is the hot update. If our final release of the Unity3d game is a client-side game, then we implement the resource code update after the client restart is the hot update. Why do you say that? Because the web game needs to ensure that the player can enter the game in time and quickly, we must complete the update of the game resources and code before the game is loaded. However, for the client-side game, the player can wait until the end of the update to enter the game, and most of the client program after the update will require the player to restart the client, so for the client game, hot update is not strictly a hot update. So why do we need to do a hot update? The answer is to shorten the process for users to get a new version of the client and improve the user experience. This is actually the blogger in the previous article mentioned in the traditional stand-alone game relying on the CD-ROM carrier for the issue of the problem, the player in order to obtain the latest version of the game, you need to download a new client and install it on the computer or mobile device. In the Internet product development is called the concept of rapid iteration, imagine if we each time the client to make local adjustments, we need players to download the new version of the client, how can such a user experience really let users satisfied? So now in order to facilitate users, retain users, and then from the retention of the user to make money, we can always find in the game Products Hot update shadow. We know that assetbundle can be used in Unity3d to update the resources in the game, in http://blog.csdn.net/janeky/article/details/ 17666409 the author of this article Janeky has given a more perfect solution, because bloggers use the Unity3d free version can not use the function of Assetbundle, and bloggers himself do not want to use cracked version, because it is a programmer's conscience, So this article is more from the code update from this point of view Unity3d hot Update, for the resources of the hot update everyone is advised to go to see janeky this article. Okay, here's the official start of the Unity3d code-level hot Update tour!

In the official Unity API, a reflection-based approach is given, where C # scripts are stored as text files, then converted to byte bytes, and the type and its methods are obtained through reflection technology. Theoretically, of course, no problem, but we know that because iOS is a closed system, designers are not allowed to use reflection technology under this platform for security reasons. So the problem is, reflection is not a perfect solution. About the reflection technology to achieve Unity3d thermal update, you can refer to this article: http://blog.csdn.net/janeky/article/details/25923151. Well, let's talk about how the blogger is implementing Unity3d's Hot update through LUA. We know that there is a dostring () method in the C # interface provided by LUA that can execute scripts in a string directly from a LUA virtual machine. So, we can execute the commands in the script by reading the Lua script locally, and if the commands in our script can manipulate the Unity3d directly, then we can update the code logic in the game with the Lua script. So how can we let the Lua script manipulate Unity3d? In the previous article, we introduced a require approach that introduces a C # library into a LUA script and executes the methods in the C # Library through Lua. Along the way, bloggers have the following ideas:


In this scenario, we first need to encapsulate the unity API into a C # class library Where we will involve dynamic loading scenarios and dynamic creation scenarios, as we will use these methods when we update the logic of the game. These methods can be referenced by require in LUA scripts after encapsulation, so that we can design them dynamically through the Lua script. We designed a fixed location to store the Lua script update file, so we just need to compare the local version to the server version to see if we need to update. Here we use the WWW to download the latest LUA script update files from the remote server, the downloaded LUA script is outside the project and we cannot load it using Resource.load (), but we can load a local file via www. This allows us to update the Lua script file. Of course, we can use Assetbundle to update the Lua script file, but Bo Master's free version does not support Assetbundle, so bloggers came up with such a curve to the salvation of the method. When the Lua script file is updated, we can execute the code in the script file through the Dostring () method in the main game logic. The main task in the main logic of the game is to compare the current version number with the server version number to determine if an update is required, download the LUA script update file if necessary, and then execute the code in the script so that we can implement the client program update. Well, let's go on to the project in a previous article as an example to turn this idea of a blogger into a reality.

First, we add the following two methods to the CSharpLib.cs class and complete the registration of the methods:

<summary>///set the coordinates of the object in the scene///</summary>///<returns> Returns the current coordinate </returns>///<param name= " Lua ">lua.</param>public static int setposition (Iluastate lua) {//object name string Mname=lua. L_checkstring (1);//Incoming parameter x,y,zfloat mx= (float) Lua. L_checknumber (2); float my= (float) Lua. L_checknumber (3); float mz= (float) Lua. L_checknumber (4);//Get Object Gameobject go=gameobject.find (mname);//Get Transformtransform mtrans=go.transform;// Sets the game body position mtrans.position=new Vector3 (MX,MY,MZ);//Returns the game body current coordinates lua. Pushnumber (mtrans.position.x); Lua. Pushnumber (MTRANS.POSITION.Y); Lua. Pushnumber (MTRANS.POSITION.Z); return 3;} <summary>///create an object using a local preset///</summary>///<returns>the resource.</returns>///<param Name= "LUA" >lua.</param>public static int Createresource (Iluastate lua) {//Incoming resource name string Mname=lua. L_checkstring (1);//load Local resources Gameobject go= (gameobject) resources.load (mname);//Incoming coordinate parameter x,y,zfloat mx= (float) Lua. L_checknumber (2); float my= (float) Lua. L_checknumber (3); float mz= (float) Lua.    L_checknumber (4);//Create a new object Object.instantiate (Go,new Vector3 (MX,MY,MZ), quaternion.identity); Returns the name of the object Lua. Pushstring (Go.name); return 1;}
Okay, so we're done with a simple C # class library, let's add a way to update the script in the main logic code updatescripts ():

void Updatescript () {startcoroutine ("Download");} <summary>///download Lua script update file///</summary>ienumerator Download () {//Load LUA script update file locally, assuming the file has been downloaded from the server www _ Www=new WWW (Mupdatefilespath); yield return _www;//Read Server version mlua.l_dostring (_www.text);}

The code logic here is simple, which is to read the script to update the local file and execute the script, where Mupdatefilepath is the script update file path:

Initialize the path mupdatefilespath= "File://d:\\lua_update.txt";

The blogger assumes that a version number is stored locally and gets the server-side version number before each update, and if the two version number is different, the update script file needs to be downloaded from the server. But the blogger here didn't think of any good way to get the version number, so here just write the update. So, let's see what the update script file does!

Local Csharplib=require "CSharpLib.cs" csharplib. SetPosition ("Cube", 2,1,0) csharplib. Createresource ("Sphere", 0,0,0) csharplib. Createresource ("Cube", 1,1,0)
First we introduced the CSharpLib.cs class library through require, then we set the location of the object named cube in the scene to (2,1,0), and created a cube and a sphere with the local two prefab resources. So, can our vision be fulfilled? Let's look at the final effect together!

Before executing the LUA script update:


After executing the LUA script update:



As we wish, the Lua script has successfully implemented an update to the scene. Maybe some friends will ask, this is the use of local resources, if I want to use the resources on the server what to do? The answer is the last Assetbundle the blogger would like to mention, which is to use Assetbundle to load remote resources and then implement updates with Lua, which can be added to the Csharplib class library. We can imagine that if one day we can encapsulate all of unity's methods, then we can use LUA to create the scene, if you want to update the client, just replace the Lua file is OK, how is it simple? But unity is not open source Ah, these ideas are just ideas. Well, today's content is this, welcome everyone to pay attention to my blog, thank you!


Daily Proverbs: Mature, not your face, look how sophisticated, not you know how many cardinal, know how many sermon, but you can understand the little things happening around you may have its last resort.




--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------

Like my blog Please remember my name: Qin Yuanpei , my blog address is Blog.csdn.net/qinyuanpei.

Reprint please indicate the source, this article Qin Yuanpei , this article source: http://blog.csdn.net/qinyuanpei/article/details/40213439

--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------

  

Unity3d game development of Lua and game of the indissoluble end: Unilua Hot Update full interpretation

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.