zoukankan      html  css  js  c++  java
  • Unity3D Android动态反射加载程序集

    这种办法在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_EDITOR
    using UnityEditor;
    #endif
    using 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());
            }
        }
    }
  • 相关阅读:
    火爆全网的合成大西瓜小游戏魔改版大全
    [Qt]cmake下Qt隐藏console的窗口
    c# WebBrowser控制台输出执行js后的网页内容
    好的编程习惯是减少bug最有效的方法
    创建线程 出现SIGSEGV crash
    linux下进程创建/僵尸进程/孤儿进程
    C++实现不可被继承的类
    程序并发概述
    C++ vector实现原理
    C++深拷贝和浅拷贝
  • 原文地址:https://www.cnblogs.com/mrblue/p/7323896.html
Copyright © 2011-2022 走看看