当前位置:网站首页>Unity修仙手游 | lua动态滑动功能(3种源码具体实现)
Unity修仙手游 | lua动态滑动功能(3种源码具体实现)
2022-07-04 22:17:00 【米莱虾】
在 Unity 中,有很多的 UI 参考,具体可以参照下方的官方手册
https://docs.unity3d.com/cn/current/Manual/script-ScrollRect.html

此次了解的是滑动列表的制作,我们在 UI-Window 下新建一个 sv 挂上 滚动矩形 (Scroll Rect),再挂上我们的 content 即可,蓝色框表示滚动方式,按照一般标准的手机分辨率,我们只勾选垂直(Vertical)方式。


在 sv 下的 Viewport上,挂载上我们的 mask,确保我们鼠标点击/移动的区域

最后在我们的 content 上挂载自动布局方式:垂直布局组 (Vertical Layout Group)

test_item的inspector面板(Unity3D属性监视面板 (Inspector) Unity3D属性监视面板中的属性允许用户在代码外部改变脚本及组件的初始值。)

为了加快熟悉业务程序 UI 的设计,简单实现了一下 lua 滑动列表的实现。实现的效果如下:


点击我们的 btn_add 按钮可以生成可供滑动的列表元素,这些元素的属性和数据可以通过 local datas = { } 来获取,我们可以在里面给每个小的横向列表附值,并且通过 function( ) .. end, 反馈 btn_add 按钮的点击事件。这是比较好的一种方法,就是将我们的 local datas = { } 写进我们的 on_clicked_btn_add(btn) 来一键生成多个列表元素。Lua 代码示例如下:
-- 这里面的功能主要是实现点击btn_add按钮一键生成items,其余稍后改动
--(3)在datas中一键生成滑动列表元素
function TestWindow1:on_clicked_btn_add(btn)
local datas = {
{
text = "number:1",
function()
print("生成的第1个列表元素")
end,
},
{
text = "number:2",
function()
print("生成的第2个列表元素")
end,
},
{
text = "number:3",
function()
print("生成的第3个列表元素")
end,
},
{
text = "number:4",
function()
print("生成的第4个列表元素")
end,
},
}
item.text_name.text = TT("test!!!!!")
item.go:SetActive(true)
for idx, data in ipairs(datas) do
local item = self:get_or_init_test_item(idx)
item.text_name.text = data.name
item.text_name.text = TT(data.text)
item.go:SetActive(true)
end
end
function TestWindow1:on_clicked_btn_minus(btn)
local item = self.test_items[1]
if item then
table.remove(self.test_items, 1)
GameObject.Destroy(item.gameObject) --一定要用item.gameObject,保证unity对应unity
end
end其他两种方法分别如下:
-- TestWindow1.lua
--Lua 中的变量全是全局变量,无论语句块或是函数里,除非用 local 显式声明为局部变量,变量默认值均为nil
local TestWindow1 = class('TestWindow1', 'UIWindow') --定义一个函数内的局部变量
--Inspector下TestWindow1下点击生成lua自动生成
function TestWindow1:__awake(name)
self:load('TestWindow1')
self.btn_add = self:fcc('btn_add', UT_BTN)
self:listen_button_clicked(self.btn_add, function(btn) self:on_clicked_btn_add(btn) end)
self.btn_minus = self:fcc('btn_minus', UT_BTN)
self:listen_button_clicked(self.btn_minus, function(btn) self:on_clicked_btn_minus(btn) end)
self.test_item = self:fco('test_item')
self.content = self:fcc('sv/Viewport/content', UT_Rect)
self.btn_bcak = self:fcc('btn_bcak', UT_BTN)
self:listen_button_clicked(self.btn_bcak, function(btn) self:on_clicked_btn_bcak(btn) end)
if self.awake then self:awake() end
end
function TestWindow1:on_clicked_btn_bcak(btn)
self:hide()
end
-------------------------------------------------------------------------------------------------------------------------
--(1)最简单的方法添加items--一个个加
function TestWindow1:on_clicked_btn_add(btn)
local item = GameObject.Instantiate(self.test_item, self.content)
item:SetActive(true)
table.insert(self.test_items, item)
end
-------------------------------------------------------------------------------------------------------------------------
--(2)for循环生成滑动列表元素
--[[
function TestWindow1:on_clicked_btn_add(btn)
item.text_name.text = TT("test!!!!!")
item.go:SetActive(true)
for i=1 , 5 do
local item = self:get_or_init_test_item(i)
item.text_name.text = TT(i)
item.go:SetActive(true)
end
end
]]--
-------------------------------------------------------------------------------------------------------------------------
function TestWindow1:on_clicked_btn_minus(btn)
local item = self.test_items[1]
if item then
table.remove(self.test_items, 1)
GameObject.Destroy(item.gameObject)
end
end
local test_icon_path = "UI/SkillIcon/5123.png"
function TestWindow1:build_test_data() --构建显示数据
local datas = {
{
text = "number:1",
function()
print("生成的第1个列表元素")
end,
},
{
text = "number:2",
function()
print("生成的第2个列表元素")
end,
},
{
text = "number:3",
function()
print("生成的第3个列表元素")
end,
},
{
text = "number:4",
function()
print("生成的第4个列表元素")
end,
},
}
return datas
end
function TestWindow1:get_or_init_test_item(idx) --获取和初始化item元素
local item = self.test_items[idx]
if not item then
local go = GameObject.Instantiate(self.test_item, self.content)
item = {
go = go,
text_name = fcc(go, "text_name", UT_TMP),
img_icon = fcc(go, "img_icon", UT_IMAGE),
btn_go = fco(go, "btn_go"),
text_btn = fco(go, "btn_go/text_btn"),
}
self.test_items[idx] = item
end
return item
end
function TestWindow1:awake()
self.test_items = {}
end
function TestWindow1:refresh()
self.datas = self:build_test_data()
log_warn("test window refresh datas: %s", pts(self.datas))
for idx, data in ipairs(self.datas or {}) do
log_warn(" refresh item: %s", pts(data))
local item = self:get_or_init_test_item(idx) --初始化item
--赋值item
item.text_name.text = data.text
item.img_icon.sprite = self:load_sprite(string.format(test_icon_path, data.icon))
local show_btn = data.btn_text ~= nil or data.btn_text ~= ""
item.btn_go.gameObject:SetActive(show_btn)
if show_btn then
self:listen_button_clicked(
item.btn_go,
function(btn)
data[1]()
end
)
end
item.go:SetActive(true)
end
end
function TestWindow1:on_show()
log_warn("test window on show")
self:refresh()
end
function TestWindow1:on_hide()
USER:unlisten_field_changed(self)
end
function TestWindow1:dispose()
end边栏推荐
- LOGO特训营 第三节 首字母创意手法
- Naacl-22 | introduce the setting of migration learning on the prompt based text generation task
- Introduction and application of bigfilter global transaction anti duplication component
- 10 schemes to ensure interface data security
- Postgresqlql advanced skills pivot table
- Zhiyang innovation signed a cooperation agreement with Huawei to jointly promote the sustainable development of shengteng AI industry
- A large number of virtual anchors in station B were collectively forced to refund: revenue evaporated, but they still owe station B; Jobs was posthumously awarded the U.S. presidential medal of freedo
- PHP short video source code, thumb animation will float when you like it
- 面试必备 LeetCode 链表算法题汇总,全程干货!
- Flask 上下文详解
猜你喜欢

