当前位置:网站首页>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
endThe 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边栏推荐
- Logo special training camp Section IV importance of font design
- Logo special training camp section 1 Identification logo and logo design ideas
- How diff are the contents of the same configuration item in different environments?
- idea中pom.xml依赖无法导入
- Naacl-22 | introduce the setting of migration learning on the prompt based text generation task
- Mysql root 账号如何重置密码
- LOGO特训营 第五节 字体结构与设计常用技法
- BigFilter全局交易防重组件的介绍与应用
- Mongodb aggregation operation summary
- Attack and defense world misc advanced grace-50
猜你喜欢

Naacl-22 | introduce the setting of migration learning on the prompt based text generation task

Ascendex launched Walken (WLKN) - an excellent and leading "walk to earn" game

攻防世界 MISC 高手进阶区 001 normal_png

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

将QA引入软件开发生命周期是工程师要遵循的最佳实践

Lost in the lock world of MySQL

Attack and defense world misc advanced grace-50

串口数据帧

NFT insider 64: e-commerce giant eBay submitted an NFT related trademark application, and KPMG will invest $30million in Web3 and metauniverse

【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用
随机推荐
Force buckle_ Palindrome number
Convolutional neural network model -- lenet network structure and code implementation
【OpenGL】笔记二十九、抗锯齿(MSAA)
虚拟人产业面临的挑战
Business is too busy. Is there really no reason to have time for automation?
繁华落尽、物是人非:个人站长该何去何从
关于栈区、堆区、全局区、文字常量区、程序代码区
攻防世界 MISC 进阶区 Erik-Baleog-and-Olaf
特征缩放 标准化 归一化
通过Go语言创建CA与签发证书
LOGO特训营 第三节 首字母创意手法
Easy to use app recommendation: scan QR code, scan barcode and view history
Google Earth Engine(GEE)——Tasks升级,实现RUN ALL可以一键下载任务类型中的所有影像
sobel过滤器
Logo special training camp section III initial creative techniques
醒悟的日子,我是怎么一步一步走向软件测试的道路
[the 2023 autumn recruitment of MIHA tour] open [the only exclusive internal push code of school recruitment eytuc]
记录:关于Win10系统中Microsoft Edge上的网页如何滚动截屏?
Shell 脚本实现应用服务日志入库 Mysql
Interview essential leetcode linked list algorithm question summary, whole process dry goods!