当前位置:   article > 正文

Unity | 常用动态UI_Demo(附源码)_unity demo源码

unity demo源码

☀️前言介绍 

为了增加制作UI及其动态效果的熟练度,前两天就修仙游戏题材,使用Unity+VsCode搞了个小Demo,主要涉及:

❥ 滚动矩形 (Scroll Rect)

❥ 输入字段 (Input Field)

❥ 滚动条 (Scrollbar)

❥ 开关组 (Toggle Group)

❥ 按钮 (Button)

❥ 自动布局 (Layout)

❥ 可视组件 (visual Component)

❥ 红点系统 (Redpoint)

...

 ☀️Demo效果呈现

录屏用了MS商店自带的ScreenToGif转了格式,流畅度和颜色效果变得有点差

☀️具体流程回顾

有关最基础的滑动列表的制作(好看点的主要是在代码中动态获取了加入的美术资源),我们在 UI-Window 下新建一个 sv 挂上 滚动矩形 (Scroll Rect),再挂上我们的 content 即可,蓝色框表示滚动方式,按照一般标准的手机分辨率,我们只勾选垂直(Vertical)方式。

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

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

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

简单 lua 滑动列表的实现效果如下:

点击我们的 btn_add 按钮可以生成可供滑动的列表元素,这些元素的属性和数据可以通过 local datas = { } 来获取,我们可以在里面给每个小的横向列表附值,并且通过 function( ) .. end, 反馈 btn_add 按钮的点击事件。这是比较好的一种方法,就是将我们的 local datas = { } 写进我们的 on_clicked_btn_add(btn) 来一键生成多个列表元素。

其他的后续补充ing ...


