欢迎加入Unity业内qq交流群:956187480
qq扫描二维码加群
ios不支持反射更新,故此方案只适用安卓,PC端设置模式 : 1.更新模式 2.不更新模式
本框架为衔接框架并非游戏框架,接入后需要扩展到自己的游戏框架里
一.Main类编写入口函数,在更新模式下负责加载打成ab包后的Dll文件,并调用DllManager的初始化函数
参考反射 方法调用:https://blog.csdn.net/qq_37310110/article/details/89515198
public class Main : MonoBehaviour
{
public bool isUpdate = false;
Assembly assembly;
void Start()
{
if (isUpdate)
{
//更新启动
Debug.Log("更新启动");
StartCoroutine(LoadDll());
}
else
{
//正常启动
Debug.Log("正常启动");
gameObject.AddComponent().InitAssembly(isUpdate,null);
}
}
IEnumerator LoadDll()
{
string path = Application.streamingAssetsPath + "/mydll";
WWW www = new WWW(path);
yield return www;
AssetBundle bundle = www.assetBundle;
TextAsset asset = bundle.LoadAsset("MyDLL", typeof(TextAsset)) as TextAsset;
assembly = Assembly.Load(asset.bytes);
Type type = assembly.GetType("DllManager");
var com = gameObject.AddComponent(type);
MethodInfo methodInfo = type.GetMethod("InitAssembly");
methodInfo.Invoke(com, new object[] { isUpdate, assembly });
}
}
二:Unity编写
刚开始所有脚本都放在unity本地,模式设置为非更新模式,所有的组件添加都为动态添加。
这样做我们可以在开发阶段本地测试,测试没问题再制作Dll文件。
public class DllManager : MonoBehaviour
{
public static DllManager Instance;
public Assembly assembly;
public bool isUpdate;
void Awake()
{
Instance = this;
}
//初始化反射资源
public void InitAssembly(bool isUpdate, Assembly assembly)
{
this.assembly = assembly;
this.isUpdate = isUpdate;
LoadMainView();
}
//加载主界面
private void LoadMainView()
{
var mainView = Instantiate(Resources.Load("Panel")) as GameObject;
mainView.transform.SetParent(GameObject.Find("Canvas").transform);
mainView.transform.localPosition = Vector3.zero;
AddCompotent(mainView, "MainUI");
}
//动态加载脚本
public Component AddCompotent(GameObject entity, string compotentName)
{
if (isUpdate)
{
//反射加载
return entity.AddComponent(assembly.GetType(compotentName));
}
else
{
//常规加载
return entity.AddComponent(Type.GetType(compotentName));
}
}
}
三:Dll文件制作
用VicrosoftStudio制作Dll
1.新建类库项目
2. 右键解决方案进入属性界面
普及.net framework版本区别 https://blog.csdn.net/qq_37310110/article/details/89514260
3.引入Unity的dll,右键引用,添加引用
在Unity的安装路径下找到UnityEngine.dll和UnityEngine.dll添加
4.把unityceshi测试好的脚本文件拖到vs内
设置当前解决方案为启动项,生成Dll后,把dll后缀改成bytes,为什么要改后面说明,改好后拖到Unity,到此dll制作完成
打包操作网上各种版本各种方法都有,后面可能会把我之前封装好的打包方案记录给 大家看,目前简单打包即可。
Editor编写打包脚本
[MenuItem("打AB包/Build Windows Resource", false, 13)]
public static void BuildWindowsResource() {
BuildAssetResource(BuildTarget.StandaloneWindows, true);
}
public static void BuildAssetResource(BuildTarget target, bool isWin) {
string dataPath = "c:/DLLTest/";
if (Directory.Exists(dataPath)) {
Directory.Delete(dataPath, true);
}
if (!Directory.Exists(Application.streamingAssetsPath)) Directory.CreateDirectory(Application.streamingAssetsPath);
BuildPipeline.BuildAssetBundles(Application.streamingAssetsPath, BuildAssetBundleOptions.None, target);
}
给dll文件设置ab标签:Dll文件直接打包会失败无效,改后缀名bytes即可
打包:
结束。
更新模式跟非更新模式 运行结果正常一样,说明没问题;接下来改一下脚本逻辑做一下更新
:
替换MainUI脚本重新生成dll,替换旧的dll
更新后运行结果
版权声明:欢迎加入qq交流群:956187480 https://blog.csdn.net/qq_37310110/article/details/83145193 欢迎加入Unity业内qq交流群:956187480
qq扫描二维码加群