Unity ILRuntime热更新(二)

一、启动ILRuntime并加载程序集

public class Lesson02 : MonoBehaviour
{
   
    private AppDomain _domain;
    private MemoryStream _dllStream;
    private MemoryStream _pdbStream;
    IEnumerator Start()
    {
   
        _domain = new AppDomain();
        _domain.UnityMainThreadID = Thread.CurrentThread.ManagedThreadId;

        //请求Dll
#if UNITY_ANDROID
        UnityWebRequest dllReq = UnityWebRequest.Get(Application.streamingAssetsPath + "/HotFix_Project.dll");am = new MemoryStream();
#else
        UnityWebRequest dllReq = UnityWebRequest.Get("file:///" + Application.streamingAssetsPath + "/HotFix_Project.dll");
#endif
        yield return dllReq.SendWebRequest();
        if(dllReq.result != UnityWebRequest.Result.Success)
            Debug.Log("加载Dll错误!");
        byte[] dllBuffer = dllReq.downloadHandler.data;
        dllReq.Dispose();
        //请求Pdb
#if UNITY_ANDROID
        UnityWebRequest pdbReq = UnityWebRequest.Get(Application.streamingAssetsPath + "/HotFix_Project.pdb");am = new MemoryStream();
#else
        UnityWebRequest pdbReq = UnityWebRequest.Get("file:///" + Application.streamingAssetsPath + "/HotFix_Project.pdb");
#endif
        yield return pdbReq.SendWebRequest();
        if (pdbReq.result != UnityWebRequest.Result.Success)
            Debug.Log("加载Pdb错误!");
        byte[] pdbBuffer = pdbReq.downloadHandler.data;
        pdbReq.Dispose();

        _dllStream = new MemoryStream(dllBuffer);
        _pdbStream = new MemoryStream(pdbBuffer);

        _domain.LoadAssembly(_dllStream,_pdbStream,new PdbReaderProvider());

        print("加载成功!!!");
    }

    private void OnDestroy()
    {
   
        if(_domain != null)
        {
   
            _domain.Dispose(

你可能感兴趣的:(unity,游戏引擎)