加载AB包程序集,反射获取并实例化类调用方法

 加载AB包文件,加载bytes程序集资源,通过反射获取类,实例化添加组件,调用方法

public class LoadAB : MonoBehaviour
{
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.H))
        {
            Load();
        }
    }
    void Load()
    {
        string classname = "ID506_BrakeFiveStep";
        string RealClassName = "DriveEventAbstractParentClassNameSpace." + classname + "_AB";
        string Dll_bytesName = classname + ".bytes";

        string path = "D:/Desktop/newcode/AssetBundles/DriveEventAssests/id506_brakefivestep";
        AssetBundle ab = AssetBundle.LoadFromFile(path);
        ab.LoadAsset(Dll_bytesName);

        //将.bytes文件转化为TextAsset
        TextAsset VS_Dll_To_bytes = ab.LoadAsset(Dll_bytesName, typeof(TextAsset)) as TextAsset;
        //转化为byte数组
        byte[] by = VS_Dll_To_bytes.bytes;
        //通过反射获取想要的类
        Assembly am = Assembly.Load(by);
        Type type = am.GetType(RealClassName);

        //实例化这个类
        object obj;
        if (GetComponent(type) == null)
        {
            gameObject.AddComponent(type);
        }
        obj = GetComponent(type);

        //直接调用
        string methodName = "InitAB";
        System.Object[] paras = new System.Object[] { "{'EventID':'506','Color':'yellow','OffsetMetre':'0'}" };
        MethodInfo method = type.GetMethod(methodName);
        method.Invoke(obj, paras);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值