当前位置:网站首页>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边栏推荐
- 面试必备 LeetCode 链表算法题汇总,全程干货!
- UML图记忆技巧
- 环境加密技术解析
- Force buckle 3_ 383. Ransom letter
- 好用app推荐:扫描二维码、扫描条形码并查看历史
- The table is backed up in ODPs. Why check m in the metabase_ Table, the logical sizes of the two tables are inconsistent, but the number of
- Li Kou 98: verify binary search tree
- 将QA引入软件开发生命周期是工程师要遵循的最佳实践
- Attack and Defense World MISC Advanced Area Erik baleog and Olaf
- 30余家机构联合发起数字藏品行业倡议,未来会如何前进?
猜你喜欢

Mongodb aggregation operation summary
How to send a reliable request before closing the page
![[Yugong series] go teaching course 003-ide installation and basic use in July 2022](/img/65/b36ca239e06a67c01d1eb0b4648f71.png)
[Yugong series] go teaching course 003-ide installation and basic use in July 2022

Hit the core in the advanced area of misc in the attack and defense world

共创软硬件协同生态:Graphcore IPU与百度飞桨的“联合提交”亮相MLPerf

国产数据库乱象

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

LOGO special training camp section I identification logo and Logo Design Ideas

Lost in the lock world of MySQL

Redis的持久化机制
随机推荐
The Sandbox 和数字好莱坞达成合作,通过人力资源开发加速创作者经济的发展
质量体系建设之路的分分合合
MD5 tool class
The sandbox has reached a cooperation with digital Hollywood to accelerate the economic development of creators through human resource development
嵌入式开发:技巧和窍门——提高嵌入式软件代码质量的7个技巧
POM in idea XML dependency cannot be imported
好用app推荐:扫描二维码、扫描条形码并查看历史
High school physics: linear motion
攻防世界 misc 高手进阶区 a_good_idea
虚拟人产业面临的挑战
2022-07-04: what is the output of the following go language code? A:true; B:false; C: Compilation error. package main import “fmt“ func main() { fmt.Pri
LOGO特训营 第三节 首字母创意手法
Google Earth Engine(GEE)——以MODIS/006/MCD19A2为例批量下载逐天AOD数据逐天的均值、最大值、最小值、标准差、方差统计分析和CSV下载(北京市各区为例)
国产数据库乱象
醒悟的日子,我是怎么一步一步走向软件测试的道路
Flask 上下文详解
Close system call analysis - Performance Optimization
Microservices -- Opening
Mongodb aggregation operation summary
Redis sentinel simply looks at the trade-offs between distributed high availability and consistency