Unity 记载ab通过反射方式获取脚本

吧脚本用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]);
    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

山竹炒大蒜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值