当前位置:   article > 正文

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

unity 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官方网站下载个最新版本  下载后要重启电脑 !!!

接下来开始配置 

 

 

 

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

接下来开始是代码部分

  1. public class ABData
  2. {
  3. public AssetBundle ab;
  4. public int num;
  5. }

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

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

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace Script
  6. {
  7. public class ScheduleOnce : UnitySingletn<ScheduleOnce>
  8. {
  9. private int id = 0;
  10. private Dictionary<int, Coroutine> m_Dictionary = new Dictionary<int, Coroutine>();
  11. public void AddSchedule(float time, Action<object> action,params object[] arr)
  12. {
  13. int name = id++;
  14. Coroutine coroutine = StartCoroutine(DelayFun(name, time, action, arr));
  15. m_Dictionary.Add(name,coroutine);
  16. }
  17. public void AddSchedule(float time, Action<object> action)
  18. {
  19. int name = id++;
  20. Coroutine coroutine = StartCoroutine(DelayFun(name, time, action));
  21. m_Dictionary.Add(name,coroutine);
  22. }
  23. IEnumerator DelayFun(int i, float time, Action<object> action,params object[] arr)
  24. {
  25. throw new NotImplementedException();
  26. }
  27. IEnumerator DelayFun(int i, float time, Action<object> action)
  28. {
  29. throw new NotImplementedException();
  30. }
  31. }
  32. }

 

  1. public class ABManager :Singleton<ABManager>
  2. {
  3. public Dictionary<string,List<string>> reflist= new Dictionary<string,List<string>>();
  4. public string SPath = Application.streamingAssetsPath + "/";
  5. public Dictionary<string, byte[]> assetDatas = new Dictionary<string, byte[]>();
  6. public Dictionary<string,ABData> abDic= new Dictionary<string,ABData>();
  7. public byte[] GetAssetData(string dllName)
  8. {
  9. return assetDatas[dllName];
  10. }
  11. public AssetBundle LoadAB(string dllName)
  12. {
  13. return AssetBundle.LoadFromMemory(GetAssetData(dllName));
  14. }
  15. public T Load<T>(string refname,string abName, string assetName) where T : UnityEngine.Object
  16. {
  17. AssetBundle ab;
  18. if(abDic.ContainsKey(abName))
  19. {
  20. ab = abDic[abName].ab;
  21. abDic[abName].num++;
  22. }
  23. else
  24. {
  25. ab=AssetBundle.LoadFromFile(SPath+ abName);
  26. ABData data=new ABData();
  27. data.ab = ab;
  28. data.num = 1;
  29. abDic.Add(abName, data);
  30. }
  31. if(ab!=null)
  32. {
  33. if(!reflist.ContainsKey(refname))
  34. {
  35. reflist.Add(refname,new List<string>());
  36. }
  37. reflist[refname].Add(abName);
  38. }
  39. return ab.LoadAsset<T>(assetName);
  40. }
  41. public GameObject Load(string abName,string assetName)
  42. {
  43. AssetBundle ab;
  44. if(abDic.ContainsKey(abName))
  45. {
  46. ab = abDic[abName].ab;
  47. abDic[abName].num++;
  48. }
  49. else
  50. {
  51. ab = AssetBundle.LoadFromFile(SPath + abName);
  52. ABData data=new ABData();
  53. data.ab = ab;
  54. data.num = 1;
  55. abDic.Add(abName, data);
  56. }
  57. if (ab != null)
  58. {
  59. if (!reflist.ContainsKey(assetName))
  60. {
  61. reflist.Add(assetName, new List<string>());
  62. }
  63. reflist[assetName].Add(abName);
  64. }
  65. return ab.LoadAsset<GameObject>(assetName);
  66. }
  67. public Type LoadScript(string assetName)
  68. {
  69. Assembly ass=Assembly.Load(GetAssetData("Script.dll.bytes"));
  70. return ass.GetType(assetName);
  71. }
  72. public void Release(string refname)
  73. {
  74. if(reflist.ContainsKey(refname))
  75. {
  76. foreach (var abName in reflist[refname])
  77. {
  78. if(abDic.ContainsKey(abName))
  79. {
  80. abDic[abName].num--;
  81. if (abDic[abName].num <= 0)
  82. {
  83. ScheduleOnce.Ins.AddSchedule(9, (obj) =>
  84. {
  85. object[] arr = obj as object[];
  86. ABData data = arr[0] as ABData;
  87. if (data.num <= 0)
  88. {
  89. data.ab.Unload(true);
  90. abDic.Remove(abName);
  91. Debug.Log("释放成功");
  92. }
  93. }, abDic[abName]);
  94. }
  95. }
  96. }
  97. reflist.Remove(refname);
  98. }
  99. }

  1. using System.Collections.Generic;
  2. using System.IO;
  3. using UnityEngine;
  4. using UnityEngine.Networking;
  5. public class LoadDll : MonoBehaviour
  6. {
  7. void Start()
  8. {
  9. StartCoroutine(this.DownLoadAssets(() =>
  10. {
  11. //程序集dll文件 添加的脚本
  12. Instantiate(ABManager.Ins.Load("Script.dll.bytes", "Test"));
  13. }));
  14. }
  15. IEnumerator DownLoadAssets(Action action)
  16. {
  17. //找到对应目录
  18. DirectoryInfo direction=new DirectoryInfo(Application.streamingAssetsPath);
  19. //获取目录下所有文件
  20. FileInfo[] files= direction.GetFiles("*",SearchOption.AllDirectories);
  21. //把所有dll文件拷贝到s目录下
  22. foreach (var item in files)
  23. {
  24. if(item.Name.EndsWith(".bytes"))
  25. {
  26. string dllPath = item.FullName.Replace("\\", "/");
  27. UnityWebRequest www=UnityWebRequest.Get(dllPath);
  28. yield return www.SendWebRequest();
  29. if(www.result!=UnityWebRequest.Result.Success)
  30. {
  31. Debug.Log(www.error);
  32. }
  33. else
  34. {
  35. byte[] data = www.downloadHandler.data;
  36. ABManager.assetDatas.Add(item.Name, data);
  37. Debug.Log(item.Name);
  38. }
  39. }
  40. }
  41. action();
  42. }
  43. }
  1. using UnityEngine;
  2. public class Test : MonoBehaviour
  3. {
  4. void Start()
  5. {
  6. GameObject game=GameObject.CreatePrimitive(PrimitiveType.Cube);
  7. Debug.Log("热更完毕");
  8. }
  9. }

 

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/686972
推荐阅读
相关标签
  

闽ICP备14008679号