- Home /
The question is answered, right answer was accepted
EditorGUI.ProgressBar not Updating!!! = (
I have a custom progress bar window setup. It works and everything, but it only shows 0 and 100 percent nothing in between, no updating of the bar. Here's my Code.
using UnityEngine;
using UnityEditor;
using System.Collections;
public class AIS_BuilderProgress : EditorWindow
{
public AIS_GameController gController;
public AIS_DBEditorWindow dbEditor;
public float progress;
public string dialog = "";
public string log = "";
private Vector2 scrollPos;
public int successCount;
public int skipCount;
void Run ( )
{
AIS_ItemDatabase database = gController.itemDataBase;
AIS_BaseItem[] itemList = database.gameItems.ToArray ( );
int ItemsStartCount = itemList.Length;
for ( int i =0; i < itemList.Length; i++ )
{
Refresh ( );
dbEditor.BuildPrefab ( itemList [ i ], i, true );
progress = ( i+1 )/itemList.Length;
dialog = "Building : " + itemList [ i ].itemName;
log += "\n"+database.gameItems [ i ].itemName + " was built successfully...";
successCount++;
dialog = "Finished!";
log += "\n" + successCount + " successful, " + skipCount + " skipped, and " + ( itemList.Length - ( successCount+skipCount ) ) + " failed out of " + itemList.Length + " total.";
}
}
void OnGUI ()
{
GUI.Label(new Rect ( 5, 5, 420, 20 ),dialog);
if ( GUI.Button ( new Rect ( 360, 5, 60, 15 ), "Build" ) )
{
Run ( );
}
if ( GUI.Button ( new Rect ( 425, 5, 60, 15 ), "Close" ) )
{
dbEditor.buildingObjects = false;
this.Close ( );
}
EditorGUI.ProgressBar ( new Rect ( 5, 25, 490, 30 ), progress, "" + ( progress*100 )+"%" );
float dialogHeight = GUI.skin.GetStyle("Label").CalcSize(new GUIContent(log)).y;
GUI.Box ( new Rect ( 5, 60, 490, 80 ), "" );
scrollPos = GUI.BeginScrollView ( new Rect (5,60,490,80), scrollPos, new Rect ( 5, 60, 475, dialogHeight) );
GUI.Label ( new Rect ( 5, 60, 490, dialogHeight ), log );
GUI.EndScrollView ( );
}
public void Refresh()
{
this.Repaint ( );
}
}
Answer by slkjdfv · Apr 26, 2013 at 12:02 AM
Got it working. It was the for loop causing the issue. I replaced the for loop with a while loop and put a break at the end of the loop. I also had to put in a check to see if the bar should be running. It all works now thanks every one. Here's my new code for any one interested(I will also be posting this in the scripting section of the forums) :
/*
This class was made by Roger Moore - Digital Egg Studios
This is a free to use script.
This is for creating custom progress bars in the editor.
To use just simply create an instance of it in your code.
Example (C#):
using UnityEngine;
using UnityEditor;
using System.Collections;
class MyEditor : Editor
{
public bool building = false;
void OnGUI
{
if(EditorGUILayout.Button("Build"))
{
if ( buildingObjects )
return;
ProgressBar window = new ProgressBar();
window.position = new Rect ( Screen.currentResolution.width/2-250, Screen.currentResolution.height/2-73, 500, 146 );
window.minSize = new Vector2 ( 500, 146 );
window.maxSize = new Vector2 ( 500, 146 );
window.ShowPopup ( );
window.editorScript = this;
window.log = "Initialized builder...;
window.isBuilding = true;
window.ListLength = 5;
window.command = new ProgressBar.method(ExampleMethod);
}
}
void ExampleMethod(int index, ProgressBar builder)
{
//Do Stuff
}
}
*/
using UnityEngine;
using UnityEditor;
using System.Collections;
public class ProgressBar : EditorWindow
{
public /*Place EditorScript reference here*/ editorScript;
public float progress;
public string dialog = "";
public string log = "";
private Vector2 scrollPos;
public int successCount;
public int skipCount;
public int counter = 0;
public bool isBuilding = false;
public int listLength;
public method myMethod;
public delegate void method ( int index, AIS_BuilderProgress builder );
void Run ( )
{
while ( counter < listLength )
{
/*This is the method you want the progress script to run.
My method creates prefabs*/
myMethod (counter, this );
progress = ( float )( ( float )( counter+1 )/( float )itemList.Length );
counter++;
break;
}
if ( counter == listLength )
{
isBuilding = false;
dialog = "Finished!";
}
}
void OnGUI ()
{
GUI.Label(new Rect ( 5, 5, 420, 20 ),dialog);
if ( progress == 1 )
GUI.enabled = true;
else
GUI.enabled = false;
if ( GUI.Button ( new Rect ( 425, 5, 60, 15 ), "Close" ) )
{
/*This prevents the user from opening two windows at once. See above example
on how to use.*/
editorScript.building = false;
this.Close ( );
}
GUI.enabled = true;
if ( isBuilding )
{
Run ( );
}
EditorGUI.ProgressBar ( new Rect ( 5, 25, 490, 30 ), progress, "" + ( progress*100 )+"%" );
float dialogHeight = GUI.skin.GetStyle ( "Label" ).CalcSize ( new GUIContent ( log ) ).y;
GUI.Box ( new Rect ( 5, 60, 490, 80 ), "" );
scrollPos = GUI.BeginScrollView ( new Rect ( 5, 60, 490, 80 ), scrollPos, new Rect ( 5, 60, 475, dialogHeight ) );
GUI.Label ( new Rect ( 5, 60, 490, dialogHeight ), log );
GUI.EndScrollView ( );
}
}
Answer by ElectricFountain · Apr 26, 2013 at 12:27 AM
The problem is, the unity editor cant update loading progress. The best thing to do is place an animation like a rotating circular object or something in its place.
As stated above, I WAS able to update the loading progress in the editor. But thank you.