Search Unity

Question How to allign buttons?

Discussion in 'Immediate Mode GUI (IMGUI)' started by AzpxMsw, Dec 14, 2022.

  1. AzpxMsw

    AzpxMsw

    Joined:
    Jun 1, 2020
    Posts:
    13
    I need to align buttons according to foldout. Here's a code:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5.  
    6. public class SampleWindow : EditorWindow
    7. {
    8.     bool Colliders;
    9.     bool Components;
    10.     bool Menu;
    11.  
    12.     Vector2 scrollView = Vector2.zero;
    13.  
    14.     [MenuItem("Window/Other/Open Sample Window #M")]
    15.     static void onOpen()
    16.     {
    17.         EditorWindow.GetWindow(typeof(SampleWindow));
    18.     }
    19.  
    20.     void OnGUI()
    21.     {
    22.         Menu = EditorGUILayout.Foldout(Menu, "Menu");
    23.         if (Menu)
    24.         {
    25.             EditorGUI.indentLevel++;
    26.             Components = EditorGUILayout.Foldout(Components, "Components");
    27.             if (Components)
    28.             {
    29.                 //Here I need to allign buttons according to foldout
    30.  
    31.                 EditorGUILayout.BeginHorizontal();
    32.                 if (GUILayout.Button("Add Capsule", GUILayout.MinWidth(115), GUILayout.MaxWidth(135)))
    33.                 {
    34.                     if (Selection.activeGameObject != null)
    35.                     {
    36.                         Selection.activeGameObject.AddComponent<CapsuleCollider>();
    37.                     }
    38.                 }
    39.                 if (GUILayout.Button("Add Box 2D", GUILayout.MinWidth(115), GUILayout.MaxWidth(135)))
    40.                 {
    41.                     if (Selection.activeGameObject != null)
    42.                     {
    43.                         Selection.activeGameObject.AddComponent<BoxCollider2D>();
    44.                     }
    45.                 }
    46.                 EditorGUILayout.EndHorizontal();
    47.             }
    48.  
    49.             Colliders = EditorGUILayout.Foldout(Colliders, "Colliders");
    50.             if (Colliders)
    51.             {
    52.                 //Here I need to allign buttons according to foldout
    53.  
    54.                 EditorGUILayout.BeginHorizontal();
    55.                 if (GUILayout.Button("Box", GUILayout.MinWidth(115), GUILayout.MaxWidth(135)))
    56.                 {
    57.                     GameObject build = new GameObject("Box");
    58.                     BoxCollider collider = build.AddComponent<BoxCollider>();
    59.                     collider.isTrigger = true;
    60.                     Selection.activeGameObject = build;
    61.                 }
    62.                 if (GUILayout.Button("Mesh", GUILayout.MinWidth(115), GUILayout.MaxWidth(135)))
    63.                 {
    64.                     GameObject build = new GameObject("Mesh");
    65.                     build.AddComponent<UnityEngine.MeshCollider>();
    66.                     Selection.activeGameObject = build;
    67.                 }
    68.                 EditorGUILayout.EndHorizontal();
    69.             }
    70.             EditorGUI.indentLevel--;
    71.         }
    72.     }
    73. }
     
  2. Belent

    Belent

    Joined:
    Mar 14, 2021
    Posts:
    1
    Code (CSharp):
    1. int indentSize = 15;
    2. int offset = 5;
    3. EditorGUILayout.BeginHorizontal();
    4. GUILayout.Space(EditorGUI.indentLevel * indentSize + offset);