Unity android down DLL hot update one (core idea)

Source: Internet
Author: User

We all know that a hot update is the first choice is Ulua this plug-in, in fact, unity can use DLL hot update, if you really do not want to use LUA to write logic, 0.0 see Dll+assetbundle How to implement the hot update. After you have finished reading this article Just know the way and concept of DLL hot update, master the hot update of the actual combat framework also need your own =. =

Our usual practice is to compile the DLL into a Assetbundle file, unity via www download ab file to get inside the DLL. By reflecting the C # component inside the Gameobject game object , This is the principle of DLL hot update. Assuming the project uses the Ugui system , let's look at the core issues that are encountered when writing code through the above ideas.

    1. How do I need to write a DLL?
    2. How can my DLL get a reference to the Unityengine namespace class?
    3. How does my DLL need to be called Assetbundle?
    4. Program Download Assetbundle How to read inside DLL?
    5. How do I bind a script to implement a hot update UI?
I need how to write DLLs?

First you need to find unity several key DLLs. UnityEngine.dll,UnityEngine.UI.dll for you to write the hot update code there is a reference to the Unityengine core class. First create a c#3.5 version of the class library (click the project Right- Properties, Target framework 3.5), and then introduce the DLLs for UnityEngine.dll and UnityEngine.UI.dll

After the project is created, we write a MyClass inheriting the Monobehaviour class

Click Start compile DLL, compiled DLL can open folder in File Explorer in Project right-->btn directory->release and debug directory Find MyDll.dll (mine is in Realse directory) We put the compiled DLL, Get unity inside to pack into assetbundle files. Unity By default does not seem to allow the suffix DLL into the assetbundle, we modified to change the MyDll.dll to Mydll.bytes name

Package the DLL into the Assetbundle file code as follows,

[MenuItem ("assets/build Android DLL")]     Public Static voidBuildassetsandroiddll () {Object Mainasset= Assetdatabase.loadmainassetatpath ("assets/dll/mydll.bytes"); Buildpipeline.buildassetbundle (Mainasset,NULL, Application.datapath +"\\Dll\\myassets.android", buildassetbundleoptions.none,buildtarget.android); if(File.exists (Androidstreamurl)) {file.delete (Androidstreamurl); }           //Copy to stream directory for easy program Download AB fileFile.Copy (Application.datapath +"\\Dll\\myassets.android", Androidstreamurl); }

Take a look at what unity needs to do to create a new scene and add text to it.

For Android under the packaged Assetbundle file, put in Streamassetbundleassets, the code will automatically help you copy the past

Test Core class

usingUnityengine;usingSystem.Collections;usingSystem.Reflection;usingSystem;usingSystem.IO;usingUnityengine.ui; Public classTest:monobehaviour { Public stringURL;  Public stringstr;  PublicText Myagetext;  Public voidAwake () {application.logmessagereceived+=application_logmessagereceived; }    voidApplication_logmessagereceived (stringConditionstringstackTrace, LogType type) {STR+="\ r \ n"+condition; }     Public voidOngui () {GUI. Box (NewRect (0,0, -, -), str); }    //Use this for initializationIEnumerator Start () {yield return NewWaitforseconds (2); if(Application.platform = =runtimeplatform.windowseditor) {URL=@"file://"+application.datapath +"\\Dll\\myassets.windows"; }Else         if(Application.platform = =runtimeplatform.android) {URL= Application.streamingassetspath +"/myassets.android"; }        Else if(Application.platform = =runtimeplatform.iphoneplayer) {URL= Application.streamingassetspath +"/myassets.iphone"; } Debug.Log ("URL:"+URL); www www=NewWWW (URL); yield returnwww; if(Www.error! =NULL) {Debug.Log ("Loading Error"); }        if(Www.isDone) {Debug.Log ("Load Complete"); Assetbundle AB=Www.assetBundle; Try            {                //First, the DLL is textasset type out, in the bytes to Assembly.Load method read ready to enter the reflection OperationAssembly Aly =System.Reflection.Assembly.Load (((Textasset) www.assetBundle.mainAsset). bytes); //get all types under a DLL                foreach(varIinchAly. GetTypes ()) {//Debug CodeDebug.Log (i.name); STR+="\ r \ n"+I.name; //Add components to the current gameobject belowComponent C = This. gameobject.addcomponent (i); //then the class name is MyClass, and the text reference is assigned to the Myclass.platefacetext property.                    if(I.name = ="MyClass") {FieldInfo info= C.gettype (). GetField ("Platefacetext"); Info.                    SetValue (c, Myagetext); }                }            }            Catch(Exception e) {Debug.Log ("Error Loading DLL");            Debug.Log (E.message); }        }    }}

View under Windows

The above is just a source of hope to use the DLL Hot update code can help you.

Project: Http://yunpan.cn/cmE4eL5948ghQ access password df6b

Unity android down DLL hot update one (core idea)

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.