Skip to content

Commit ffa571c

Browse files
authored
feat: Progress Bar Additions (#87)
* Update EditorWebRequest.cs * chore: docs * chore: minor changes
1 parent de72807 commit ffa571c

File tree

1 file changed

+42
-3
lines changed

1 file changed

+42
-3
lines changed

Editor/EditorUtilities/EditorWebRequest.cs

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using UnityEditor;
3+
using UnityEngine;
34
using UnityEngine.Networking;
45

56
namespace StansAssets.Foundation.Editor
@@ -9,9 +10,33 @@ namespace StansAssets.Foundation.Editor
910
/// </summary>
1011
public class EditorWebRequest
1112
{
13+
/// <summary>
14+
/// UnityWebRequest handler type.
15+
/// </summary>
16+
public enum HandlerType
17+
{
18+
///<summary>Used to download data.</summary>
19+
Download,
20+
///<summary>Used to upload data.</summary>
21+
Upload,
22+
}
23+
24+
private string HandlerProgress {
25+
get {
26+
switch (m_HandlerType)
27+
{
28+
default:
29+
return $"Downloaded: {Convert.ToInt32(UnityRequest.downloadedBytes)} bytes";
30+
case HandlerType.Upload:
31+
return $"Uploaded: {Convert.ToInt32(UnityRequest.uploadedBytes)} bytes";
32+
}
33+
}
34+
}
35+
1236
Action<UnityWebRequest> m_OnComplete = null;
1337

1438
bool m_ShowProgress = false;
39+
HandlerType m_HandlerType;
1540
string m_ProgressDialogTitle = string.Empty;
1641

1742
/// <summary>
@@ -40,9 +65,11 @@ public EditorWebRequest(UnityWebRequest request)
4065
/// Request will display editor progress dialog with the given title.
4166
/// </summary>
4267
/// <param name="title">Editor progress dialog title.</param>
43-
public void AddEditorProgressDialog(string title)
68+
/// <param name="handlerType">UnityWebRequest handler type.</param>
69+
public void AddEditorProgressDialog(string title, HandlerType handlerType = HandlerType.Download)
4470
{
4571
m_ShowProgress = true;
72+
m_HandlerType = handlerType;
4673
m_ProgressDialogTitle = title;
4774
}
4875

@@ -67,12 +94,24 @@ public void Send(Action<UnityWebRequest> callback)
6794
/// </summary>
6895
public string DataAsText => UnityRequest.downloadHandler.text;
6996

97+
private float m_LastUpdate;
98+
7099
void OnEditorUpdate()
71100
{
72101
if (m_ShowProgress)
73102
{
74-
var progress = $"Download Progress: {Convert.ToInt32(UnityRequest.downloadProgress * 100f)}%";
75-
EditorUtility.DisplayProgressBar(m_ProgressDialogTitle, progress, UnityRequest.downloadProgress);
103+
if (!Application.isBatchMode)
104+
{
105+
if (Time.realtimeSinceStartup > m_LastUpdate + 1)
106+
{
107+
Debug.Log(HandlerProgress);
108+
m_LastUpdate = Time.realtimeSinceStartup;
109+
}
110+
}
111+
else
112+
{
113+
EditorUtility.DisplayProgressBar(m_ProgressDialogTitle, HandlerProgress, UnityRequest.downloadProgress);
114+
}
76115
}
77116

78117
if (UnityRequest.isDone)

0 commit comments

Comments
 (0)