Unity3D game development-Lua and the endless fate of games final: UniLua hot update full interpretation, unity3dunilua

Source: Internet
Author: User

Unity3D game development-Lua and the endless fate of games final: UniLua hot update full interpretation, unity3dunilua

Certificate -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

If you like my blog, please remember my name: Qin Yuanpei. My blog address is blog.csdn.net/qinyuanpei.

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

Certificate -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Hello everyone, I'm Qin Yuanpei. Welcome to follow my blog. My blog address is blog.csdn.net/qinyuanpei. In the previous three series of articles "Unity3D Game Development's Lua and game's indissoluble bond", the blogger guides everyone through the powerful and fascinating role of Lua in game development, through the UniLua open-source project, we introduced Lua into the Unity3D world and wrote the first example program for interaction between lua' and Unity3D. Today, let's talk about Unity3D with AssetBundle and; Lua for hot update.

First, let's take a look at what is hot update! Hot update refers to changes to the system without stopping services, for example, in Windows, patches can be updated without restarting, Web servers can be replaced with data and files without restarting, and other classic instances are hot-updated. So what is hot update for Unity3D? If our final Unity3D game is a Web game, the resource code update is a hot update during each game loading process. If the Unity3D game we finally released is a client game, we will update the resource code after restarting the client. Why? Because Web games need to ensure that players can quickly enter the game in time, we must update the game resources and code before loading the game. However, for client games, players can enter the game after this update is complete, and most client programs require players to restart the client after the update is complete. Therefore, for client games, hot Update Is Not A strict hot update. So why do we need to perform hot updates? The answer is to shorten the process for users to obtain new versions of clients and improve user experience. This is actually the problem that the blogger mentioned earlier that traditional stand-alone games rely on the disc carrier for release. In order to get the latest version of the game, you need to download a new client and install it on your computer or mobile phone. There is a concept in Internet product development called rapid iteration. Imagine that if we make some adjustments to the client each time, players need to download the new version of the client, is such a user experience really satisfying? So now, in order to make it easier for users to retain users and make money from them, we can always find the shadows of hot updates in Game Products. We know that in Unity3D, we can use AssetBundle to update Resources in the game. Now, we will officially start the Unity3D code-level hot update journey!

The official API of Unity provides a reflection-based Approach to store C # scripts as text files and convert them into byte bytes, this type and its method are obtained through reflection technology. Theoretically, there is no problem, but we know that because IOS is a closed system, the designers are not allowed to use reflection technology on this platform for security reasons. The problem arises, and reflection is not a perfect solution. For details about the reflection technology to achieve hot update of Unity3D, refer to this article: http://blog.csdn.net/janeky/article/details/25923151. Well, let's talk about how the bloggers use Lua to implement hot update of Unity3D. We know that there is a DoString () method in the C # interface provided by Lua. This method can directly use the Lua Virtual Machine to execute the script in the string. Therefore, we can execute the commands in the script by reading the Lua script locally. If the commands in the script can be directly operated on Unity3D, then we can use the Lua script to update the code logic in the game. So how can we allow the Lua script to operate Unity3D? In the previous article, we introduced a Require method, which can introduce the C # library into the Lua script and use Lua to execute the methods in the C # library. Following these ideas, the blogger has the following ideas:


In this idea, we first need to encapsulate the Unity API into a C # class library. In this class library, we will involve dynamic loading and dynamic creation scenarios, because these methods will be used when we update the game logic. After these methods are encapsulated, We can reference them in the Lua script using the Require method, and then we can dynamically design them using the Lua script. We designed a fixed location to store the Lua script Update file, so that we only need to compare the local version and server version to know if we need to update. Here we use WWW to download the latest Lua script Update file from the remote server. The downloaded Lua script is outside the project and cannot use Resource. load () method to Load, but we can use WWW to Load a local file, so that we can update the Lua script file. Of course, we can use AssetBundle to update the Lua script file. However, the free version of the blogger does not support AssetBundle, so the blogger came up with a way to save the country. After the Lua script file is updated, we can use the DoString () method in the game's main logic to execute the code in the script file. In the main logic of the game, the main task is to compare the current version number and server version number to determine whether an update is required. If an update is required, download the Lua script Update file and execute the code in the script, in this way, the client program is updated. Now, let's take the project in the previous article as an example to turn this idea of the blogger into reality.

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