☀️Lua主要实现代码

  1. -- TestWindow1.lua
  2. ---- DO NOT CHANGE BEGIN ----
  3. local TestWindow1 = class('TestWindow1', 'UIWindow')
  4. function TestWindow1:__awake(name)
  5. self:load('TestWindow1')
  6. self.btn_add = self:fcc('btn_add', UT_BTN)
  7. self:listen_button_clicked(self.btn_add, function(btn) self:on_clicked_btn_add(btn) end)
  8. self.btn_minus = self:fcc('btn_minus', UT_BTN)
  9. self:listen_button_clicked(self.btn_minus, function(btn) self:on_clicked_btn_minus(btn) end)
  10. self.test_item_drop = self:fco('test_item_drop')
  11. self.content = self:fcc('sv/Viewport/content', UT_Rect)
  12. self.btn_back = self:fcc('btn_back', UT_BTN)
  13. self:listen_button_clicked(self.btn_back, function(btn) self:on_clicked_btn_back(btn) end)
  14. self.btn_kfu = self:fcc('btn_kfu', UT_BTN)
  15. self:listen_button_clicked(self.btn_kfu, function(btn) self:on_clicked_btn_kfu(btn) end)
  16. self.btn_kfuget = self:fcc('btn_kfuget', UT_BTN)
  17. self:listen_button_clicked(self.btn_kfuget, function(btn) self:on_clicked_btn_kfuget(btn) end)
  18. self.panel_kfu = self:fco('sv/panel_kfu')
  19. self.panel_kfuget = self:fco('panel_kfuget')
  20. self.Toggle = self:fco('btn_kfu/Toggle')
  21. self.test_item = self:fco('test_item')
  22. self.txt_title = self:fcc('panel_kfuget/txt_title', UT_TMP)
  23. self.content_panel = self:fcc('panel_kfuget/sv_panel/vp_panel/content_panel', UT_Rect)
  24. self.test_item2 = self:fco('test_item2')
  25. self.btn_add_panel = self:fcc('panel_kfuget/btn_add_panel', UT_BTN)
  26. self:listen_button_clicked(self.btn_add_panel, function(btn) self:on_clicked_btn_add_panel(btn) end)
  27. self.text_name2 = self:fcc('test_item2/text_name2', UT_TMP)
  28. self.text_name = self:fcc('test_item/text_name', UT_TMP)
  29. self.text_status = self:fcc('test_item2/text_status', UT_TMP)
  30. if self.awake then self:awake() end
  31. end
  32. ---- DO NOT CHANGE END ----
  33. --"
  34. local test_icon_path = "UI/MonsterIcon/41018.png"
  35. local test_icon_path1 = "UI/NpcIcon/41018.png"
  36. local test_icon_path2 = "UI/MonsterIcon/41024.png"
  37. local test_icon_path3 = "UI/MonsterIcon/41026.png"
  38. local test_icon_path4 = "UI/HeroIcon/30001.png"
  39. local pages = {
  40. kfu = 1, -- 武功
  41. kfuget = 2, -- 习得武功
  42. }
  43. local default_page = pages.kfuget
  44. function TestWindow1:awake()
  45. self.test_items = {}
  46. self:init_widgets()
  47. -- self:bind_red_point()
  48. end
  49. function TestWindow1:init_widgets()
  50. self.kfu_widget = self:load_widget("KfuWidget", self.panel_kfu)
  51. self.kfuget_widget = self:load_widget("KfuGetWidget", self.panel_kfuget)
  52. end
  53. function TestWindow1:on_hide()
  54. ClientSaveD.save_cur_login_data("got_kfuget", true)
  55. if ClientSaveD.get_cur_login_data("enter_kfu_widget") then
  56. ClientSaveD.save_cur_login_data("exit_kfu_widget", true)
  57. end
  58. GongfaRed.check_all_red()
  59. USER:unlisten_field_changed(self)
  60. end
  61. function TestWindow1:on_show(page_idx)
  62. self:refresh()
  63. self:refresh2()
  64. self.cur_page = page_idx or default_page
  65. self:open_widget(self.cur_page)
  66. self.txt_title.text = "好友如下"
  67. end
  68. --打开对应组件
  69. function TestWindow1:open_widget(page_idx)
  70. self.panel_kfu:SetActive(page_idx == pages.kfu) --根据布尔值点亮unity中的某个组件
  71. self.panel_kfuget:SetActive(page_idx == pages.kfuget)
  72. if page_idx == pages.kfuget then
  73. PanelOpenLogD.log_one_login_open("kfuget_open_log")
  74. elseif page_idx == pages.kfu then
  75. self:set_kfu_flag()
  76. PanelOpenLogD.log_one_login_open("kfu_open_log")
  77. end
  78. end
  79. function TestWindow1:set_kfu_flag()
  80. local data = DailyFlagD.query_flag("kfu")
  81. if data then
  82. return
  83. end
  84. DailyFlagD.set_flag("kfu", true)
  85. end
  86. function TestWindow1:on_clicked_btn_back(btn)
  87. self:hide()
  88. end
  89. function TestWindow1:on_clicked_btn_kfu(btn)
  90. self:open_widget(pages.kfu)
  91. print("!!打开功法界面!!")
  92. end
  93. function TestWindow1:on_clicked_btn_kfuget(btn)
  94. self:open_widget(pages.kfuget)
  95. print("!!打开功法领悟界面!!")
  96. end
  97. function TestWindow1:on_clicked_btn_add(btn)
  98. local datas = {
  99. {
  100. text = "number:1",
  101. function()
  102. print("生成的第1个列表元素")
  103. end,
  104. },
  105. {
  106. text = "number:2",
  107. function()
  108. print("生成的第2个列表元素")
  109. end,
  110. },
  111. {
  112. text = "number:3",
  113. function()
  114. print("生成的第3个列表元素")
  115. end,
  116. },
  117. {
  118. text = "number:4",
  119. function()
  120. print("生成的第4个列表元素")
  121. end,
  122. },
  123. }
  124. for idx, data in ipairs(datas) do
  125. local item = self:get_or_init_test_item(idx)
  126. item.text_name.text = data.name
  127. item.text_name.text = TT(data.text)
  128. item.go:SetActive(true)
  129. end
  130. print("列表生成完毕,可以滑动")
  131. end
  132. function TestWindow1:on_clicked_btn_add_panel(btn)
  133. local panel_datas = {
  134. {
  135. text = "Archer",
  136. text1 = "在线",
  137. icon = test_icon_path,
  138. function()
  139. print("生成的第1个列表元素")
  140. end,
  141. },
  142. {
  143. text = "Saber",
  144. text1 = "忙碌",
  145. icon = test_icon_path1,
  146. function()
  147. print("生成的第2个列表元素")
  148. end,
  149. },
  150. {
  151. text = "Rabbit",
  152. text1 = "离开",
  153. icon = test_icon_path2,
  154. function()
  155. print("生成的第3个列表元素")
  156. end,
  157. },
  158. {
  159. text = "Wolf",
  160. text1 = "勿扰",
  161. icon = test_icon_path3,
  162. function()
  163. print("生成的第4个列表元素")
  164. end,
  165. },
  166. {
  167. text = "Hero",
  168. text1 = "Q我",
  169. icon = test_icon_path4,
  170. function()
  171. print("生成的第5个列表元素")
  172. end,
  173. },
  174. }
  175. for i, panel_data in ipairs(panel_datas) do
  176. local item2 = self:get_or_init_test_item_panel(i)
  177. item2.text_name2.text = panel_data.name
  178. item2.text_name2.text = TT(panel_data.text)
  179. item2.text_status.text = panel_data.name
  180. item2.text_status.text = TT(panel_data.text1)
  181. item2.img_avator.sprite = self:load_sprite( string.format(panel_data.icon) )
  182. item2.go:SetActive(true)
  183. end
  184. print("工会成员列表生成完毕,可以滑动")
  185. end
  186. function TestWindow1:on_clicked_btn_minus(btn)
  187. local item = self.test_items[1]
  188. if item then
  189. table.remove(self.test_items, 1)
  190. GameObject.Destroy(item.gameObject)
  191. end
  192. end
  193. function TestWindow1:get_or_init_test_item(idx)
  194. local item = self.test_items[idx]
  195. if not item then
  196. local go = GameObject.Instantiate(self.test_item, self.content)
  197. item = {
  198. go = go,
  199. text_name = fcc(go, "text_name", UT_TMP), -- 全局的FindChildComponent:function fcc(game_ob, path, comp)
  200. img_icon = fcc(go, "img_icon", UT_IMAGE), -- 全局的findChildObject
  201. btn_go = fco(go, "btn_go"),
  202. text_btn = fco(go, "btn_go/text_btn"),
  203. }
  204. self.test_items[idx] = item
  205. end
  206. return item
  207. end
  208. function TestWindow1:get_or_init_test_item_panel(i)
  209. local item2 = self.test_items[i]
  210. if not item2 then
  211. local go = GameObject.Instantiate(self.test_item2, self.content_panel)
  212. item2 = {
  213. go = go,
  214. text_name2 = fcc(go, "text_name2", UT_TMP),
  215. text_status = fcc(go, "text_status", UT_TMP),
  216. img_avator = fcc(go, "img_avator", UT_IMAGE),
  217. btn_chat = fco(go, "btn_chat"),
  218. text_btn = fco(go, "btn_chat/text_btn_chat"),
  219. }
  220. self.test_items[i] = item2
  221. end
  222. return item2
  223. end
  224. function TestWindow1:refresh()
  225. for idx, data in ipairs(self.datas or {}) do
  226. local item = self:get_or_init_test_item(idx)
  227. item.text_name.text = data.text
  228. item.img_icon.sprite = self:load_sprite(string.format(test_icon_path, data.icon))
  229. local show_btn = data.btn_text ~= nil or data.btn_text ~= ""
  230. item.btn_go.gameObject:SetActive(show_btn)
  231. if show_btn then
  232. self:listen_button_clicked(
  233. item.btn_go,
  234. function(btn)
  235. data[1]()
  236. end
  237. )
  238. end
  239. item.go:SetActive(true)
  240. end
  241. end
  242. function TestWindow1:refresh2()
  243. --self.datas = self:build_test_data()
  244. --log_warn("test window refresh datas: %s", pts(self.datas))
  245. for i, panel_data in ipairs(self.panel_datas or {}) do
  246. --log_warn(" refresh item: %s", pts(data))
  247. local item2 = self:get_or_init_test_item2(i)
  248. item2.text_name2.text = panel_data.text
  249. item2.text_status.text = panel_data.text1
  250. item2.img_avator.sprite = self:load_sprite(string.format(test_icon_path, panel_data.icon))
  251. local show_btn2 = panel_data.btn_text ~= nil or panel_data.btn_text ~= ""
  252. item2.btn_go.gameObject:SetActive(show_btn2)
  253. if show_btn2 then
  254. self:listen_button_clicked(
  255. item2.btn_go,
  256. function(btn)
  257. panel_data[1]()
  258. end
  259. )
  260. end
  261. item2.go:SetActive(true)
  262. end
  263. end
  264. function TestWindow1:dispose()
  265. end
  266. -- -- 绑定红点系统
  267. -- function TestWindow1:bind_red_point()
  268. -- --绑定功法红点
  269. -- RedPointD.bind_ui(RedPointConst.Kfu, fco(self.btn_kfu.gameObject, "red_point"))
  270. -- RedPointD.bind_ui(RedPointConst.KfuGet, fco(self.btn_kfuget.gameObject, "red_point"))
  271. -- end

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/661814
推荐阅读
相关标签
  

闽ICP备14008679号