Search Unity

Window Editor: Getting the correct layout through code

Discussion in 'Immediate Mode GUI (IMGUI)' started by FeastSC2, Apr 9, 2017.

  1. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    I'm having an issue where I can't place my button in a grid layout. It's my first editor, so it might be something basic that I don't understand.
    I've been trying to make it work with GUILayout.BeginHorizontal(); but it just won't work I don't know what I'm doing wrong.

    Here's my custom window editor right now, I'd like my buttons to be ordered in a grid layout.
    http://i.imgur.com/pLRMWbU.png

    Here's the code where I'm drawing my buttons, I call this in the OnGUI() method.

    private bool DrawButtonObject(Object obj, string pathStartingFromResourcesFolder, int number)
    {
    //GUILayout.BeginHorizontal();

    bool retVal = false;

    var tex = Resources.Load(pathStartingFromResourcesFolder) as Texture2D;

    if (tex != null)
    retVal = GUILayout.Button(tex, GUILayout.Width(64), GUILayout.Height(64));
    else
    {
    retVal = GUILayout.Button("Select", GUILayout.Width(64f), GUILayout.Height(64f));
    }
    GUILayout.Label(obj.name, GUILayout.Width(160f));

    GUI.color = Color.white;

    //GUILayout.EndHorizontal();
    return retVal;
    }

    So how do I go about creating that grid layout with the buttons?

    Thanks in advance for any help ;)
     
  2. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    Ok the solution is using GUILayout.BeginArea(), it will create 1 column of a certain amount of space defined by a rect, so if you want more than 1 column, you have to put it in a for loop with an x offset for each rect.
    I've still got to make the scrolling work with that method because it's not working anymore.