UIHelper.lua.txt 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 = CS.SFramework.SResourceManager.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. return UIHelper