当前位置:网站首页>Unity Xiuxian mobile game | Lua dynamic sliding function (specific implementation of three source codes)
Unity Xiuxian mobile game | Lua dynamic sliding function (specific implementation of three source codes)
2022-07-04 22:41:00 【Rice shrimp】
stay Unity in , There are a lot of UI Reference resources , For details, please refer to the official manual below
https://docs.unity3d.com/cn/current/Manual/script-ScrollRect.html
This time I learned about the making of sliding lists , We are UI-Window So let's make a new one sv Hang up Scroll rectangle (Scroll Rect), Hang up our content that will do , The blue box indicates the scrolling mode , According to the general standard mobile phone resolution , We only check vertical (Vertical) The way .
stay sv Under the Viewport On , Mount our mask, Make sure we click / Moved area
Finally in our content Mount the automatic layout mode : Vertical layout group (Vertical Layout Group)
test_item Of inspector panel (Unity3D Property monitoring panel (Inspector) Unity3D Properties in the property monitoring panel Allow users to change the initial values of scripts and components outside the code .)
To speed up familiarity with business processes UI The design of the , It's a simple implementation lua Implementation of sliding list . The effect is as follows :
Click on our btn_add Buttons can generate list elements for sliding , The attributes and data of these elements can be obtained by local datas = { } To get , We can attach values to each small horizontal list , And through function( ) .. end, feedback btn_add Button click event . This is a better method , It's our local datas = { } Write it into our on_clicked_btn_add(btn) To generate multiple list elements with one key .Lua The code example is as follows :
-- The main function here is to realize clicking btn_add Button one click generation items, The rest will be changed later
--(3) stay datas Middle click to generate sliding list elements
function TestWindow1:on_clicked_btn_add(btn)
local datas = {
{
text = "number:1",
function()
print(" The second generation 1 List elements ")
end,
},
{
text = "number:2",
function()
print(" The second generation 2 List elements ")
end,
},
{
text = "number:3",
function()
print(" The second generation 3 List elements ")
end,
},
{
text = "number:4",
function()
print(" The second generation 4 List elements ")
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) -- Be sure to use item.gameObject, Guarantee unity Corresponding unity
end
end
The other two methods are as follows :
-- TestWindow1.lua
--Lua The variables in are all global variables , In a block or function , Unless with local Explicitly declared as a local variable , The default values of variables are nil
local TestWindow1 = class('TestWindow1', 'UIWindow') -- Define local variables in a function
--Inspector Next TestWindow1 Click generate lua Automatic generation
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) The easiest way to add items-- One by one
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 Loop to generate sliding list elements
--[[
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() -- Build display data
local datas = {
{
text = "number:1",
function()
print(" The second generation 1 List elements ")
end,
},
{
text = "number:2",
function()
print(" The second generation 2 List elements ")
end,
},
{
text = "number:3",
function()
print(" The second generation 3 List elements ")
end,
},
{
text = "number:4",
function()
print(" The second generation 4 List elements ")
end,
},
}
return datas
end
function TestWindow1:get_or_init_test_item(idx) -- Get and initialize item Elements
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) -- initialization item
-- assignment 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
边栏推荐
- Locust性能测试 —— 环境搭建及使用
- 环境加密技术解析
- LOGO特訓營 第一節 鑒別Logo與Logo設計思路
- 如何实现轻松管理1500万员工?
- Shell script implements application service log warehousing MySQL
- Challenges faced by virtual human industry
- 攻防世界 MISC 进阶 glance-50
- Logo special training camp Section V font structure and common design techniques
- 现在mysql cdc2.1版本在解析值为0000-00-00 00:00:00的datetime类
- 制作条形码的手机App推荐
猜你喜欢
共创软硬件协同生态:Graphcore IPU与百度飞桨的“联合提交”亮相MLPerf
堆排序代码详解
蓝队攻防演练中的三段作战
LOGO特训营 第五节 字体结构与设计常用技法
PMO: compare the sample efficiency of 25 molecular optimization methods
2022-07-04:以下go语言代码输出什么?A:true;B:false;C:编译错误。 package main import “fmt“ func main() { fmt.Pri
[acwing] solution of the 58th weekly match
攻防世界 MISC 进阶区 hit-the-core
More than 30 institutions jointly launched the digital collection industry initiative. How will it move forward in the future?
Unity修仙手游 | lua动态滑动功能(3种源码具体实现)
随机推荐
Recommendation of mobile app for making barcode
Tiktok actual combat ~ the number of comments is updated synchronously
制作条形码的手机App推荐
啃下大骨头——排序(二)
The Sandbox 和数字好莱坞达成合作,通过人力资源开发加速创作者经济的发展
关于栈区、堆区、全局区、文字常量区、程序代码区
NFT Insider #64:电商巨头eBay提交NFT相关商标申请,毕马威将在Web3和元宇宙中投入3000万美元
Prosperity is exhausted, things are right and people are wrong: where should personal webmasters go
页面关闭前,如何发送一个可靠请求
微服务--开篇
国产数据库乱象
Introducing QA into the software development lifecycle is the best practice that engineers should follow
30余家机构联合发起数字藏品行业倡议,未来会如何前进?
Practice and principle of PostgreSQL join
攻防世界 MISC 进阶区 3-11
【OpenGL】笔记二十九、抗锯齿(MSAA)
Introduction and application of bigfilter global transaction anti duplication component
通过Go语言创建CA与签发证书
Attack and defense world misc advanced area ditf
Summary of index operations in mongodb