ILRuntime第一課HelloWorld

1.環境配置

a).導入熱更需要的文件夾

  • ILRuntime

  • Mono.Cecil.20

  • Mono.Cecil.Pdb

b)設置不安全模式

  • Assets目錄裏建立一個名爲smcs.rsp的文本文件

  • 在smcs.rsp文件中加入 -unsafe

  • 如果你使用的是Unity5.5以上的版本,你需要將上述說明中的smcs.rsp文件名改成mcs.rsp

c)刪除一些測試代碼

    ​    ​已刪除完畢的,可以直接導入的:

    ​    ​鏈接:http://pan.baidu.com/s/1hsEbpVQ

    ​    ​密碼:0yxk


2.編寫熱更新的工程(HotFix_TestProject)

a)先編譯一次主工程,然後導入Assembly-CSharp、UnityEngine、UnityEngine.UI

b)編寫工程

c)導出到Unity項目中的StreamingAssets文件夾下面


3.主工程中的完整代碼:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using ILRuntime.Runtime.Enviorment;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
 
public class HelloWorld : MonoBehaviour {
    AppDomain appdomain;
 
 
    void Start()
    {
        StartCoroutine(LoadHotFixAssembly());
    }
 
    IEnumerator LoadHotFixAssembly() {
        appdomain = new ILRuntime.Runtime.Enviorment.AppDomain();
 
        //加載DLL文件
#if UNITY_ANDROID
        WWW www = new WWW(Application.streamingAssetsPath + "/HotFix_TestProject.dll");
#else
        WWW www = new WWW("file:///" + Application.streamingAssetsPath + "/HotFix_TestProject.dll");
#endif
        while (!www.isDone)//如果資源未加載完
            yield return null;
        if (!string.IsNullOrEmpty(www.error))
            Debug.LogError(www.error);
        byte[] dll = www.bytes;
        www.Dispose();
 
        //加載PDB文件
#if UNITY_ANDROID
        www = new WWW(Application.streamingAssetsPath + "/HotFix_TestProject.pdb");
#else
        www = new WWW("file:///" + Application.streamingAssetsPath + "/HotFix_TestProject.pdb");
#endif
        while (!www.isDone)
            yield return null;
 
        if (!string.IsNullOrEmpty(www.error))
            Debug.LogError(www.error);
        byte[] pdb = www.bytes;
        using (System.IO.MemoryStream fs=new System.IO.MemoryStream(dll)) {
            using (System.IO.MemoryStream p=new System.IO.MemoryStream(pdb)) {
                appdomain.LoadAssembly(fs, p, new Mono.Cecil.Pdb.PdbReaderProvider());
            }
        }
 
        InitializeILRuntime();
        OnHotFixLoaded();
    }
 
    //初始化,註冊
    void InitializeILRuntime()
    {
 
    }
 
    //調用
    void OnHotFixLoaded() {
        appdomain.Invoke("HotFix_TestProject.InstanceClass""StaticFunTest"null,null);
    }
 
}

發佈了40 篇原創文章 · 獲贊 12 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章