1.创建C#类库(dll)项目
2.删除命名空间,不然可能会出错
public class Manager { private int count; public Manager() { count = 2; } public string IntToStr() { return count.ToString(); }
}
public class Login:MonoBehaviour { void Start() { Manager mgr = new Manager(); Debug.Log(mgr.IntToStr()); } }
3.点击生成
将Test2.dll 的后缀名改为bytes -> Test2.bytes
将这个文件拖拽到Unity工程中
将工程打包为Hot.unitypackage
本次样例路径:C:/Users/14486/Desktop/2020/热更新学习/Hot/Hot.unitypackage
在unity项目中添加脚本:
private static readonly string DLL_URL = "file:///C:/Users/14486/Desktop/2020/热更新学习/Hot/Hot.unitypackage"; void Start() { StartCoroutine(LoadDllScript()); } private IEnumerator LoadDllScript() { WWW www = new WWW(DLL_URL); yield return www; AssetBundle bundle = www.assetBundle; if (bundle == null) Debug.Log("Is NUll"); TextAsset asset = bundle.LoadAsset("Test2", typeof(TextAsset)) as TextAsset; System.Reflection.Assembly assembly = System.Reflection.Assembly.Load(asset.bytes); System.Type login = assembly.GetType("Login"); GameObject obj = new GameObject(); obj.AddComponent(login); }
运行即可