Unity3D heat updates LuaFramework articles [09] - specific heat of resources and updates the updated hot code

Foreword

In the previous article  [08] Unity3D hot updates LuaFramework articles - Principles and hot update server set up more hot  , I introduced the basic principles of heat update, and begin to build a server.

Benpian to do a practical exercise to achieve real hot update function.

First, the preparatory work

1, for making a thermal interface update

I had made a hall interface, and placed two buttons: "list" and "mall", charts button has been used to open the leaderboard page.

So now and then to make a store page, click on the button when the mall opens store page.

1) Production ShopPanel interface.

a) make a store interface ShopPanel, title and put a simple three commodity items on the screen. As shown below:

 

b) the preform is made ShopPanel placed \ LuaFramework \ CustomPrj \ directory under Shop Assets.

c) create ShopPalel.lua and ShopCtrl.lua and do CtrlNames PanelNames registration and registration. ShopPanel.lua on Assets \ LuaFramework \ Lua \ under the View \ Shop catalog, ShopCtrl.lua on \ LuaFramework \ Lua \ Controller \ directory under Shop Assets.

d) adding the HandleExampleBundle Packager.cs method of packaging ShopPanel code;

        // packed and ready for testing the preform ShopPanel 
        AddBuildMap ( " Shop " + AppConst.ExtName, " * .prefab " , " Assets / LuaFramework / CustomPrj / Shop " );

 

2) Click the button to display the mall store panel

a) adding a reference to the store button in HallPanel.lua; 

Transform local; 
local gameObject; 

HallPanel = {}; 
local the this = HallPanel;

 - Start Event - 
function HallPanel.Awake (obj) 
    gameObject = obj; 
    Transform = obj.transform; 

    the this .InitPanel (); 
    logWarn ( " the Awake Lua >> --- " ..gameObject.name); 
End

 - initialize the panel - 
function HallPanel.InitPanel () 
    logWarn ( " I am HallPanel, I was loaded. " );

     - charts button 
    HallPanel.rankingBtn = the Transform: FindChild ( "BtnRanking " ) .gameObject;

     - ranking panel 
    HallPanel.rankingPanel = transform.parent: the Find ( " RankingPanel " );

     - Shop button 
    HallPanel.shopBtn = the Transform: FindChild ( " BtnShop " ) .gameObject;

     - call the Ctrl panel creation method when completed 
    HallCtrl.OnCreate (gameObject); 
End 

function HallPanel.OnDestroy () 
    logWarn ( " OnDestroy ---- >>> " ); 
End
HallPanel.lua

b) Add button in the mall HallCtrl.lua event handling 

= HallCtrl {}; 
local the this = HallCtrl; 

local Behavior; 
local Transform; 
local gameObject;

 - Construction Function - 
function HallCtrl.New () 
    logWarn ( " HallCtrl.New --- >> " );
     return  the this ; 
End 

function HallCtrl.Awake () 
    logWarn ( " HallCtrl.Awake --- >> " ); 

    logWarn ( " I am HallCtrl, I was loaded. " ); 
End


 - launch event - 
function HallCtrl.OnCreate (obj) 
    gameObject = obj; 
    Transform =obj.transform; 

    UIEventEx.AddButtonClick (HallPanel.rankingBtn, function () 
        log ( " you click on the chart button " ); 

        HallPanel.rankingPanel.gameObject: SetActive ( to true ); 
    End); 

    UIEventEx.AddButtonClick (HallPanel.shopBtn, function () 
        log ( " you click on the Shop button " );

         - instantiate panel shop 
        local shopCtrl = CtrlManager.GetCtrl (CtrlNames.Shop); 
        shopCtrl: Awake (); 
    End); 
End

 - click event - 
function HallCtrl .OnClick (Go) 
    the destroy (gameObject); 
End

 - off event -
function HallCtrl.Close()
    panelMgr:ClosePanel(CtrlNames.Hall);
end
HallCtrl.lua

3) Packaging and run

a) Build Windows Resources to perform the operation, the newly added ShopPanel will be automatically scored bag

b) Run Unity, click on the "mall" button to open a shop panel correctly.

 

2, modifying the configuration more heat

Open AppConst.cs script,

The UpdateMode set to true;

The WebUrl address to http://192.168.0.110:8080/StreamingAssets/  

192.168.110 This is my ip of the machine, Tomcat default port number is 8080, where you can modify for your own ip according to the actual situation.

Modifying the position shown below:

 

 

3, the test server more heat

1) The StreamingAssets directory under Assets entire copied to the webapps directory under Tomcat, I am here

I:\apache-tomcat-9.0.22\webapps。

2) Enter in your browser http://192.168.0.110:8080/StreamingAssets/files.txt  .

If you can get to the content files.txt, it indicates that the server is ready more heat.

 

Second, the resource hot update

1, make a client program

Hot update operation involves two parties, a server, a client. The server is ready, and now also the lack of a client program.

1) First, convert unity project to the Android platform, click Build Android Resource menu regenerate packaged resources.

