Unity HybridCLR(Huatuo)基本配置和简易热更新(一)

 来自HybridCLR官网:

关于HybridCLR (code-philosophy.com)https://hybridclr.doc.code-philosophy.com/#/README

 可以自行学习查看

首先在Unity添加 HybridCLR

细心的可以看到官网有这个下载网站   怕你们找不到我就放在下面了

https://gitee.com/focus-creative-games/hybridclr_unity.git

 如果添加后有git的报错建议去git官方网站下载个最新版本  下载后要重启电脑 !!!

接下来开始配置 

 

 

 

点击之后就会发现多了热更新的文件夹 

接下来开始是代码部分

public class ABData
{
    public AssetBundle ab;
    public int num;
}

这里用到了泛型单利  不懂的自行跳转到本人的泛型单利: 

实现泛型单例模式C#_慕容鑫非的博客-CSDN博客

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Script
{
    public class ScheduleOnce : UnitySingletn<ScheduleOnce>
    {
        private int id = 0;
        
        private Dictionary<int, Coroutine> m_Dictionary = new Dictionary<int, Coroutine>();

        public void AddSchedule(float time, Action<object> action,params object[] arr)
        {
            int name = id++;
            Coroutine coroutine = StartCoroutine(DelayFun(name, time, action, arr));
            m_Dictionary.Add(name,coroutine);
        }

        public void AddSchedule(float time, Action<object> action)
        {
            int name = id++;
            Coroutine coroutine = StartCoroutine(DelayFun(name, time, action));
            m_Dictionary.Add(name,coroutine);
        }
        
        IEnumerator DelayFun(int i, float time, Action<object> action,params object[] arr)
        {
            throw new NotImplementedException();
        }
        
        IEnumerator DelayFun(int i, float time, Action<object> action)
        {
            throw new NotImplementedException();
        }
    }
}

 

