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 are updating our Terms of Service for all Unity subscription plans, effective October 13, 2022, to create a more streamlined, user-friendly set of terms. Please review them here: unity.com/legal/terms-of-service.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

[solved] GUI from outside OnGUI( )?

Discussion in 'Immediate Mode GUI (IMGUI)' started by Raziaar, Dec 21, 2009.

  1. Raziaar

    Raziaar

    Joined:
    Dec 20, 2009
    Posts:
    82
    Hey guys. I notice that I cannot create any GUI elements if I'm not doing it within OnGUI().

    I can understand why this is. Though I want to ask, is there any way around it?

    For example... I have an object that sends out an event. I have another object that is subscribing to that event and he does something in his Event Handling method. I want to make it so that when he handles the event, getting the information from it, he can then use that information and display it on the screen as a GUI item.

    Is that possible?
     
  2. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    No. The only place you can draw/create GUI elements is by triggering them from inside an OnGUI function.

    For your example, you can have variables that are tracked and when they have certain values you show various GUI elements, it's all quite easy. For example:

    Code (csharp):
    1. var ShowThisGUI = false;
    2.  
    3. function OnGUI () {
    4.  
    5.   if (ShowThisGUI) {
    6.  
    7.     // draw a bunch of GUI stuff here
    8.  
    9.   }
    10.  
    11. }
    The above is exceedingly simplistic, but you can imaging that ShowThisGUI is a public variable that is settable from other scripts (like the one doing your event handling) and you flip it to true when needed. From there I hope you can also see that the script with the OnGUI call can be fed information from externally, or find another Component (again like the one doing your event handling) and pull the data to display as you wish.


    So, all GUI elements must be drawn/triggered from within an OnGUI function, but you can use variables and state maintenance efforts to control that from within, or without. :)
     
    CPPCLINUX1024 and Nido like this.
  3. mbfloyd

    mbfloyd

    Joined:
    Mar 6, 2009
    Posts:
    12
    in a script attached to an gameObject int the scene ( i use an empty one) call a public function in object ( mine is called doGui) from the OnGUI function

    For example
    Code (csharp):
    1.  
    2.  
    3. var o_obj : SomeObject;
    4.  
    5. function Start () {
    6. o_obj = new SomeObject();
    7. }
    8.  
    9. function OnGUI () {
    10. o_obj.doGUI();
    11. }
    12.  
    13.  
    Then in you class object

    Code (csharp):
    1.  
    2.  
    3. class SomeObject {
    4.  
    5. function SomeObject() {
    6.  
    7. }
    8.  
    9. function doGUI() {
    10.  
    11. GUI.Lable(Rect(0,0,100,100), "I'm a GUI");
    12.  
    13. }
    14.  
    15. }
    16.  
    17.  
    Use boolean flags and functions to organize what gui elements you want to display and when.

    hope this helps
     
  4. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    Yes, that is in fact entirely valid and I'm sorry if I wasn't clear enough on that being a possibility. I tried (perhaps weakly :p) to cover that by saying that the call still has to be triggered from within an OnGUI function. Good catch and my bad on that mbfloyd!
     
  5. Raziaar

    Raziaar

    Joined:
    Dec 20, 2009
    Posts:
    82
    HiggyB and mbfloyd.

    Thank you for your responses! I had a feeling that I might have to go about dealing with this using variables to monitor a state as well which information to feed into the OnGUI function.

    The thing you proposed mbfloyd is a good way to manage a lot of external GUI information in one single GUIManager type class that contains a list of objects implementing an interface to their DoGUI() method or whatever you want it to be called. Would be very useful... though unfortunately not useful in my application as far as I can tell, since I'm dealing with an event that only flashes for a split moment in time until all the information contained within it disappears.

    Although... I do see a way I can incorporate both of your ideas into what I'm trying to do.

    Once again, thanks a bunch!


    EDIT: Ahem. I shall make no mention of your avatar and how it reminds me of a certain actor, as per your sig. LOL.
     
  6. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    <Off-Topic>

    Feel free to comment, mention and discuss as you like, just do so in the thread I have linked in my signature... :)

    (and do that as I'm interested in knowing who... :p )

    </Off-Topic>
     
unityunity