Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Dynamic Gui reading database

Discussion in 'Scripting' started by adre, Apr 23, 2014.

  1. adre

    adre

    Joined:
    Mar 28, 2014
    Posts:
    38
    Hello guys,
    i am working on an interior design app. I need to create a gui menu which reads categories and subcategories from a DB and populate the DB with the meshes loaded on the DB.

    I can't figure out how to create a dynamic gui which expands reading the database.

    Eg:

    main categories: Lights | Tables | Libraries | Accessories

    Subcat of Lights: Table lights | Wall Lights | Portable Lights

    Objects in Table Lights: Mesh1 | Mesh2 | Mesh3


    The Database creation is not a problem, i don't know where to start in unity to populate the dynamic gui menu.
    Anyone has some tips for a starting point, or dealt with a similar problem?


    Thanks
     
  2. Patico

    Patico

    Joined:
    May 21, 2013
    Posts:
    886
    My advice is to separate UI and database. Add a class structure wich will reflect your GUI items and pupulate it from database. Then create a class for drawing GUI elements, and draw them depends on data and state of corresponded class.
     
  3. Patico

    Patico

    Joined:
    May 21, 2013
    Posts:
    886
    Here is example of such architecture:

    Code (csharp):
    1.  
    2. ///Example menu
    3. class Menu
    4. {
    5.     public string Header        {get;set;}
    6.     public string StartButtons  {get;set;} /// it also could be an array of buttons ;-)
    7.     public string CloseButton   {get;set;}
    8.  
    9.     public bool IsActive        {get;set;} /// state of GUI element (shown or not)
    10. }
    11.  
    12. class MyGUI : MonoBehaviour
    13. {
    14.     Menu menu = new Menu;
    15.  
    16.     void Start()
    17.     {
    18.         /// Populate GUI elements here:
    19.         menu = MySuperDatabase_GUIRepository.GetMenu("Menu version 1"); /// Class that responsible for downloading menu content from DB
    20.         menu.IsActive; /// show menu
    21.     }
    22.  
    23.     void OnGUI()
    24.     {
    25.         if(menu.IsActive)
    26.         {
    27.             GUILayout.Label(menu.Header);
    28.             if(GUILayout.Button(menu.StartButton))
    29.                 ;/// TODO
    30.             if(GUILayout.Button(menu.CloseButton))
    31.                 menu.IsActive = false;
    32.         }
    33.     }
    34. }
    35.  
     
    Last edited: Apr 23, 2014
  4. adre

    adre

    Joined:
    Mar 28, 2014
    Posts:
    38
    Thanks! Should be a viable solution, gonna work on it asap.
    And thanks for the example!