Unity3D Android动态反射加载程序集
2017-08-09 10:34
471 查看
这种办法在iOS下是不让用的,只能在Android下用。用起来也很方便了。
1、先创建一个c#工程,引用到的UnityEngine.dll在Unity的安装目录里找吧
2、将编译的dll放入Unity工程,并打成assetBundle。(要把缀名改成.bytes,这个类型Unity才识别,才能打成assetbundle)
打bundle代码
右键点资源,就有BuildAsset
bundle就会生成StreamingAssets里
3、写测试代码
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()); } } }
相关文章推荐
- Android中利用“反射”动态加载R文件中的资源
- c#中如何利用反射动态加载程序集
- Android 反射机制动态加载模块
- Android-动态加载插件化的两种实现方式(一):反射
- 应用程序配置和动态加载5----反射程序集
- 应用程序配置和动态加载5----反射程序集(转载)
- 反射动态加载类库时出现未能加载文件或程序集的解决方法
- Android 利用反射实现不安装直接运行APK(动态加载)
- 动态加载外部程序集并用反射获取指定类型的信息
- Unity3D:NGUI UIGrid 排序、动态加载与销毁
- Android apk动态加载机制的研究(二)
- Android静态安全检测 -> 代码动态加载安全检测
- android左右滑动加载分页以及动态加载数据
- 美团Android DEX自动拆包及动态加载简介
- Unity3D实现动态加载游戏资源
- android: 静态XML和动态加载XML混合使用,以及重写Layout控件
- Android 4.0 如何动态加载ko
- Android热修复(动态加载)方案汇总
- Android动态加载——加载已安装APK中的类
- c#使用反射加载程序集Assembly。load()使用全过程