huatuo(hybridclr)热更新踩坑笔记

        个人尝试的结果,不一定为正规的操作,若观者有更好的方案,望赐教。

        1.第三方框架应该放在哪里?

        以热更框架为例,入口函数进入后,需要调用热更代码检查资源,更新资源,加载程序集。测试1:把热更框架放到Assembly-CSharp.dll(主工程)中,会发现其他的热更asmdef无法引用热更框架代码,结果失败。测试2:把热更框架单独生成asmdef放到热更asmdef列表,然后其他热更asmdef进行引用,主工程生成admdef也对其进行引用,结果打包时报错提示主工程引用了引用工程,结果失败。测试3:依然把热更框架单独生成asmdef,但是不放在热更asmdef列表,而是放在Assembly-CSharp文件夹,然后其他热更资源再对其进行引用,此时因为热更框架本身就在Assembly-CSharp文件夹,所以主工程不需要再设置成asmdef,可以直接使用,结果成功!

        2.Could not load type 'XxxType' from assembly 'yyyAssembly'

        官方文档已经给了几个解决方式,这里主要说下 “yyyAssembly是热更新assembly” 这种情况,使用场景是,在A的asmdef中调用B的asmdef中的类,此时A是需要引用B的,以以下代码为例,其中hotfixtest脚本位于A的asmdef,hotupdate3test位于B的asmdef。

public class HotFixTest : MonoBehaviour
{
    void Start()
    {
        Debug.Log($"---?????HotFixTest?????---    {gameObject.name}");

        Model.Assetbundle.AssetbundleLoader.I.Load("dlls/hotupdatedlls/hotupdate3.dll.ab", (holder) =>
        {
            GameObject go = new GameObject("HotUpdate3Test");
            go.AddComponent<HotUpdate3Test>();
        },
        (error) =>
        {
            Debug.Log(error.errorMessage);
        });
    }
}
public class HotUpdate3Test : MonoBehaviour
{
    void Start()
    {
        Debug.Log(" this is  hot update 3 ");
    }
}

        这样写就会报上面的错,Could not load type-----。是因为A依赖了B,那么在加载A(Assembly.Load)之前要先加载B。但实际情况是,A作为最先启动的热更Asmdef,加载代码是写在主项目里的,没办法热更代码,此时可以换一种方式,通过反射加载,代码示例如下,其中新加脚本HotUpdate3_1为B的asmdef中的另一个类,也可以加载到。

public class HotFixTest : MonoBehaviour
{
    void Start()
    {
        Debug.Log($"---?????HotFixTest?????---    {gameObject.name}");

        Model.Assetbundle.AssetbundleLoader.I.Load("dlls/hotupdatedlls/hotupdate3.dll.ab", (holder) =>
        {
            Assembly hotUpdate3Ass = Assembly.Load(holder.LoadAsset<TextAsset>().bytes);
            
            Type entryType = hotUpdate3Ass.GetType("HotUpdate3Test");
            entryType.GetMethod("Test").Invoke(null, null);
        },
        (error) =>
        {
            Debug.Log(error.errorMessage);
        });
    }
}
public class HotUpdate3Test : MonoBehaviour
{
    void Start()
    {
        Debug.Log(" this is  hot update 3 ");
    }

    public static void Test()
    {
        Debug.Log(" this is  hot update 3 Test");
        GameObject go = new GameObject("HotUpdate3Test");
        go.AddComponent<HotUpdate3Test>();

        GameObject go2 = new GameObject("HotUpdate3_1");
        go2.AddComponent<HotUpdate3_1>();
    }
}
public class HotUpdate3_1 : MonoBehaviour
{

    void Start()
    {
        Debug.Log(" this is  hot update 3_1 ");
    }
}

        按照上面的写法就可以实现不打包,新增热更的asmdef了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值