Éducation à la transmission du savoir | Comment passer à un test logiciel pour l'un des postes les mieux rémunérés sur Internet? (joindre la Feuille de route pour l'apprentissage des tests logiciels)

Tla+ introductory tutorial (1): introduction to formal methods

More than 30 institutions jointly launched the digital collection industry initiative. How will it move forward in the future?

【OpenGL】笔记二十九、抗锯齿(MSAA)

10 schemes to ensure interface data security

LOGO特訓營 第一節 鑒別Logo與Logo設計思路

Energy momentum: how to achieve carbon neutralization in the power industry?

LOGO特训营 第一节 鉴别Logo与Logo设计思路

都说软件测试很简单有手就行,但为何仍有这么多劝退的?

Ascendex launched Walken (WLKN) - an excellent and leading "walk to earn" game
随机推荐
ApacheCN 翻译、校对、笔记整理活动进度公告 2022.7
Service online governance
Force buckle_ Palindrome number
Use blocconsumer to build responsive components and monitor status at the same time
Ascendex launched Walken (WLKN) - an excellent and leading "walk to earn" game
LOGO特训营 第四节 字体设计的重要性
Logo special training camp section 1 Identification logo and logo design ideas
PHP short video source code, thumb animation will float when you like it
Enabling digital economy Fuxin software attends the BRICs high level Forum on Sustainable Development
测试必会:BUG的分类及推进解决
LOGO特训营 第一节 鉴别Logo与Logo设计思路
[the 2023 autumn recruitment of MIHA tour] open [the only exclusive internal push code of school recruitment eytuc]
《命令行上的数据科学第二版》校对活动重新启动
Logo special training camp section II collocation relationship between words and graphics
Zhiyang innovation signed a cooperation agreement with Huawei to jointly promote the sustainable development of shengteng AI industry
Kdd2022 | what features are effective for interaction?
传智教育|如何转行互联网高薪岗位之一的软件测试?(附软件测试学习路线图)
sqlserver对数据进行加密、解密
How to manage 15million employees easily?
leetcode 72. Edit Distance 编辑距离(中等)