Unity编辑器之ProgressBar 进度条
在Editor 文件夹下创建脚本TestEditor
using UnityEngine;
using UnityEditor;
public enum ShowProgressType
{
Hide,
Progress,
CancelableProgress,
}
public class TestEditor : EditorWindow
{
float maxValue = 10.0f;
double startVal = 0;
private ShowProgressType showType = ShowProgressType.Hide;
[MenuItem("Examples/Cancelable Progress Bar Usage")]
static void Init()
{
var window = GetWindow(typeof(TestEditor));
window.Show();
}
void OnGUI()
{
maxValue = EditorGUILayout.FloatField("Time to wait:", maxValue);
if (GUILayout.Button("Display Progress bar"))
{
showType = ShowProgressType.Progress;
startVal = EditorApplication.timeSinceStartup; //开始编译到现在的时间
}
GUILayout.Space(5);
if (GUILayout.Button("Clear Progress bar"))
{
showType = ShowProgressType.Hide;
EditorUtility.ClearProgressBar();
}
GUILayout.Space(30);
if (GUILayout.Button("Display Cancelable Progress bar"))
{
showType = ShowProgressType.CancelableProgress;
startVal = EditorApplication.timeSinceStartup; //开始编译到现在的时间
}
float slider = (float)(EditorApplication.timeSinceStartup - startVal) / maxValue;
if (showType == ShowProgressType.Progress)
{
//参数1 为标题,参数2为提示,参数3为 进度百分比 0~1 之间
EditorUtility.DisplayProgressBar("Simple Progress Bar", "Show a progress bar for the given seconds", slider);
}
else if (showType == ShowProgressType.CancelableProgress)
{
//参数1 为标题,参数2为提示,参数3为 进度百分比 0~1 之间
//进度条下方显示一个关闭按钮,点击关闭按钮隐藏进度条
if (EditorUtility.DisplayCancelableProgressBar("Simple Progress Bar", "Show a progress bar for the given seconds", slider))
{
showType = ShowProgressType.Hide;
EditorUtility.ClearProgressBar();
}
}
}
void OnInspectorUpdate() //更新
{
Repaint(); //重新绘制
}
}