local UIHelper = {} function UIHelper:OpenUI(scriptPath) print("UIHelper:OpenUI -> " .. scriptPath) local luaRoot = CS.UnityEngine.GameObject.Find("LuaRoot") assert(luaRoot ~= nil) local uiScriptHolder = luaRoot.transform:Find("uiScriptHolder") if not uiScriptHolder then uiScriptHolder = CS.UnityEngine.GameObject("uiScriptHolder") uiScriptHolder.transform:SetParent(luaRoot.transform) end local uiController = require(scriptPath) local Utils = require("Base/Utils.lua") local scriptName = Utils:GetScriptNameByScriptPath(scriptPath) local scriptGameObject = CS.UnityEngine.GameObject(scriptName) scriptGameObject.transform:SetParent(uiScriptHolder.transform) CS.SFramework.SLuaComponent.Add(scriptGameObject, uiController, scriptPath) return uiController end function UIHelper:CloseUI(scriptPath) print("UIHelper:CloseUI -> " .. scriptPath) local Utils = require("Base/Utils.lua") local scriptName = Utils:GetScriptNameByScriptPath(scriptPath) local go = CS.UnityEngine.GameObject.Find("LuaRoot/uiScriptHolder/" .. scriptName) CS.UnityEngine.GameObject.Destroy(go) end function UIHelper:SwitchUI(dstUIControllerPath, srcUIControllerPath) UIHelper:OpenUI(dstUIControllerPath) UIHelper:CloseUI(srcUIControllerPath) end function UIHelper:AddClickEvent(rootPanel, buttonPath, clickFunc) print(string.format("UIHelper:AddClickEvent rootPanel.name:%s buttonPath:%s clickFunc:%s", rootPanel.name, buttonPath, clickFunc)) local buttonGameObject = rootPanel.transform:Find(buttonPath).gameObject buttonGameObject:GetComponent("Button").onClick:AddListener(clickFunc) end function UIHelper:AddSliderEvent(rootPanel, sliderPath, valueChangeFunc) print(string.format("UIHelper:AddSliderEvent rootPanel.name:%s buttonPath:%s clickFunc:%s", rootPanel.name, sliderPath, valueChangeFunc)) local sliderGameObject = rootPanel.transform:Find(sliderPath).gameObject sliderGameObject:GetComponent("Slider").onValueChanged:AddListener(valueChangeFunc) end function UIHelper:AddToggleEvent(rootPanel, togglePath, valueChangeFunc) print(string.format("UIHelper:AddToggleEvent rootPanel.name:%s buttonPath:%s toggleChangeFunc:%s", rootPanel.name, togglePath, valueChangeFunc)) local toggleGameObject = rootPanel.transform:Find(togglePath).gameObject toggleGameObject:GetComponent("Toggle").onValueChanged:AddListener(valueChangeFunc) end function UIHelper:SetTextInfo(rootPanel, textPath, textInfo) print(string.format("UIHelper:SetTextInfo rootPanel.name:%s buttonPath:%s textInfo:%s", rootPanel.name, textPath, textInfo)) local textGameObject = rootPanel.transform:Find(textPath).gameObject textGameObject:GetComponent("Text").text = textInfo end function UIHelper:LoadSprite(rootPanel, resourcePath, spriteName) print(string.format("UIHelper:LoadSprite rootPanel.name:%s resourcePath:%s spriteName:%s", rootPanel.name, resourcePath, spriteName)) local texture2D = LoadResource(resourcePath) local skillSprite = CS.UnityEngine.Sprite.Create(texture2D, CS.UnityEngine.Rect(0,0,texture2D.width, texture2D.height), CS.UnityEngine.Vector2.zero) skillSprite.name = spriteName rootPanel.transform:GetComponent("Image").sprite = skillSprite end function UIHelper:AddScript(rootPanel, szScriptPath) local controller = require(szScriptPath) CS.SFramework.SLuaComponent.Add(rootPanel, controller, szScriptPath) end function UIHelper:IsPointerOverGameObject() local eventSystem = CS.UnityEngine.EventSystems.EventSystem local eventData = CS.UnityEngine.EventSystems.PointerEventData(eventSystem.current) local mousePosition = CS.UnityEngine.Input.mousePosition local mousePos = CS.UnityEngine.Vector2(mousePosition.x, mousePosition.y) eventData.position = mousePos local listRaycast = CS.System.Collections.Generic.List(CS.UnityEngine.EventSystems.RaycastResult)() eventSystem.current:RaycastAll(eventData, listRaycast) return listRaycast.Count > 0 end function UIHelper:SetButtonInteractable(rootPanel, buttonPath, bEnable) local button = rootPanel.transform:Find(buttonPath).gameObject button:GetComponent("Button").interactable = bEnable end function UIHelper:SetUIComponentEnable(rootPanel, uiPath, szComponent, bEnable) local uiGameObject = rootPanel if uiPath then uiGameObject = rootPanel.transform:Find(uiPath).gameObject end local uiComponent = uiGameObject.transform:GetComponent(szComponent) uiComponent.enabled = bEnable end function UIHelper:SetTextMeshInfo(rootPanel, textPath, textInfo) local textMeshObj = rootPanel.transform:Find(textPath).gameObject textMeshObj:GetComponent("TextMesh").text = textInfo end function UIHelper:AddTextChangedEvent(rootPanel, inputFieldPath, valueChangeFunc) print(string.format("UIHelper:AddTextInputEvent rootPanel.name:%s inputFieldPath:%s", rootPanel.name, inputFieldPath)) local inputGameObject = rootPanel.transform:Find(inputFieldPath).gameObject inputGameObject:GetComponent("InputField").onValueChanged:AddListener(valueChangeFunc) end return UIHelper