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 local function OnClickButtonQuit(bBackToAndroid, sOpenPath, sClosePath) if bBackToAndroid then CS.UnityEngine.Application.Quit() return end local openUI = require(sOpenPath) local closeUI = require(sClosePath) openUI.SetRootPanelActive(true) closeUI.SetRootPanelActive(false) -- UIHelper:SwitchUI(sOpenPath, sClosePath) end function UIHelper:RegisterButtonQuit(rootPanel, bBackToAndroid, sOpenPath, sClosePath) print(string.format("UIHelper.RegisterButtonQuit rootPanel.name:%s bBackToAndroid:%s, sOpenPath:%s, sClosePath:%s", rootPanel.name, bBackToAndroid, sOpenPath, sClosePath)) UIHelper:AddClickEvent(rootPanel, "ButtonQuit", function () OnClickButtonQuit(bBackToAndroid, sOpenPath, sClosePath) end) end return UIHelper