当前位置:网站首页>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边栏推荐
- The proofreading activity of data science on the command line second edition was restarted
- 测试必会:BUG的分类及推进解决
- About stack area, heap area, global area, text constant area and program code area
- ACM multimedia 2022 | counterfactual measurement and elimination of social prejudice in visual language pre training model
- BigFilter全局交易防重组件的介绍与应用
- Solana chain application crema was shut down due to hacker attacks
- leetcode 72. Edit distance edit distance (medium)
- 高中物理:直线运动
- It is said that software testing is very simple, but why are there so many dissuasions?
- 傳智教育|如何轉行互聯網高薪崗比特之一的軟件測試?(附軟件測試學習路線圖)
猜你喜欢

Nat. Commun.| Machine learning jointly optimizes the affinity and specificity of mutagenic therapeutic antibodies

LOGO特训营 第四节 字体设计的重要性
Redis sentinel simply looks at the trade-offs between distributed high availability and consistency

质量体系建设之路的分分合合

Why is Dameng data called the "first share" of domestic databases?

【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用

Radio and television Wuzhou signed a cooperation agreement with Huawei to jointly promote the sustainable development of shengteng AI industry

Enabling digital economy Fuxin software attends the BRICs high level Forum on Sustainable Development

虚拟人产业面临的挑战

AscendEX 上线 Walken (WLKN) - 一款卓越领先的“Walk-to-Earn”游戏
随机推荐
Zhiyang innovation signed a cooperation agreement with Huawei to jointly promote the sustainable development of shengteng AI industry
安装人大金仓数据库
Convolutional neural network model -- lenet network structure and code implementation
It is said that software testing is very simple, but why are there so many dissuasions?
Service online governance
Implementation rules for archiving assessment materials of robot related courses 2022 version
MD5 tool class
Logo Camp d'entraînement section 3 techniques créatives initiales
Mysql root 账号如何重置密码
现在mysql cdc2.1版本在解析值为0000-00-00 00:00:00的datetime类
ACM multimedia 2022 | counterfactual measurement and elimination of social prejudice in visual language pre training model
力扣_回文数
PostgreSQL服务端编程聚合和分组
Jvm-Sandbox-Repeater的部署
Postgresqlql advanced skills pivot table
Locust performance test - environment construction and use
傳智教育|如何轉行互聯網高薪崗比特之一的軟件測試?(附軟件測試學習路線圖)
Now MySQL cdc2.1 is parsing the datetime class with a value of 0000-00-00 00:00:00
质量体系建设之路的分分合合
力扣2_1480. 一维数组的动态和