Framework_YooAsset_HybridCLR/Assets/DemoGame/GameScript/Loader/Loader.cs

83 lines
3.2 KiB
C#
Raw Normal View History

using HybridCLR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Cysharp.Threading.Tasks;
using UnityEngine;
using YooAsset;
namespace Unity.Loader
{
public class Loader
{
public List<string> aotDllNames = new List<string>()
{
@"Assets/DemoGame/GameRes/AotDlls/mscorlib.dll.bytes",
@"Assets/DemoGame/GameRes/AotDlls/UniTask.dll.bytes",
@"Assets/DemoGame/GameRes/AotDlls/YooAsset.dll.bytes",
};
private Dictionary<string, TextAsset> dlls = new Dictionary<string, TextAsset>();
private Dictionary<string, TextAsset> aotDlls = new Dictionary<string, TextAsset>();
public async UniTask DownloadAsync()
{
if (true) // Dll
{
string dllName = @"Assets/DemoGame/GameRes/Codes/Hotfix.dll.bytes";
// Editor环境下HotUpdate.dll.bytes已经被自动加载不需要加载重复加载反而会出问题。
#if !UNITY_EDITOR
string hotfixDll = dllName;
var loadAssetAsync = await ResourcesComponent.Instance.LoadAssetAsync<TextAsset>(hotfixDll);
this.dlls.Add(hotfixDll, loadAssetAsync);
#else
// Editor下无需加载直接查找获得HotUpdate程序集
// Assembly hotUpdateAss = System.AppDomain.CurrentDomain.GetAssemblies().First(a => a.GetName().Name == "Hotfix");
string hotfixDll = "Hotfix";
Assembly hotUpdateAss = System.AppDomain.CurrentDomain.GetAssemblies().First(a => a.GetName().Name == hotfixDll);
#endif
}
if (true) // AOTDll
{
#if !UNITY_EDITOR
foreach (var dll in this.aotDllNames)
{
var loadAssetAsync = await ResourcesComponent.Instance.LoadAssetAsync<TextAsset>(dll);
this.aotDlls.Add(dll, loadAssetAsync);
}
#endif
}
await UniTask.Yield();
}
public void OnStart(GameObject go, EPlayMode playMode)
{
foreach (var kv in this.aotDlls) // 先加载aot内容避免报错
{
TextAsset textAsset = kv.Value;
Debug.Log($":{kv.Key}. text: {textAsset.text}");
var errorCode = RuntimeApi.LoadMetadataForAOTAssembly(textAsset.bytes, HomologousImageMode.SuperSet);
Debug.Log($"LoadMetadataForAOTAssembly:{kv.Key}. ret: {errorCode}");
}
string dllName = @"Assets/DemoGame/GameRes/Codes/Hotfix.dll.bytes";
// Editor环境下HotUpdate.dll.bytes已经被自动加载不需要加载重复加载反而会出问题。
#if !UNITY_EDITOR
var dllAsset = this.dlls[dllName];
Assembly hotUpdateAss = Assembly.Load(dllAsset.bytes);
#else
// Editor下无需加载直接查找获得HotUpdate程序集
Assembly hotUpdateAss = System.AppDomain.CurrentDomain.GetAssemblies().First(a => a.GetName().Name == "Hotfix");
#endif
var type = hotUpdateAss.GetType("ZC.GameEntry");
object[] pa = new object[] { go, playMode };
type.GetMethod("Init").Invoke(null, pa);
}
}
}