/// <Summary> /// set the coordinates of objects in the scenario /// </summary> /// <returns> returns the current coordinates </returns> /// <param name = "lua"> Lua. </param> public static int SetPosition (ILuaState lua) {// Object Name string mName = lua. rochelle checkstring (1); // input parameters x, y, zfloat mX = (float) lua. rochelle checknumber (2); float mY = (float) lua. rochelle checknumber (3); float mZ = (float) lua. rochelle checknumber (4); // get object GameObject go = GameObject. find (mName); // obtain TransformTransform mTrans = go. transform; // set the location of the game body, mTrans. position = new Vector3 (mX, mY, mZ); // returns the current coordinate lua of the game body. pushNumber (mTrans. position. x); lua. pushNumber (mTrans. position. y); lua. pushNumber (mTrans. position. z); return 3 ;}//< summary> /// use a local preset to create an object /// </summary> /// <returns> The resource. </returns> // <param name = "lua"> Lua. </param> public static int CreateResource (ILuaState lua) {// input the Resource Name string mName = lua. rochelle checkstring (1); // load the local resource GameObject go = (GameObject) Resources. load (mName); // input coordinate parameters x, y, zfloat mX = (float) lua. rochelle checknumber (2); float mY = (float) lua. rochelle checknumber (3); float mZ = (float) lua. rochelle checknumber (4); // creates an Object. instantiate (go, new Vector3 (mX, mY, mZ), Quaternion. identity); // returns the object name lua. pushString (go. name); return 1 ;}
Now we have completed a simple C # class library. Next we will add a method for updating the script in the main logic code UpdateScripts ():

Void UpdateScript () {StartCoroutine ("Download") ;}/// <summary> // Download the Lua script Update file /// </summary> IEnumerator Download () {// load the Lua script Update file locally. Assume that the file has been downloaded from the server WWW _ WWW = new WWW (mUpdateFilesPath); yield return _ WWW; // read the server version mLua. rochelle dostring (_ WWW. text );}

The Code logic here is very simple: read the script to update the local file and execute the script. The mUpdateFilePath is the path of the script Update file:

// Initialization path: mUpdateFilesPath = "file: // D: \ lua_update.txt ";

Here, the blogger assumes that a local version number is stored. The server version number is obtained before each update. If the two versions are different, the updated script file needs to be downloaded from the server for updates. However, the blogger did not come up with any good methods to obtain the version number, so only updates are written here. So let's take a look at what has been done to update the script file!

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 CSharpLib through Require. cs class library. Next, we will set the position of the object named Cube in the scenario to (, 0), and create a Cube and a Sphere using two local Prefab resources. So can our ideas be realized? Let's take a look at the final effect!

Before executing the Lua script update:


After executing the Lua script update:



As we wish, the Lua script successfully updated the scenario. Some may ask, what if I want to use resources on the server? The answer is the AssetBundle that the blogger does not want to mention, that is, to use AssetBundle to load remote resources and then implement updates using Lua. These logics can be added to the CSharpLib class library. You can imagine that if one day we can encapsulate all the methods of Unity, we can directly use Lua to create a scenario. If we want to update the client, you only need to replace the Lua file. How can this problem be solved? But Unity is not open-source. After all, these ideas are just ideas. Well, today's content is like this. You are welcome to pay attention to my blog. Thank you!


Daily proverbs: maturity is not an old saying of your face. It is not a saying of what you know, how much you know, how much you know, but how much you can understand what is happening around you.




Certificate -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

If you like my blog, please remember my name: Qin Yuanpei. My blog address is blog.csdn.net/qinyuanpei.

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

Certificate -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------





Unity3D game development instances, source code recommendation books, or download Source Code addresses

Unity 3D Game Development

What are the mainstream 3d Game Development engines? Currently I am learning Unity3d

It is not recommended to learn about impermanence. If you do not use it for free, you will receive the layer after commercial use. It is not cost-effective at all. We recommend that you learn untiy3d, because unity3d will not be expensive even if it is commercially available in the future, only one-time fee is charged! Besides, big companies all have their own engines. small companies do not like to use such expensive engines, so they still have better prospects for untiy3d!

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.