UI 底层基类(不算是UI) BaseGameNode类 最底层基类, 谁也没继承, 单纯的class()
比较疑惑的字段 1 2 3 4 5 6 7 8 9 10 11 12 self ._positionself ._scaleself ._rotationself .__name
初始化 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 BaseGameNode:initPrefabGameObject(path ) BaseGameNode:initEmptyGameObject(name) BaseGameNode:initGameObject(obj)
子父节点 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 BaseGameNode:addChild(nodeChild, transformChild) BaseGameNode.setParent(nodeParent, transformParent) BaseGameNode:removeAllChildren() BaseGameNode:removeChild(child, cleanup) BaseGameNode:removeFromParent(cleanup)
销毁 1 2 3 4 5 6 7 8 9 10 11 12 13 BaseGameNode:cleanup() BaseGameNode:onCleanUp()
状态 1 2 3 4 5 6 BaseGameNode:setActive(ac) BaseGameNode:onActiveChange(bValue)
Update功能 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 BaseGameNode:triggerUpdate() BaseGameNode:update() BaseGameNode:triggerLateUpdate() BaseGameNode:lateUpdate() BaseGameNode:triggerFixedUpdate() BaseGameNode:fixedUpdate()
Action功能 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 BaseGameNode:runAction(action) BaseGameNode:stopAction(action) BaseGameNode:stopAllAction()
监听 消息/事件 功能 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 BaseGameNode:addMessageListener(key, fun, obj) BaseGameNode:removeMessageListener(key, fun, obj) BaseGameNode:removeAllMessageListener() BaseGameNode:on(key, fun, obj) BaseGameNode:off(key, fun, obj) BaseGameNode:send(key, ...)
二级派生类(不算UI) BaseViewItem 继承自BaseGameNode, BaseViewItem: BaseGameNode.
状态 1 2 3 4 5 BaseViewItem:show() BaseViewItem:hide()
资源下载/网络 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 BaseViewItem:getAssetPath() BaseViewItem:loadRes(scb, fcb, silience)
三级派生类(UI的底层节点基类) BaseUINode 继承自 BaseViewItem, BaseUINode: BaseViewItem.
初始化(同BaseGameNode基本一样) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 BaseUINode:initEmptyGameObject(name, comp) BaseUINode:initGameObject(obj) BaseUINode:initPrefabGameObject(path )
特殊方法(添加maskLayer) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 BaseUINode:addMaskLayer(cb, alpha, isPassEvent) BaseUINode.addBlurTexture(color)
四级派生类(UI节点类) BaseUI 继承自BaseUINode, BaseUI: BaseUINode
初始化
注意遗弃的方法:BaseUINode:initPrefabGameObject()
改用 BaseUI:initPanel()
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 BaseUI:initEmptyGameObject(name) BaseUI:initPanel(path ) BaseUI:initPanelGameObject(go)
子父节点
当添加子节点是node是BaseGameNode及派生类时, 使用BaseUI:addSubLayer().
1 2 3 4 BaseUI:addSubLayer(layer)
销毁 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 BaseUI:cleanup() BaseUI:removeSelf() BaseUI:defaultClose() BaseUI:addClickDefaultClose(btnTransform) ``` > 总结: 有关闭按钮就调用``BaseUI:addClickDefaultClose()``, > 需要调用关闭接口就调用``BaseUI.defaultClose()``, 不要调用removeSelf(). ### 特殊方法(UI浮入DOTween) ### ```Lua BaseUI:floatIn(time )
特殊方法(ui放大进入DOTween) 1 2 3 4 5 6 BaseUI:scaleIn(time )
五级派生类(UI节点类) BaseSubUI 销毁 1 2 3 4 5 6 7 BaseSubUI:removeSelf()
不同类的使用规律总结 以下说明了什么情况用什么, 怎么用.
当创建的是类似于玩家头像节点这样的UI时
继承自BaseUINode, 命名为nodeXXX.
节点创建完需要在外部设置父节点
基础定义框架:
A: 公共节点框架(单独一个prefab文件):1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 function NodeXXX:create (param1, param2) local node = NodeXXX:new(param1, param2) return node end function NodeXXX:ctor (param1, param2) BaseUINode.ctor(self ) self ._a = param1 self ._b = param2 self ._nodeGameObject = nil self ._nodeTransform = nil self :init() end function NodeXXX:init () self :initEmptyGameObject("node_XXX" ) local function onDownloadSucc (prefabGameObject) self ._nodeGameObject = prefabGameObject self ._nodeTransform = prefabGameObject.transform self :addChild(nil , self ._nodeTransform) self :initComponents() self :initXXX() self :setXXX() end local function onDownloadFail () logError("NodeXXX load failed, path:" + NodeXXX.PrefabPath) end ResTool:getPrefabGameObjectAsyn(NodeXXX.prefabPath, onDownloadSucc, onDownloadFail, self .pGameObject ) end
B: 某一层的私有节点框架(节点的prefab直接放某一层的prefab里):1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 function NodeXXX:create (parentNode, prefabGameObject, param1, param2) local node = NodeXXX:new(parentNode, prefabGameObject, param1, param2) return node end function NodeXXX:ctor (parentNode, prefabGameObject, param1, param2) BaseUINode.ctor(self ) self ._parentNode = parentNode self ._prefabGameObject = prefabGameObject self ._a = param1 self ._b = param2 self :init() end function NodeXXX:init () if self ._prefabGameObject == nil then return end self :initEmptyGameObject("node_XXX" ) self ._nodeGameObject = UnityEngine.GameObject.Instantiate(self ._prefabGameObject) self ._nodeTransform = self ._nodeGameObject.transform self ._nodeGameObject:SetActive(true ) self :addChild(nil , self ._nodeTransform) self :initComponents() self :initXXX() self :SetXXX() end
当创建是的Dialog弹窗时
继承子BaseSubUI, 命名为XXXDialog
A: 当Dialog是某个系统私有时框架:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 function XXXControl:loadAndShow (param1, param1) local function onLoadSucc () local dialog = XXXDialog:new(param1, param1) XXXControl.addDialog(self , dialog) end local function onLoadFail () logError(XXXDialog.DialogName + " load failed, path:" + XXXDialog.PrefabPath) end XXXDialog:loadRes(onLoadSucc, onLoadFail, true ) end XXXDialog.DialogName = "XXXDialog" function XXXDialog:ctor (param1, param2) BaseSubUI.ctor(self ) self ._a = param1 self ._b = param2 self :_setName(XXXDialog.DialogName) self :init() end function XXXDialog:init () self :initEmptyGameObject("dialog_XXX" ) self :initPanel(XXXDialog.PrefabPath) self :initComponents() self :setXXX() self :refreshXXX() end
B: 当Dialog是公共时框架:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 function XXXDialog:loadAndShow (param1, param2) local function onLoadSucc () local dialog = XXXDialog:new(param1, param2) ViewManager:addDialog(dialog) end local function onLoadFail () logError(XXXDialog.DialogName + " load failed, path:" + XXXDialog.PrefabPath) end XXXDialog:loadRes(onLoadSucc, onLoadFail, true ) end XXXDialog.DialogName = "XXXDialog" function XXXDialog.ctor (param1, param2) BaseSubUI.ctor(self ) self ._a = param1 self ._b = param2 self ._setName(XXXDialog.DialogName) self :init() end function XXXDialog:init () self :initEmptyGameObject("dialog_XXX" ) self :initPanel(XXXDialog.PrefabPath) self :initComponents() self :setXXX() self :refreshXXX() end
当创建的是Tips提示时
继承自BaseSubUI, 命名为XXXTips
不论哪种情况都使用该框架:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 function XXXTips:loadAndShow (param1) local inst = ViewManager:getTipsByName(XXXTips.TipsName) if inst ~= nil then return end local function onLoadSucc () local tips = XXXTips:new(param1) ViewManager:addTips(tips) end local function onLoadFail () logError(XXXTips.TipsName + " load failed, path: " + XXXTips.PrefabPath) end XXXTips:loadRes(onLoadSucc, onLoadFail, true ) end XXXTips.TipsName = "XXXTips" function XXXTips:ctor (param1) BaseSubUI:ctor(self ) self ._a = param1 self :_setName(XXXTips.TipsName) self :init() end function XXXTips:init () self :initEmptyGameObject("tips_XXX" ) self :initPanel(XXXTips.PrefabPath) self :initComponents() self :setXXX() self :refreshXXX() end
当创建的是二级页面UI
继承自BaseSubUI
命名 XXXUI
外部接口create(), 常规initPanel(XXXUI.PrefabPath)