UIHelper.lua.txt 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. function UIHelper:IsPointerOverGameObject(mousePos)
  58. -- local eventSystem = CS.UnityEngine.EventSystems.EventSystem
  59. -- local eventData = CS.UnityEngine.EventSystems.PointerEventData(eventSystem.current)
  60. -- eventData.position = mousePos
  61. -- local listRaycast = CS.System.Collections.Generic.List(CS.UnityEngine.EventSystems.RaycastResult)()
  62. -- eventSystem.current:RaycastAll(eventData, listRaycast)
  63. local eventSystem = CS.UnityEngine.EventSystems.EventSystem
  64. local eventData = CS.UnityEngine.EventSystems.PointerEventData(eventSystem.current)
  65. local RaycastInCanvas = CS.UnityEngine.GameObject.Find("Canvas"):GetComponent("GraphicRaycaster")
  66. eventData.pressPosition = CS.UnityEngine.Input.mousePosition
  67. eventData.position = CS.UnityEngine.Input.mousePosition
  68. local listRaycast = CS.System.Collections.Generic.List(CS.UnityEngine.EventSystems.RaycastResult)()
  69. RaycastInCanvas:Raycast(eventData, listRaycast)
  70. local bTouchUI = false
  71. if listRaycast.Count ~= 0 then
  72. for _, list in pairs(listRaycast) do
  73. print("----> list.name", list.gameObject.name)
  74. end
  75. -- local rawImageRootPanel = require("UI/ZUIPanelPlayerMove.lua").rootPanel
  76. -- local rawImage = rawImageRootPanel.transform:Find("RawImage").gameObject
  77. -- local firstRaycast = listRaycast[0].gameObject
  78. -- bTouchUI = rawImage.name ~= firstRaycast.name
  79. local firstRaycast = listRaycast[0].gameObject
  80. bTouchUI = firstRaycast.name == "RawImage"
  81. end
  82. return bTouchUI
  83. end
  84. function UIHelper:SetButtonInteractable(rootPanel, buttonPath, bEnable)
  85. local button = rootPanel.transform:Find(buttonPath).gameObject
  86. button:GetComponent("Button").interactable = bEnable
  87. end
  88. return UIHelper