Skip to content

UI Window

In this example we create a custom UI and open it from a pause menu button.

function OnTemplate()
    -- Creates a new UI Canvas to hold our UI.
    -- The true value means the game will free the mouse when this canvas is active.
    myCanvas = UI.CreateCanvas(true)
    -- Creates a main panel in our new canvas.
    mainPanel = UI.CreatePanel(myCanvas).SetAnchors(0.3, 0.7, 0.1, 0.9)
    -- Adds an image to the main panel.
    UI.CreateImage(mainPanel,1,1,1,0.8)
    -- Creates a new title panel in the main panel.
    titlePanel = UI.CreatePanel(mainPanel).SetAnchors(0.1,0.9,0.80,0.95)
    -- Adds a text to the title panel.
    titleText = UI.CreateTextMesh(titlePanel, "Title",76,0,0.7,1,1,"center","titletext").SetFont("TitleShadow")
    -- Creates a panel to hold our buttons
    buttonsPanel = UI.CreatePanel(mainPanel).SetAnchors(0.1,0.9,0.05,0.78)
    -- Adds a grid layout to our buttons panel that will automatically place
    -- any child of that panel in a grid form with 100 width and 26 height.
    buttonsGrid = UI.CreateGridLayout(buttonsPanel,100,26,"bgrid")
    -- Creates a new button in the buttons panel that calls TestFunction when clicked.
    newButton = UI.AddBlueButton(buttonsPanel,"Test Button", "buttons", TestFunction)

    -- Adds a close button to the main panel
    closeButton = UI.AddCloseButton(mainPanel, "closeButton", CloseUI)
    -- Sets the anchors of the close button's rect transform to the right corner.
    closeButton.rectTransform.SetAnchors(0.9,1,0.9,1)

    -- Hides our canvas.
    myCanvas.gameObject.SetActive(false)

    -- Register a listener to add a button to open ui in the
    -- main pause menu when the main pause menu is created. 
    self.RegisterListener(Messager.StartGameUI,MakeUIOpenButton)
end

function MakeUIOpenButton()
    -- Adds a button to open our UI
    UI.AddMenuButton("Open UI",OpenUI)
end

function OpenUI()
    -- Unpauses the game so we don't have the pause menu on top of our new UI
    Game.UnpauseGame()
    -- Activates our canvas
    myCanvas.gameObject.SetActive(true)
end

function CloseUI()
    -- Deactivates our canvas
    myCanvas.gameObject.SetActive(false)
    -- Pauses the game so we can go back to the pause menu
    Game.PauseGame()
end

function TestFunction()
    -- Prints test to the console.
    Console.Log("Test")
end

Comments