x


Displaying a progress bar on the OnInspectorGUI function

Hi people, I'm trying to display a progress bar when I calculate some things in the Inspector but it appears this message:

ArgumentException: You can only call GUI functions from inside OnGUI.
UnityEngine.GUIUtility.CheckOnGUI () 
UnityEngine.GUILayoutUtility.DoGetRect (UnityEngine.GUIContent content, UnityEngine.GUIStyle style, UnityEngine.GUILayoutOption[] options) [0x00000]
UnityEngine.GUILayoutUtility.GetRect (UnityEngine.GUIContent content, UnityEngine.GUIStyle style, UnityEngine.GUILayoutOption[] options) [0x00000]
UnityEditor.InspectorWindow.OnGUI () 
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000]

The main code of the example is the follow, this is the editor of another class ViewArea:

using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(ViewArea))]
public class ViewAreaEditor : Editor{

    public override void OnInspectorGUI()
    {
        ViewArea va;
        va = target as ViewArea;

        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.PrefixLabel("Cell Division");
        va.cellDivision = EditorGUILayout.Slider(va.cellDivision, 0.01f, 1f);
        EditorGUILayout.EndHorizontal();

        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.PrefixLabel("Ray Division");
        va.rayDivision = EditorGUILayout.Slider(va.rayDivision, 0.01f, 1f);
        EditorGUILayout.EndHorizontal();

        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("Number Of Rays", va.numberOfRays.ToString());
        EditorGUILayout.EndHorizontal();

        if (GUILayout.Button("Calculate PVS"))
        {
            EditorUtility.DisplayProgressBar("Calculating the PVS", "Calculating - 0%", 0.0f);
            va.ThrowRays();
            EditorUtility.DisplayProgressBar("Calculating the PVS", "Calculating - 50%", 0.5f);
            va.CalculateTransformsPerCell();
            EditorUtility.DisplayProgressBar("Calculating the PVS", "Calculating - 100%", 1.0f);
            EditorUtility.ClearProgressBar();
        }
        if (GUI.changed)
        {
            EditorUtility.SetDirty(va);
        }

    }
}
more ▼

asked Nov 25 '09 at 09:42 AM

limdor gravatar image

limdor
55 3 3 8

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

I just saw this in the docs.

If you want to put 2D GUI objects (GUI, EditorGUI and friends), you need to wrap them in calls to Handles.BeginGUI() and Handles.EndGUI().

I'm guessing the problem is with the following line, since you're calling GUILayout instead of EditorGUILayout but don't have it surrounded by Handles.BeginGUI()/EndGUI().

if (GUILayout.Button("Calculate PVS"))
more ▼

answered Nov 25 '09 at 07:32 PM

Ehren gravatar image

Ehren
4.2k 34 43 77

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3681
x1671

asked: Nov 25 '09 at 09:42 AM

Seen: 3103 times

Last Updated: Nov 25 '09 at 09:42 AM