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

Question about GUI groups

Discussion in 'Immediate Mode GUI (IMGUI)' started by geetoo, May 10, 2011.

  1. geetoo

    geetoo

    Joined:
    Apr 21, 2009
    Posts:
    42
    Hi.
    Is there a way to know the "world position" of a GUI item on the screen, even if it's inside a GUI.Group ?

    Basically, I have set up a GUI.Group, let's say with a Rect(10,10,100,100).
    And in this group, I create a GUI.Button, for example with a Rect(10,10, 50,50).
    I would like to know the top-left position of the button in the world. In that case, it would be 20,20.

    Thanks.
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    There isn't a function in the API to do this but it is fairly straightforward (just by adding the corner values of the group rectangle to those of the control, which is basically what you have done in your example).
     
  3. geetoo

    geetoo

    Joined:
    Apr 21, 2009
    Posts:
    42
    Thanks for your reply andeeee. I already put that in place, but in the precise case I have, it wasn't enough.
    I should have been more precise the first time, so I'll try to describe exactly the problem I'm facing.

    I'm creating an "AdvancedGUI" static class, so that I can use AdvancedGUI function in my script. For instance, I implemented a DialogBox function to display information to the user. Now, I'm working on a ColorPicker function, to allow the user to select a color from a spectrum texture using a GetPixel.

    So I have a OnGUI() function in some script that creates a GUI.Group (let's call it "Group1").
    Within "Group1", I call a AdvancedGUI.ColorPicker(...) that will create a new group of GUI controls (also using some GUI.Group, that we can call "Group2").
    So I have some kind of GUI hierarchy like : Group1 > Group2 > mySpectrumButton

    The bottom line of my first question is : how can I get the exact pixel of the spectrum texture, without (if possible) passing the top-left position of my Group1 as an argument of the ColorPicker(...) constructor.

    With some values that gives something like :
    Group1 (10, 10, 100, 100)
    Group2 (25, 25, 50, 50)
    Button (20, 20, 20, 20)
    In that case, that would give something like 55,55. But as I do my picking inside Group2, it calculates 55,55 from the top-left position of Group2.

    I hope this is clear enough. If not, I'll try to do a picture to help. Thanks.