来自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;
}
这里用到了泛型单利 不懂的自行跳转到本人的泛型单利:
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)简易的更新就搞定了