UIHelper.lua.txt 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. function UIHelper:AddScript(rootPanel, szScriptPath)
  58. local controller = require(szScriptPath)
  59. CS.SFramework.SLuaComponent.Add(rootPanel, controller, szScriptPath)
  60. end
  61. function UIHelper:IsPointerOverGameObject()
  62. local eventSystem = CS.UnityEngine.EventSystems.EventSystem
  63. local eventData = CS.UnityEngine.EventSystems.PointerEventData(eventSystem.current)
  64. local mousePosition = CS.UnityEngine.Input.mousePosition
  65. local mousePos = CS.UnityEngine.Vector2(mousePosition.x, mousePosition.y)
  66. eventData.position = mousePos
  67. local listRaycast = CS.System.Collections.Generic.List(CS.UnityEngine.EventSystems.RaycastResult)()
  68. eventSystem.current:RaycastAll(eventData, listRaycast)
  69. return listRaycast.Count > 0
  70. end
  71. function UIHelper:SetButtonInteractable(rootPanel, buttonPath, bEnable)
  72. local button = rootPanel.transform:Find(buttonPath).gameObject
  73. button:GetComponent("Button").interactable = bEnable
  74. end
  75. function UIHelper:SetUIComponentEnable(rootPanel, uiPath, szComponent, bEnable)
  76. local uiGameObject = rootPanel
  77. if uiPath then
  78. uiGameObject = rootPanel.transform:Find(uiPath).gameObject
  79. end
  80. local uiComponent = uiGameObject.transform:GetComponent(szComponent)
  81. uiComponent.enabled = bEnable
  82. end
  83. function UIHelper:SetTextMeshInfo(rootPanel, textPath, textInfo)
  84. local textMeshObj = rootPanel.transform:Find(textPath).gameObject
  85. textMeshObj:GetComponent("TextMesh").text = textInfo
  86. end
  87. function UIHelper:AddTextChangedEvent(rootPanel, inputFieldPath, valueChangeFunc)
  88. print(string.format("UIHelper:AddTextInputEvent rootPanel.name:%s inputFieldPath:%s", rootPanel.name, inputFieldPath))
  89. local inputGameObject = rootPanel.transform:Find(inputFieldPath).gameObject
  90. inputGameObject:GetComponent("InputField").onValueChanged:AddListener(valueChangeFunc)
  91. end
  92. return UIHelper