Unity implementation of C # Thermal update scheme (i)

Source: Internet
Author: User

recently, we have studied how to implement the thermal update of C # in Unity, and have a preliminary understanding of the process and scenarios for the entire DLL hot update, which is written here for further in-depth investigation and solution selection. I. Dynamic loading and unloading of C # DLLssince the hot update, then is the dynamic loading of C # DLL, so the first step is to study how to implement DLL dynamic loading and unloading. in the CLR Via C #, for DLL loading is explained in detail, here is no longer to explain the whole process, simply speaking, in C # project, will generate a default program domain AppDomain, called Defaultappdomain Bar, On the basis of this program domain, we can load several different assemblies. In. NET, assemblies cannot be unloaded, but can be released together with the release of the program domain, so we can use the program domain to implement assembly (DLL) loading and deallocation.

The above theory comes from the CLR Via C #, with the following figure:

based on this theory, we can create multiple AppDomain in addition to Defaultappdomain, and then implement DLL loading and unloading based on AppDomain. Based on this, write related engineering tests, refer to a project on the web to further test, here is the original text, at the end of the relevant code to download:Thermal Upgrade of the programOn the basis of the original code, further construction. First, Build 4 class Library:

The default project is Mainserver, which sets the Module1 and Module2 build paths to the Mainserver bin so mainserver can load the latest Module1.dll/module2.dll (PS : The settings here are important, ignoring will make it impossible to load the latest DLL)Module1 and Module2 both add commonlib references in references to implement the Icalculater interface, each of which is implemented as:Module1:

Module2:

In this way, the Icalculater interface is implemented in two different class libraries, respectively, by adding and multiplying. In Mainserver's main program Portal:

First, on the basis of the default AppDomain, load the 2 AppDomain further, and then load the DLLs on the basis of the 2 program domains respectively. The results are as follows:

The entire procedure explains the entire execution process in detail, builds the AppDomain first, loads the DLLs, and then executes the methods inside. Then a new AppDomain loads the previously loaded DLLs, executes them again, and does not conflict with each other. So the AppDomain can be a pair of multiple DLLs, and a DLL can be loaded by multiple different AppDomain.

second, the load of test DLLs in UnityOn the basis of the first part, we further study how to implement DLL loading in unity, the basic procedure can refer to this article:Unity DLL implements hot update of course, the article is not a full implementation of the hot update, the implementation of Windows and Android platform, for the DLL file hot update. For iOS why not hot update, we will discuss later, first look at the hot update steps of Android and Windows DLL. 1. Create a new ClassLibrary (class Library) project, in which the corresponding classes and methods are implemented;2, the project is exported as a DLL;3. Change the DLL to bytes file and deposit it into the Streamingassets folder in Unity Project;4, in the project run time, read streamingassets DLL file, with Assembly.Load (byte[] bytes) method, the DLL file read out, and then perform related operations. The code for this step is:

For DLL files, the www.bytes is executed, and for assetbundle files, the Ab.mainasset conversion to Textasset is performed and further bytes is obtained. On both Windows and Android platforms, you'll get a screen output like this:

The nature of this program, and the nature of the front is not small, unity engineering in the implementation of the time, will build a default appdomain,assembly.load, in fact, is to load the DLL on this program domain, so the related substance and the previous part of the difference is not small, this is C # Apps for hot updates in unity (not included with iOS).

Below we will explain why iOS does not support hot updates for DLLs and how to use Ilruntime to implement hot updates for Android and iOS.

Unity implementation of C # Thermal update scheme (i)

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.