UIHelper.lua.txt 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. local UIHelper = {}
  2. function UIHelper:OpenUI(scriptPath)
  3. print("UIHelper:OpenUI -> " .. scriptPath)
  4. local luaRoot = CS.UnityEngine.GameObject.Find("LuaRoot")
  5. assert(luaRoot ~= nil)
  6. local uiScriptHolder = luaRoot.transform:Find("uiScriptHolder")
  7. if not uiScriptHolder then
  8. uiScriptHolder = CS.UnityEngine.GameObject("uiScriptHolder")
  9. uiScriptHolder.transform:SetParent(luaRoot.transform)
  10. end
  11. local uiController = require(scriptPath)
  12. local Utils = require("Base/Utils.lua")
  13. local scriptName = Utils:GetScriptNameByScriptPath(scriptPath)
  14. local scriptGameObject = CS.UnityEngine.GameObject(scriptName)
  15. scriptGameObject.transform:SetParent(uiScriptHolder.transform)
  16. CS.SFramework.SLuaComponent.Add(scriptGameObject, uiController, scriptPath)
  17. return uiController
  18. end
  19. function UIHelper:CloseUI(scriptPath)
  20. print("UIHelper:CloseUI -> " .. scriptPath)
  21. local Utils = require("Base/Utils.lua")
  22. local scriptName = Utils:GetScriptNameByScriptPath(scriptPath)
  23. local go = CS.UnityEngine.GameObject.Find("LuaRoot/uiScriptHolder/" .. scriptName)
  24. CS.UnityEngine.GameObject.Destroy(go)
  25. end
  26. function UIHelper:SwitchUI(dstUIControllerPath, srcUIControllerPath)
  27. UIHelper:OpenUI(dstUIControllerPath)
  28. UIHelper:CloseUI(srcUIControllerPath)
  29. end
  30. function UIHelper:AddClickEvent(rootPanel, buttonPath, clickFunc)
  31. print(string.format("UIHelper:AddClickEvent rootPanel.name:%s buttonPath:%s clickFunc:%s", rootPanel.name, buttonPath, clickFunc))
  32. local buttonGameObject = rootPanel.transform:Find(buttonPath).gameObject
  33. buttonGameObject:GetComponent("Button").onClick:AddListener(clickFunc)
  34. end
  35. function UIHelper:AddSliderEvent(rootPanel, sliderPath, valueChangeFunc)
  36. print(string.format("UIHelper:AddSliderEvent rootPanel.name:%s buttonPath:%s clickFunc:%s", rootPanel.name, sliderPath, valueChangeFunc))
  37. local sliderGameObject = rootPanel.transform:Find(sliderPath).gameObject
  38. sliderGameObject:GetComponent("Slider").onValueChanged:AddListener(valueChangeFunc)
  39. end
  40. function UIHelper:AddToggleEvent(rootPanel, togglePath, valueChangeFunc)
  41. print(string.format("UIHelper:AddToggleEvent rootPanel.name:%s buttonPath:%s toggleChangeFunc:%s", rootPanel.name, togglePath, valueChangeFunc))
  42. local toggleGameObject = rootPanel.transform:Find(togglePath).gameObject
  43. toggleGameObject:GetComponent("Toggle").onValueChanged:AddListener(valueChangeFunc)
  44. end
  45. function UIHelper:SetTextInfo(rootPanel, textPath, textInfo)
  46. -- print(string.format("UIHelper:SetTextInfo rootPanel.name:%s buttonPath:%s textInfo:%s", rootPanel.name, textPath, textInfo))
  47. local textGameObject = rootPanel.transform:Find(textPath).gameObject
  48. textGameObject:GetComponent("Text").text = textInfo
  49. end
  50. function UIHelper:LoadSprite(rootPanel, resourcePath, spriteName)
  51. print(string.format("UIHelper:LoadSprite rootPanel.name:%s resourcePath:%s spriteName:%s", rootPanel.name, resourcePath, spriteName))
  52. local texture2D = LoadResource(resourcePath)
  53. local skillSprite = CS.UnityEngine.Sprite.Create(texture2D, CS.UnityEngine.Rect(0,0,texture2D.width, texture2D.height), CS.UnityEngine.Vector2.zero)
  54. skillSprite.name = spriteName
  55. rootPanel.transform:GetComponent("Image").sprite = skillSprite
  56. end
  57. local function OnClickButtonQuit(bBackToAndroid, sOpenPath, sClosePath)
  58. if bBackToAndroid then
  59. CS.UnityEngine.Application.Quit()
  60. return
  61. end
  62. local openUI = require(sOpenPath)
  63. local closeUI = require(sClosePath)
  64. openUI.SetRootPanelActive(true)
  65. closeUI.SetRootPanelActive(false)
  66. -- UIHelper:SwitchUI(sOpenPath, sClosePath)
  67. end
  68. function UIHelper:RegisterButtonQuit(rootPanel, bBackToAndroid, sOpenPath, sClosePath)
  69. print(string.format("UIHelper.RegisterButtonQuit rootPanel.name:%s bBackToAndroid:%s, sOpenPath:%s, sClosePath:%s",
  70. rootPanel.name, bBackToAndroid, sOpenPath, sClosePath))
  71. UIHelper:AddClickEvent(rootPanel, "ButtonQuit", function ()
  72. OnClickButtonQuit(bBackToAndroid, sOpenPath, sClosePath)
  73. end)
  74. end
  75. return UIHelper