EditorGUI.ProgressBar

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

using UnityEngine;
using UnityEditor;
using System.Collections;

[CustomEditor(typeof(DayNightCycle))]

public class DayNightCycleEditor : Editor
{
    public override void OnInspectorGUI()
    {
        base.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)
        {
            EditorUtility.SetDirty((DayNightCycle)target);
        }
    }
}

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.

Thanks.

Kamil

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!");
    GUILayout.Space(16);
    EditorGUILayout.EndVertical();
    

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");