用的框架是LuaFramework_UGUI
地址:https://github.com/jarjin/LuaFramework_UGUI
最终效果:
更新前:
更新后:
1.新建场景,创建GameManager上挂上Main.cs
Main.cs代码:(作用就是启动框架,为框架入口)
void Start() {
AppFacade.Instance.StartUp(); //启动游戏
}
2.创建Panel1并作为预设,然后删除:(放到Assets/Prefabs下)
3.打开main.lua,添加如下代码
local go;
--主入口函数。从这里开始lua逻辑
function Main()
--print("hello");
LuaFramework.Util.LogWarning("here");
LuaHelper=LuaFramework.LuaHelper;
resMgr=LuaHelper.GetResManager();
resMgr:LoadPrefab('panel',{'Panel1'},OnLoadFinish);
end
--加载完成后的回调
function OnLoadFinish( objs )
go = UnityEngine.GameObject.Instantiate(objs[0]);
local parent = UnityEngine.GameObject.Find("Canvas")
go.transform:SetParent(parent.transform);
go.transform.localScale=Vector3.one;
go.transform.localPosition=Vector3.zero;
end
4.打开AppConst.cs,将更新模式和Bundle模式开启,并修改weburl
5.Util.cs中GetRelativePath方法改成如下:
public static string GetRelativePath() {
if (AppConst.UpdateMode)
{
return "file:///" + DataPath;
}
if (Application.isEditor)
return "file://" + System.Environment.CurrentDirectory.Replace("\\", "/") + "/Assets/" + AppConst.AssetDir + "/";
else if (Application.isMobilePlatform || Application.isConsolePlatform)
return "file:///" + DataPath;
else // For standalone player.
return "file://" + Application.streamingAssetsPath + "/";
}
6.GameManager.cs方法OnInitialize()注释我们不需要的东西:
7.打开Packager.cs,在”处理框架实例包“HandleExampleBundle()中修改如下:
8.菜单栏LuaFramework>Build Window Resources;
此时会把Assets/Prefabs下的所有*prefab打包到StreamingAssets下。
9.本地开一个NetBox,并把StreamingAssets文件夹复制过去
10.运行unity,可以build一个exe出来方便后续操作观察。
11.跟2一样,做成预设,删除,然后执行8和9.
自此,就可以稳稳地操作热更新了。
补充:打开更新模式和不以ab方式读取时,路径出错
public const bool UpdateMode = false; //更新模式-默认关闭
public const bool LuaByteMode = false; //Lua字节码模式-默认关闭
public const bool LuaBundleMode = false; //Lua代码AssetBundle模式
//LuaBundleMode修改为false,这样代码文件便不会以AssetBundle模式读取,会直接生效,以方便调试。
LuaConst.cs中,lua逻辑代码目录改为如下:
//public static string luaDir = Application.dataPath + "/Lua"; //lua逻辑代码目录
public static string luaDir = Application.dataPath + "/LuaFramework/Lua";