Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Open a popup window from sprite

Discussion in 'UGUI & TextMesh Pro' started by KaamosV, Jun 5, 2015.

  1. KaamosV

    KaamosV

    Joined:
    Jun 5, 2015
    Posts:
    2
    Hi everyone!

    Im trying to do something, like the title says.

    I have this grid (img01), and a PopUp, with two buttons, when I clicked some of the hexagon sprite. The PopUp is made with the old UI, using OnGui and drawing it.

    My question is: How can make it with the new UI?
    and the other question, How can make it only enable when I click on the hexagon sprite, o close it when i click outside the popup??

    Thanks for the help, and sorry for my bad english!! ^^ img01.png
     
  2. TechCor

    TechCor

    Joined:
    Apr 3, 2015
    Posts:
    56
    There are many ways you could do this. Do you want to instantiate a prefab or have the window pre-existing on the scene?

    Are your hexagon sprites done in the new UI?

    Let's assume you already have the click functionality you want for the hexagon. On that click, you could instantiate a prefab with the new UI or set an existing popup on the scene as active.

    I usually go with leaving it on the scene unless I need to have more than one copy or use it on more than one scene. So the script that executes the on click for the hexagon, use
    Code (csharp):
    1. [SerializeField] private Canvas m_MyPopup;
    2. // OR
    3. public Canvas m_MyPopup;
    and assign the popup object on the scene to the hexagon script in the inspector.

    That popup object will contain an image object that represents the background, and two button objects for each button. For clicking off, you could use a transparent button that fills the screen.

    * You can find the new UI objects under the top menu: GameObject > UI.

    * On Buttons, you can implement the on click functionality by clicking the "+" below the "On Click ()" box in the inspector. Then drag and drop the object you want to have receive the call, and assign the click function in the drop down.

    In the case of your transparent button, the click function should call GameObject.Destroy() or gameObject.SetActive(false) depending on whether you instantiated it or it already exists on the scene. In my example it would be:

    Code (csharp):
    1. public void OnClick_BackgroundButton()
    2. {
    3.     m_MyPopup.gameObject.SetActive(false);
    4.     // Alternatively, you could use:
    5.     m_MyPopup.enabled = false;
    6.     // but this will cause keyboard navigation issues when the popup is supposed to be hidden, be warned.
    7. }
    I'm not using any screenshots, so this may be difficult to understand, but hopefully this helps.
     
  3. KaamosV

    KaamosV

    Joined:
    Jun 5, 2015
    Posts:
    2
    Ok! I will try to implement.

    About your first question.
    1. What is better, instantiate the prefab or hace the pre-existing? I would like instantiate.
    2. The hexagon sprite was made before the new UI.
    3. I have only the "OnMouseDown" click function on Explorer Window Script, that i works when i clicked the hexagon sprite. (debug.log)

    The idea of the PopUp window, is that it appears in all different hexagon.