吧脚本用DLL的方式打包起来,然后改成.bytes后缀进行打包AB ,然后代码进行读取脚本进行反射
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using UnityEngine.UI;
public class JumpScene : MonoBehaviour
{
public string str;
public Text myAgeText;
private IEnumerator LoadAssetBundle()
{
//Debug.Log(System.DateTime.Now.ToString("yyyyMMddHHmmss")+Int64.Parse(DateTime.Now.ToString("yyyyMMddHHmmss")));
//动态版本号:(int)Int64.Parse(DateTime.Now.ToString("yyyyMMddHHmmss"))
//网络加载 先用固态版本号 加入改动ab包 要手动更改目前是版本号1
WWW WWW = WWW.LoadFromCacheOrDownload(Application.streamingAssetsPath + "/" + "tookit.ab", (int)Int64.Parse(DateTime.Now.ToString("yyyyMMddHHmmss")));
//WWW obj = new WWW(Application.streamingAssetsPath + "/" + lodeAssetBundleName[i]);
yield return WWW;
if (WWW == null)
{
Debug.LogWarning("加载失败");
}
else
{
AssetBundle ab = WWW.assetBundle;
Debug.Log(WWW.assetBundle);
Debug.Log((TextAsset)getMainAsset(ab));
Debug.Log(((TextAsset)getMainAsset(ab)).bytes);
//先把DLL以TextAsset类型取出来,在把bytes给Assembly.Load方法读取准备进入反射操作
Assembly aly = System.Reflection.Assembly.Load(((TextAsset)getMainAsset(ab)).bytes);
//获取DLL下全部的类型
foreach (var i in aly.GetTypes())
{
//调试代码
Debug.Log(i.Name);
str += "\r\n" + i.Name;
//添加组件到当前GameObject下面
Component c = this.gameObject.AddComponent(i);
//然后类名是MyClass,就把文本引用赋值给MyClass.platefaceText属性.
if (i.Name == "MyClass")
{
FieldInfo info = c.GetType().GetField("platefaceText");
info.SetValue(c, myAgeText);
}
}
Debug.LogWarning("成功读取" + "tookit.ab");
yield return new WaitForEndOfFrame();
ab.Unload(false);
}
}
public static UnityEngine.Object getMainAsset(AssetBundle assetBundle)
{
return assetBundle.LoadAsset(assetBundle.GetAllAssetNames()[0]);
}
}