Unity3D Android動態反射載入程式集

來源:互聯網
上載者:User

標籤:ref   collectd   button   程式   select   .com   dep   lte   cap   

這種辦法在iOS下是不讓用的,只能在Android下用。用起來也很方便了。

 

1、先建立一個c#工程,引用到的UnityEngine.dll在Unity的安裝目錄裡找吧

 

2、將編譯的dll放入Unity工程,並打成assetBundle。(要把綴名改成.bytes,這個類型Unity才識別,才能打成assetbundle)

 

打bundle代碼

#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 = Application.streamingAssetsPath + "/" + o.name + ".unity3d";if (!BuildPipeline.BuildAssetBundle(o, null, tar, BuildAssetBundleOptions.CollectDependencies,BuildTarget.Android)){Debug.Log(tar);}}AssetDatabase.Refresh();}#endif

 

右鍵點資源,就有BuildAsset

 

 

bundle就會產生StreamingAssets裡

 

3、寫測試代碼

using System.Collections;using System.Collections.Generic;#if UNITY_EDITORusing UnityEditor;#endifusing UnityEngine;public class TestGame : MonoBehaviour{    // Use this for initialization    void Start ()    {            }        // Update is called once per frame    void Update () {            }    IEnumerator Load()    {#if UNITY_EDITOR        var path = "file://" + Application.streamingAssetsPath + "/" + "ReflectTest.dll.unity3d";#else#if UNITY_ANDROID        var path = "jar:file://" + Application.dataPath + "!/assets/" + "ReflectTest.dll.unity3d";#elif UNITY_IOS        var path = Application.dataPath + "/Raw/"+"ReflectTest.dll.unity3d";#endif#endif        //var path = "file://"+Application.streamingAssetsPath + "/" + "HelipadEscapeGame.unity3d";        Debug.Log("path="+path);        using (WWW www = new WWW(path))        {            yield return www;            var tex = www.assetBundle.LoadAsset<TextAsset>("ReflectTest.dll");            //var tex = www.assetBundle.LoadAsset<TextAsset>("HelipadEscapeGame");                        var ass = System.Reflection.Assembly.Load(tex.bytes);            var type = ass.GetType("Class1");             gameObject.AddComponent(type);        }                }#if UNITY_EDITOR    [MenuItem("Assets/BuildAsset")]    static void BuildAsset()    {        var se = Selection.GetFiltered(typeof (Object), SelectionMode.DeepAssets);        foreach (var o in se)        {            string sp = AssetDatabase.GetAssetPath(o);            string tar = Application.streamingAssetsPath + "/" + o.name + ".unity3d";            if (!BuildPipeline.BuildAssetBundle(o, null, tar, BuildAssetBundleOptions.CollectDependencies,BuildTarget.Android))            {                Debug.Log(tar);            }        }        AssetDatabase.Refresh();            }#endif    void OnGUI()    {        if (GUI.Button(new Rect(0, 0, 200, 200), "Load"))        {            StartCoroutine(Load());        }    }}

 

Unity3D Android動態反射載入程式集

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.