-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeBundleAssemblyLoader.cs
More file actions
102 lines (82 loc) · 2.86 KB
/
CodeBundleAssemblyLoader.cs
File metadata and controls
102 lines (82 loc) · 2.86 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System;
using UnityEngine;
using UnityEngine.Events;
namespace CodeBundle
{
/// <summary>
/// Load assembly from dll placed in asset bundle or just in Resource folder
/// </summary>
public class CodeBundleAssemblyLoader : MonoBehaviour
{
public string DllName;
public string DllNameForAssetBundle;
public UnityEvent OnError;
public UnityEvent OnSuccess;
public CodeBundleState State;
public bool ForceUseAssetBundles;
public void Load()
{
if ((State = CodeBundleState.CheckAndLoad(State)) == null)
{
OnError.Invoke();
return;
}
if (!Application.isEditor || ForceUseAssetBundles)
{
LoadFromAssetBundle();
}
else
{
LoadFromResources();
}
}
private void LoadFromResources()
{
Debug.Log("Start loading dll from resource");
var dllText = Resources.Load<TextAsset>(DllName);
if (dllText == null)
{
Debug.LogError($"Dll text asset not found in resources, name is: {DllName}");
OnError.Invoke();
return;
}
try
{
AppDomain.CurrentDomain.Load(dllText.bytes);
}
catch (Exception e)
{
Debug.LogError($"Error when loading dll from assembly in AppDomain");
Debug.LogException(e);
OnError.Invoke();
return;
}
Debug.Log("Loading dll from resource finish success");
OnSuccess.Invoke();
}
private void LoadFromAssetBundle()
{
Debug.Log("Start loading dll from asset bunle");
var dllText = State.AssetNameToBundle[DllNameForAssetBundle].LoadAsset<TextAsset>(DllNameForAssetBundle);
if (dllText == null)
{
Debug.LogError($"Dll text asset not found in assetbundle, name is: {DllNameForAssetBundle}");
OnError.Invoke();
return;
}
try
{
AppDomain.CurrentDomain.Load(dllText.bytes);
}
catch (Exception e)
{
Debug.LogError($"Error when loading dll from assembly in AppDomain");
Debug.LogException(e);
OnError.Invoke();
return;
}
Debug.Log("Loading dll from asset bundle finish success");
OnSuccess.Invoke();
}
}
}