2) After the build is completed, the item will be now labeled a apk packet (note that the Hall position 0 in the scene, in order to load the first scene)

 (打包的时候如果出现报错说插件冲突和架构重复啥的,删掉Plugins目录下的x86目录就好,保留x86_64目录)

3)把StreamingAssets目录复制到Tomcat的webapps目录(之前的删掉就行)。之前测试服务器的时候拷过去的是Windows版本资源,无法和安卓客户端进行更新,所以这里需要重新拷贝。

4)安装这个apk包到手机,手机应该和电脑位于同一局域网。我这里使用的是一个安装在本机上的安卓模拟器。

5)运行刚安装的程序。

能正常打开大厅场景,点击商城按钮,也能正常弹出商店界面。如下图:

 

如果刚进游戏如果直接点击不起作用,等一下再试(正在解压资源)。

现在客户端也已经就绪。

 

2、制作更新内容并上传到热更服务器;

       上一步的操作中,我们打了一个安卓包,并将此时的StreamingAssets目录复制了到了Tomcat中,现在的状态是:

       服务器上的StreamingAssets内容和安卓客户的StreamingAssets内容是相同的。这种状态下客户端是不会进行更新操作的。

       假使安卓包已经上线,过了几天,我们需要更新版本。

       更新内容就是:调整商店页面的商品,将商品1进行更新。

现在制作一个新的商店页面。

1)修改ShopPanel的预制体,将原商品1的名称修改为“新品“,图标也进行调整,如下图:

    

2)执行Build Android Resource菜单,重新进行打包;

3)打包完成后,将StreamingAssets目录复制到Tomcat的webapps目录下,覆盖掉之前的内容。

3、重新运行客户端

       经过上一步的操作,现在的服务器资源是更新后的显示为“新品”的资源,而客户而还是之前的显示为“商品1”的资源。如下图:

 

在商店资源更新到服务器后,不用重新打安卓包。

直接重新运行之前的安卓客户端 ,如果客户端打开商店显示了“新口”的商品,则说明资源热更新成功了(预制体是一种Unity资源)。

好了,重新运行客户端。

点击商城按钮。能看到,商店界面已经自动更新到服务器的版本了。

 

效果与预期的一致,资源热更成功。

 

三、代码热更新

这里说的代码热更,只包含lua代码,所有的c#代码是没法更新的。

所以c#代码一般用来写一此底层逻辑,以及通用型逻辑,具体的业务逻辑最好都由lua来实现。

在版本计划中,如果不得不改动c#代码,那就只能发个全量包了。

现在开始做代码热更。

计划:修改ShopPanel.lua的代码,在代码中动态修改商店界面的标题,如果能看到商店标题被更改,则表示代码热更成功。

1、  修改ShopPanel.lua的代码,在代码中引用标题所在的Text组件,并将商店标题修改为“热更商店”。具体修改见下图:  

2、  点击Build Android Resource,重新生成打包资源;

3、  将StreamingAssets目录复制到Tomcat的webapps目录,覆盖掉之前的;

4、  重新运行客户端程序。

能看到商店的标题已经被修改了。

 

说明代码更新成功。

 

至此,热更操作实战演练完成。

 

一些说明:

  1. 本文选用安卓包做客户端,一是为了验证这个热更功能在移动端上的可用性;二是因为,这个资源热更功能在Windows平台无法正常加载更新的资源。可能是框架原因,也可能是我的疏漏,没有查证。 不过好在安卓平台上的热更是没有问题的。至于iOS平台,条件有限,没法去测试。
  2.  热更版本的包在第一次启动的时候,有一个漫长的解压缩过程,上一篇讲原理的时候,有提到过。直接表现就是第一次运行,会黑屏很久。我这里没有黑屏,是因为我的大厅面板是直接显示的,不用等待解压过程。 但是,一看到大厅界面,就立即点击排行榜按钮和商城按钮,是不会在响应的,因为资源未解压完成,lua脚本啥的还未加载。 我给工程加了一个Unity-Logs-Viewer插件,在屏幕上划2圈,能打开日志界面,从日志里能看到解压缩的过程。
  3.  关于更新,目前框架实现的逻辑也比较简单,直接比较files.txt的内容,发现不一致就进行更新。 如果是实际项目的话,要做版本控制:Tomcat的webapps目录下应该是按版本放置的资源,并提供服务端版本号,客户端要有取服务端版本信息的能力,并根据版本号,从不同的目录读取资源。
  4.  关于下载,目前的下载逻辑也比较简易,是按资源一定能下载成功做的逻辑,没有处理资源下载失败等异常情况,最好在UI上也能对此过程有显示,这个需要自己完善。
  5.  总的来说,这是一个可用的框架,但是还需要自己完善。

 

项目地址

为方便参照学习,本项目源码已经上传到github,地址:

https://github.com/tanyuqing/LuaFramework-in-action

Guess you like

Origin www.cnblogs.com/imteach/p/11266958.html