Unity热更新专题(九)Unity热更新实例
经过前面的学习。我们已经知道了用uLua进行Unity热更新的大概方法了。现在我们通过一个实例,来实现Unity热更新。
我们在这里仿照上一节进行UI的热更新。
首先创建你自己的UI界面,然后制成Prefab。然后再Asset Labels里AssetBundle里命名。这里要注意,制作UI所用到的所有素材也都要制成AssetBundle。
然后就是代码部分了。
首先,我们创建个空物体,添加脚本GlobalGenerator.cs脚本。
using UnityEngine;
using System.Collections;
namespace SimpleFramework {
/// <summary>
/// 全局构造器,每个场景里都有,所以每个场景都会初始化一遍,也会初始化游戏管理器一次
/// 如果游戏管理器已经存在了,就跳过了,否则创建游戏管理器,来保证游戏里只有一个GameManager
/// </summary>
public class GlobalGenerator : MonoBehaviour {
void Awake() {
InitGameMangager();
}
/// <summary>
/// 实例化游戏管理器
/// </summary>
public void InitGameMangager() {
string name = "GameManager";
gameObject.AddComponent<AppView>();
GameObject manager = GameObject.Find(name);
if (manager == null) {
manager = new GameObject(name);
manager.name = name;
AppFacade.Instance.StartUp(); //启动游戏
}
}
}
}
更改GameManager.lua脚本:
require "Common/define"
require "Controller/BottomCtrl"
require "Controller/SettingsCtrl"
require "Controller/DialogCtrl"
GameManager = {}
local this = GameManager
function GameManager.LuaScriptPanel()
return 'Bottom','Settings','Dialog'
end
function GameManager.OnInitOK()
AppConst.SocketPort = 2012
AppConst.SocketAddress = "127.0.0.1"
NetManager:SendConnect()
BottomCtrl.Awake()
SettingsCtrl.Awake()
DialogCtrl.Awake()
end
这个脚本可以参照原示例来写。
其中:
function GameManager.LuaScriptPanel()
return 'Bottom','Settings','Dialog'
end
与我们需要热更的对应:
然后就是三个对应的View脚本:
SettingsPanel = {}
local this = SettingsPanel
local transform
local gameObject
function SettingsPanel.Awake(obj)
gameObject = obj
transform = gameObject.transform
this.InitPanel()
end
function SettingsPanel.InitPanel()
this.anim = transform:FindChild("Bg"):GetComponent("Animator")
this.buttonClose = transform:FindChild("Bg/ButtonClose").gameObject
end
BottomPanel={}
local this = BottomPanel
local transfrom
local gameObject
function BottomPanel.Awake(obj)
gameObject = obj
transfrom = obj.transform
this.InitPanel()
end
function BottomPanel.InitPanel()
this.buttonSettings = transfrom:FindChild("ButtonSettings").gameObject
this.buttonPeople = transfrom:FindChild("ButtonPeople").gameObject
this.buttonDialog = transfrom:FindChild("ButtonDialog").gameObject
end
DialogPanel = {}
local this = DialogPanel
local transform
local gameObject
function DialogPanel.Awake(obj)
gameObject = obj
transform = obj.transform
this.InitPanel()
end
function DialogPanel.InitPanel()
this.anim = transform:FindChild("Bg"):GetComponent("Animator")
this.buttonClose = transform:FindChild("Bg/ButtonClose").gameObject
end
对应的Controller下的脚本。
require "Common/define"
SettingsCtrl = {}
local this = SettingsCtrl
local transform
local gameObject
local lua
function SettingsCtrl.New()
return this
end
function SettingsCtrl.Awake()
PanelManager:CreatePanel( "Settings", this.OnCreate )
end
function SettingsCtrl.OnCreate(obj)
gameObject = obj
transform = obj.transform
lua = gameObject:GetComponent("LuaBehaviour")
lua:AddClick( SettingsPanel.buttonClose,this.OnButtonClose )
end
function SettingsCtrl.OnButtonClose()
this.Hide()
end
function SettingsCtrl.Show()
SettingsPanel.anim:SetBool("IsShow",true)
end
function SettingsCtrl.Hide()
SettingsPanel.anim:SetBool("IsShow",false)
end
require "Common/define"
DialogCtrl = {}
local this = DialogCtrl
local transforml
local gameObject
local lua
function DialogCtrl.New()
return this
end
function DialogCtrl.Awake()
PanelManager:CreatePanel( "Dialog", this.OnCreate )
end
function DialogCtrl.OnCreate(obj)
gameObject = obj
transform = obj.transform
lua = gameObject:GetComponent("LuaBehaviour")
lua:AddClick( DialogPanel.buttonClose,this.OnButtonClose )
end
function DialogCtrl.OnButtonClose()
this.Hide()
end
function DialogCtrl.Show()
DialogPanel.anim:SetBool("IsShow",true)
end
function DialogCtrl.Hide()
DialogPanel.anim:SetBool("IsShow",false)
end
require "Common/define"
BottomCtrl = {}
local this = BottomCtrl
local transform
local gameObject
local lua
function BottomCtrl.New()
return this
end
function BottomCtrl.Awake()
PanelManager:CreatePanel( "Bottom",this.OnCreate )
end
function BottomCtrl.OnCreate(obj)
gameObject = obj
transform = gameObject.transform
lua = gameObject:GetComponent("LuaBehaviour")
lua:AddClick( BottomPanel.buttonDialog,this.OnButtonDialogClick )
lua:AddClick( BottomPanel.buttonPeople,this.OnButtonPeopleClick )
lua:AddClick( BottomPanel.buttonSettings,this.OnButtonSettingsClick )
end
function BottomCtrl.OnButtonDialogClick()
DialogCtrl.Show()
end
function BottomCtrl.OnButtonPeopleClick()
end
function BottomCtrl.OnButtonSettingsClick()
SettingsCtrl.Show()
end
我们在运行前,要先Clear,再Build。
运行结果如下:
这就是一个简单的实例。
unity有了很多lua的绑定库,也有了ulua这样的pure c#移植实现。
你还有一个选择是C#Light/Evil,他是C#语法的,pure c#实现的一门新生脚本语言,就是为了Unity3D逻辑热更新而生。
以后再讨论。
====================================================================================
结束。