Unity3d Android Dynamic Reflection loading assemblies

Source: Internet
Author: User

This method is not allowed under iOS and can only be used under Android. It is also very convenient to use.

1, first create a C # project, the reference to the UnityEngine.dll in Unity's installation directory to find it

2, put the compiled DLL into Unity Project, and play into Assetbundle. (To change the prefix name to. Bytes, this type of unity is recognized before it can be called assetbundle)

Play Bundle code

#if Unity_editor[menuitem ("Gameobject/buildasset")]static void Buildasset () {var se = selection.getfiltered (typeof ( Object), Selectionmode.deepassets), foreach (Var o in se) {string sp = Assetdatabase.getassetpath (o); string tar = Applicati On.streamingassetspath + "/" + O.name + ". Unity3d"; Buildpipeline.buildassetbundle (o, null, tar, buildassetbundleoptions.collectdependencies,buildtarget.android)) { Debug.Log (TAR);}} Assetdatabase.refresh ();} #endif

 

Right-click Resources, there is buildasset

 

Bundles will generate streamingassets.

3. Write test code

usingSystem.Collections;usingSystem.Collections.Generic;#ifUnity_editorusingUnityeditor;#endifusingUnityengine; Public classtestgame:monobehaviour{//Use this for initialization    voidStart () {}//Update is called once per frame    voidUpdate () {} IEnumerator Load () {#ifUnity_editorvarPath ="file://"+ Application.streamingassetspath +"/"+"ReflectTest.dll.unity3d";#else#ifUnity_androidvarPath ="jar:file://"+ Application.datapath +"!/assets/"+"ReflectTest.dll.unity3d";#elifUnity_iosvarPath = Application.datapath +"/raw/"+"ReflectTest.dll.unity3d";#endif#endif        //var path = "File://"+application.streamingassetspath +"/"+" Helipadescapegame.unity3d ";Debug.Log ("path="+path); using(WWW www =NewWWW (path)) {            yield returnwww; varTex = www.assetbundle.loadasset<textasset> ("ReflectTest.dll"); //var tex = www.assetbundle.loadasset<textasset> ("Helipadescapegame");                        varTheSystem.Reflection.Assembly.Load (tex.bytes); varType =. GetType ("Class1");        Gameobject.addcomponent (type); }                }#ifUnity_editor[MenuItem ("Assets/buildasset")]    Static voidBuildasset () {varSE = selection.getfiltered (typeof(Object), selectionmode.deepassets); foreach(varOinchse) {            stringSP =Assetdatabase.getassetpath (o); stringtar = Application.streamingassetspath +"/"+ O.name +". Unity3d"; if(! Buildpipeline.buildassetbundle (O,NULL, Tar, buildassetbundleoptions.collectdependencies,buildtarget.android))            {Debug.Log (TAR);            }} assetdatabase.refresh (); }#endif    voidOngui () {if(GUI. Button (NewRect (0,0, $, $),"Load") {startcoroutine (Load ())); }    }}

Unity3d Android Dynamic Reflection loading assemblies

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.