HotUpdateCodeLoader.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.Collections;
  3. using System.Reflection;
  4. using UnityEngine;
  5. using GFGGame.Launcher;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using YooAsset;
  9. namespace GFGGame
  10. {
  11. public class HotUpdateCodeLoader : SingletonMonoBase<HotUpdateCodeLoader>
  12. {
  13. public Type[] allTypes;
  14. public Type[] GetTypes()
  15. {
  16. return this.allTypes;
  17. }
  18. private void Awake()
  19. {
  20. DontDestroyOnLoad(this.gameObject);
  21. }
  22. public void StartLoad()
  23. {
  24. StartCoroutine(StartLoadAssemblyHotfix());
  25. }
  26. IEnumerator StartLoadAssemblyHotfix()
  27. {
  28. yield return new WaitForSeconds(0.1f);
  29. var dllPath = $"{LauncherConfig.DllDirHotfix}Game.HotUpdate.dll.bytes";
  30. var handle = YooAssets.LoadAssetAsync<TextAsset>(dllPath);
  31. yield return handle;
  32. var asset = handle.AssetObject as TextAsset;
  33. // WebGL平台特殊处理
  34. LoadAssemblyForWebGL(asset.bytes);
  35. handle.Release();
  36. // 加载热更新场景
  37. YooAssets.LoadSceneAsync("Assets/ResIn/Scene/HotUpdate.unity");
  38. }
  39. void LoadAssemblyForWebGL(byte[] dllBytes)
  40. {
  41. // WebGL平台下需要使用JavaScript互操作来加载程序集
  42. // 这里需要实现一个JavaScript插件来实际加载程序集
  43. // 示例代码,实际实现可能需要与你的JavaScript插件配合
  44. string dllContent = Convert.ToBase64String(dllBytes);
  45. Application.ExternalCall("LoadHotUpdateAssembly", dllContent);
  46. // 注意:在WebGL下可能无法直接获取所有类型,需要根据实际情况调整
  47. this.allTypes = new Type[0]; // 可能需要从JS端获取类型信息
  48. Debug.Log($"调试 LoadAssemblyForWebGL :{this.allTypes.Length}");
  49. }
  50. void LoadAssemblyJustInTime(Assembly assembly)
  51. {
  52. // 常规平台加载方式
  53. this.allTypes = assembly.GetTypes();
  54. }
  55. }
  56. }