public class ABManager :Singleton<ABManager>
{
    public Dictionary<string,List<string>> reflist= new Dictionary<string,List<string>>();
    public string SPath = Application.streamingAssetsPath + "/";
    public Dictionary<string, byte[]> assetDatas = new Dictionary<string, byte[]>();
    public Dictionary<string,ABData> abDic= new Dictionary<string,ABData>();
    public byte[] GetAssetData(string dllName)
    {
        return assetDatas[dllName];
    }
    public AssetBundle LoadAB(string dllName)
    {
        return AssetBundle.LoadFromMemory(GetAssetData(dllName));
    }
    public T Load<T>(string refname,string abName, string assetName) where T : UnityEngine.Object
    {
        AssetBundle ab;
        if(abDic.ContainsKey(abName))
        {
            ab = abDic[abName].ab;
            abDic[abName].num++;
        }
        else
        {
            ab=AssetBundle.LoadFromFile(SPath+ abName);
            ABData data=new ABData();
            data.ab = ab;
            data.num = 1;
            abDic.Add(abName, data);
        }
        if(ab!=null)
        {
            if(!reflist.ContainsKey(refname))
            {
                reflist.Add(refname,new List<string>());
            }
            reflist[refname].Add(abName);
        }
        return ab.LoadAsset<T>(assetName);
    }
    public GameObject Load(string abName,string assetName)
    {
        AssetBundle ab;
        if(abDic.ContainsKey(abName))
        {
            ab = abDic[abName].ab;
            abDic[abName].num++;
        }
        else
        {
            ab = AssetBundle.LoadFromFile(SPath + abName);
            ABData data=new ABData();
            data.ab = ab;
            data.num = 1;
            abDic.Add(abName, data);
        }
        if (ab != null)
        {
            if (!reflist.ContainsKey(assetName))
            {
                reflist.Add(assetName, new List<string>());
            }
            reflist[assetName].Add(abName);
        }
        return ab.LoadAsset<GameObject>(assetName);
    }
    public Type LoadScript(string assetName)
    {
        Assembly ass=Assembly.Load(GetAssetData("Script.dll.bytes"));
        return ass.GetType(assetName);
    }
    public void Release(string refname)
    {
        if(reflist.ContainsKey(refname))
        {
            foreach (var abName in reflist[refname])
            {
                if(abDic.ContainsKey(abName))
                {
                    abDic[abName].num--;
                    if (abDic[abName].num <= 0)
                    {
                        ScheduleOnce.Ins.AddSchedule(9, (obj) =>
                        {
                            object[] arr = obj as object[];
                            ABData data = arr[0] as ABData;
                            if (data.num <= 0)
                            {
                                data.ab.Unload(true);
                                abDic.Remove(abName);
                                Debug.Log("释放成功");
                            }
                        }, abDic[abName]);
                    }
                }
            }
            reflist.Remove(refname);
        }
    }

using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;

public class LoadDll : MonoBehaviour
{
    void Start()
    {
        StartCoroutine(this.DownLoadAssets(() =>
        {
                                            //程序集dll文件    添加的脚本
            Instantiate(ABManager.Ins.Load("Script.dll.bytes", "Test"));
        }));
    }
    IEnumerator DownLoadAssets(Action action)
    {
        //找到对应目录
        DirectoryInfo direction=new DirectoryInfo(Application.streamingAssetsPath);
        //获取目录下所有文件
        FileInfo[] files= direction.GetFiles("*",SearchOption.AllDirectories);
        //把所有dll文件拷贝到s目录下
        foreach (var item in files)
        {
            if(item.Name.EndsWith(".bytes"))
            {
                string dllPath = item.FullName.Replace("\\", "/");
                UnityWebRequest www=UnityWebRequest.Get(dllPath);
                yield return www.SendWebRequest();
                if(www.result!=UnityWebRequest.Result.Success)
                {
                    Debug.Log(www.error);
                }
                else
                {
                    byte[] data = www.downloadHandler.data;
                    ABManager.assetDatas.Add(item.Name, data);
                    Debug.Log(item.Name);
                }
            }
        }
        action();
    }
}
using UnityEngine;

public class Test : MonoBehaviour
{
    void Start()
    {
        GameObject game=GameObject.CreatePrimitive(PrimitiveType.Cube);

        Debug.Log("热更完毕");
    }
}

 

 HybridCLR(Huatuo)简易的更新就搞定了

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
配置HybridCLR更新,可以按照以下步骤进行操作: 1. 新建一个Unity项目。 2. 安装HybridCLR插件。可以通过导入hybridclr_trial官方示例中的包来安装。 3. 配置PlayerSettings,确保项目设置正确。 4. 安装HybridCLR插件。点击菜单栏的"HybridCLR",选择"Installer...",进行安装。 5. 导入两个工具库。将com.gwiazdorrr.betterstreamingassets和Unity-Logs-Viewer文件夹拷贝到项目的Packages文件夹中。 6. 创建Hotfix.dll。按照HybridCLR的文档指导,创建Hotfix.dll文件。 7. 配置HybridCLR。根据需要,设置更新的相关参数。 8. 安装YooAsset插件。根据需要,安装YooAsset插件。 9. 修改LoadDll.cs文件。按照HybridCLR的文档指导,对LoadDll.cs文件进行修改。 10. 配置YooAsset。根据需要,对YooAsset进行配置。 11. 进行"Generate/All"和"Build"操作。根据HybridCLR的文档指导,进行代码生成和构建操作。 12. 验证配置是否成功。运行项目,验证更新功能是否正常工作。 请注意,以上步骤仅为一般配置操作的示例,具体的配置过程可能因HybridCLR插件的版本而有所不同。建议参考HybridCLR的官方文档和指南,以获取最新的配置信息和步骤。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [HybridCLR 更新配置](https://blog.csdn.net/weixin_43830069/article/details/129855550)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值