Hi guys. I’m trying to add a progress bar to the editor. Here’s my code:

using UnityEngine;
using UnityEditor;
using System.Collections;


public class DayNightCycleEditor : Editor
    public override void OnInspectorGUI()

        ((DayNightCycle)target).animationToggle = EditorGUILayout.Toggle("Animation Toggle", ((DayNightCycle)target).animationToggle);
        ((DayNightCycle)target).timeMultiplier = EditorGUILayout.Slider("Time Multiplier", ((DayNightCycle)target).timeMultiplier, 1.0f, 5.0f);
        ((DayNightCycle)target).totalHours = EditorGUILayout.IntField("Total Noumber of Hours", ((DayNightCycle)target).totalHours);
        EditorGUI.ProgressBar(new Rect(10, 825, 300, 25), ((DayNightCycle)target).dayProgress, "Day Progress");

        if (GUI.changed)

The first thing you pass to the progress bar is a rectangle and I’m trying to figure out how to snap the rectangle within the script menu. Now the second thing passed to the rectangle is the distance from the top of the screen and I sort of need the distance from the start of the script menu so if I compress all the stuff above the script menu the progress bar moves as well.



The solution is threefold:

  1. Use BeginVertical and EndVertical to define a vertical layout group in the overall layout for the progress bar.

  2. Put the progress bar in the layout group, passing it the rect returned from BeginVertical.

  3. Use GuiLayout.Space to reserve space within the layout group for the progress bar (since it isn’t a -Layout function).

    Rect r = EditorGUILayout.BeginVertical();
    EditorGUI.ProgressBar(r, 0.5f, "Halfway there!");

Actually you can use GUILayoutUtility.GetRect to specify an arbitrary layouted rectangle. Like all layouted controls you can also use any combination of GUILayoutOptions to modify the appearance. With GUILayout.Width and GUILayout.Height you can set a fix width / height.

DayNightCycle DNC = (DayNightCycle)target;
DNC.totalHours = EditorGUILayout.IntField("Total Noumber of Hours", DNC.totalHours);
Rect progressBarRect = GUILayoutUtility.GetRect(300,25);

EditorGUI.ProgressBar(progressBarRect, DNC.dayProgress, "Day Progress");