Unity中拼UI说明
命名规则
- 面板prefab名称一般以Panel开头(如:PenelLogin),列表子项以Item开头(如:ItemShopGood)
- prefab中所有gameObject名字只包含大小写字母和下划线
- 需要程序操作的大写开头,不需要程序取的小写开头,单词第一个字母大写
- 关闭按钮统一命名BtnClose
- 不用组件用对应的前缀开头:
Trans Transform
Go GameObject
Btn GameObject(导出脚本回生成对应的按钮点击处理函数)
Txt Text
Inf InputField
Spr Image
Tex RawImage
Grid GridDynamic
Spine SkeletonGraphic
Grid列表处理
- grid的Item预制体命名以Item开头
- 在列表绑定的父节点上挂GridDynamic组件,将要实例化的item的prefab赋值给GridDynamic的goItemPrefabs字段,这里支持多个item,同一个列表下面生成不同的item
- item一般通过节点上面GridLayoutGroup和ContentSizeFitter排版
- 预览item填充的效果,GridDynamic组件右键InstantiateItem命令可以生成count数量的item,预览列表填充效果。
- GridDynamic的CreateItem方法默认生成goItemPrefabs中的第一个item的预制体的实例,不是第一个的可以通过CreateItemByIdx方法创建
lua代码生成
- 选中已经拼好UI的prefab预制文件,执行Tools/ExportUIScript命令,生成该UI的代码模板(PenelLogin.lua),将代码文件复制到对应的lua代码目录下并完成功能
多语言相关
- UI的预制体增加对应语言后缀(如:PenelLogin_en.prefab, PenelLogin_tw.prefab),代码使用同一份。
生成代码示例:
--UnityEditor 自动生成
local LuaUtil = LuaUtil;
local UIUtils = UIUtils;
---@class ItemShopGood
local ItemShopGood = class("ItemShopGood");
function ItemShopGood:ctor(parent, goRoot, idx)
self.parent = parent
self.gameObject = goRoot;
self.index = idx;
self.txtName = LuaUtil.GetUIText(self.gameObject, "TxtName");
self.txtCount = LuaUtil.GetUIText(self.gameObject, "OriginalCount/TxtCount");
self.btnBuy = LuaUtil.GetUIButton(self.gameObject, "BtnBuy");
LuaUtil.AddClickEventB(self.btnBuy, handler(self.OnBtnBuy, self));
end
function ItemShopGood:UpdateData(itemData)
--TODO updateUI with data
print("updateItemUI with data not implement");
end
function ItemShopGood:OnBtnBuy(btn)
--TODO
print("ItemShopGood OnBuy");
end
function ItemShopGood:OnDestroy()
UIUtils.RemoveAllEventListener(self);
self.txtName = nil;
self.txtCount = nil;
self.btnBuy = nil;
end
---@class PanelShopTest
local PanelShopTest = class("PanelShopTest");
function PanelShopTest:Start()
self.txtTitle = LuaUtil.GetUIText(self.gameObject, "UI/BackGround/TxtTitle");
self.gridShopGood = LuaUtil.GetGridDynamic(self.gameObject, "UI/ScrollView/Viewport/GridShopGood");
self.listShopGood = {};
self.btnClose = LuaUtil.GetUIButton(self.gameObject, "BtnClose");
LuaUtil.AddClickEventB(self.btnClose, handler(self.OnBtnClose, self));
end
function PanelShopTest:CreateShopGoodList(dataList)
self:DestroyShopGoodList();
local itemShopGood;
for __idx=1,#dataList do
itemShopGood = ItemShopGood.new(self, self.gridShopGood:CreateItem(), __idx);
table.insert(self.listShopGood, itemShopGood);
itemShopGood:UpdateData(dataList[__idx]);
end
end
function PanelShopTest:DestroyShopGoodList()
for __idx=1,#self.listShopGood do
self.listShopGood[__idx]:OnDestroy();
end
table.clear(self.listShopGood);
end
function PanelShopTest:OnBtnClose(btn)
UIPanelManager.ClosePanelT(self);
end
function PanelShopTest:OnDestroy()
UIUtils.RemoveAllEventListener(self);
self.txtTitle = nil;
self:DestroyShopGoodList();
self.btnClose = nil;
end
return PanelShopTest;