Search Unity

External Modular UI Creation

Discussion in 'Immediate Mode GUI (IMGUI)' started by xkrfs, Jun 13, 2016.

  1. xkrfs

    xkrfs

    Joined:
    Apr 5, 2016
    Posts:
    4
    Hey Forum,

    I searched in the forum but didn't found a proper answer to my problem and I am pretty new to Unity and C#.

    I want to create a dynamic app-based UI in Unity. That means, a external script should be able to change the UIs behaviour, like e.g. adding a button in runtime or varying the location of a group of UI Elements.

    In a first "test" I just want to have one script, with the Update() as my "runtime" and the OnGUI, where I receive the GUI-Objects from the Runtime.

    To start simply, I tried to push the Objects directly into a Queue and handle them in the OnGUI(), but I got several Error MSG for trying to Call GUI Functions from outside the OnGUI().

    How can I fix this? Or is there a much better solution?

    Code (CSharp):
    1. public class IMGUI_Test : MonoBehaviour {
    2.  
    3.     int i;
    4.     Queue myqueue = new Queue();
    5.  
    6.     // Use this for initialization
    7.     void Start () {
    8.         i = 5;
    9.     }
    10.  
    11.     void OnGUI () {
    12.         if (i > 500)
    13.             i = 5;
    14.  
    15.         object actual = myqueue.Dequeue();
    16.  
    17.         i++;
    18.     }
    19.    
    20.     // Update is called once per frame
    21.     void Update () {
    22.  
    23.         myqueue.Enqueue(GUI.Button(new Rect(i, 10, 200, 20), "Click me!"));
    24.    
    25.     }
    26. }
     
  2. IzzySoft

    IzzySoft

    Joined:
    Feb 11, 2013
    Posts:
    376
    I haven't done this myself, but I've seen scripts that call myGUI() functions from the Unity event OnGUI().

    ex:
    Code (csharp):
    1.  
    2. void OnGUI()
    3. {
    4.    classA1.myGUI();
    5.    classA2.myGUI();
    6.    ....
    7. }
    8.  
    maybe have a List of class object refs and loop through them, call myGUI() on them? :]
     
  3. xkrfs

    xkrfs

    Joined:
    Apr 5, 2016
    Posts:
    4
    Do you mean myGUI() as the myGUI library from OGRE? If so, I think it's a little overpowered for my case :/
     
  4. sindrijo

    sindrijo

    Joined:
    Mar 11, 2014
    Posts:
    13
    The GUI.Button doesn't actually return a button object it returns a boolean that denotes if the button has been clicked or not.

    The only way you can do what you describe is to write some wrapper classes that can draw "themselves", you then enqueue these objects in the queue in update/whatever and then you call the drawing code in OnGUI.

    Code (CSharp):
    1. public class IMGUI_Test : MonoBehaviour {
    2.         Queue<ButtonWrapper> myqueue = new Queue<ButtonWrapper>();
    3.         void OnGUI () {
    4.  
    5.             while (myqueue.Count > 0)
    6.             {
    7.                 myqueue.Dequeue().Draw();
    8.             }
    9.         }
    10.  
    11.         // Update is called once per frame
    12.         void Update () {
    13.             myqueue.Enqueue(new ButtonWrapper("Click me!"));
    14.         }
    15.  
    16.         private class ButtonWrapper
    17.         {
    18.             private string buttonText;
    19.  
    20.             public ButtonWrapper(string buttonText)
    21.             {
    22.                 this.buttonText = buttonText;
    23.             }
    24.  
    25.             public void Draw()
    26.             {
    27.                 GUILayout.Button(buttonText);
    28.             }
    29.         }
    30.     }
    Obviously just some very simple example code, handling layout is going to be a pain though... I recommend that you check out the new UI-System (UGUI) as that is actually object based probably easier for you to work with. ->

    http://unity3d.com/learn/tutorials/topics/user-interface-ui
     
  5. xkrfs

    xkrfs

    Joined:
    Apr 5, 2016
    Posts:
    4
    hey sindrijo,

    Thanks for your reply, I worked my problem out with the wrapper classes and am in the testing now! Thanks for the tip :)