Skip to content

Commit 6598243

Browse files
Dzoni94TwoTenPvP
authored andcommitted
refactor: Updated WWW to UnityWebRequest (MLAPIEditor.cs) (#188)
WWW class replaced with UnityWebRequest to get rid of annoying warnings inside the console.
1 parent f096307 commit 6598243

File tree

1 file changed

+101
-92
lines changed

1 file changed

+101
-92
lines changed

MLAPI-Editor/MLAPIEditor.cs

Lines changed: 101 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Text;
88
using UnityEditor;
99
using UnityEngine;
10+
using UnityEngine.Networking;
1011

1112
public class VersionUpgradePopup : EditorWindow
1213
{
@@ -471,38 +472,43 @@ private IEnumerator InstallRelease(int index)
471472
bool downloadFail = false;
472473
for (int i = 0; i < releases[index].assets.Length; i++)
473474
{
474-
WWW www = new WWW(releases[index].assets[i].browser_download_url);
475-
while (!www.isDone && string.IsNullOrEmpty(www.error))
475+
using (UnityWebRequest www = UnityWebRequest.Get(releases[index].assets[i].browser_download_url))
476476
{
477-
statusMessage = "Downloading " + releases[index].assets[i].name + "(" + (i + 1) + "/" + releases[index].assets.Length + ") " + www.progress + "%";
478-
yield return null;
479-
}
477+
www.SendWebRequest();
478+
while (!www.isDone && string.IsNullOrEmpty(www.error))
479+
{
480+
statusMessage = "Downloading " + releases[index].assets[i].name + "(" + (i + 1) + "/" + releases[index].assets.Length + ") " + www.downloadProgress + "%";
481+
yield return null;
482+
}
480483

481-
if (!string.IsNullOrEmpty(www.error))
482-
{
483-
//Some kind of error
484-
downloadFail = true;
485-
statusMessage = "Failed to download asset " + releases[index].assets[i].name + ". Error: " + www.error;
486-
double startTime = EditorApplication.timeSinceStartup;
487-
//Basically = yield return new WaitForSeconds(5);
488-
while (EditorApplication.timeSinceStartup - startTime <= 5f)
484+
if (!string.IsNullOrEmpty(www.error))
485+
{
486+
//Some kind of error
487+
downloadFail = true;
488+
statusMessage = "Failed to download asset " + releases[index].assets[i].name + ". Error: " + www.error;
489+
double startTime = EditorApplication.timeSinceStartup;
490+
//Basically = yield return new WaitForSeconds(5);
491+
while (EditorApplication.timeSinceStartup - startTime <= 5f)
492+
yield return null;
493+
statusMessage = "";
494+
}
495+
else
496+
{
497+
statusMessage = "Writing " + releases[index].assets[i].name + " to disk";
489498
yield return null;
490-
statusMessage = "";
491-
}
492-
else
493-
{
494-
statusMessage = "Writing " + releases[index].assets[i].name + " to disk";
495-
yield return null;
496499

497-
File.WriteAllBytes(Application.dataPath + "/MLAPI/Lib/" + releases[index].assets[i].name, www.bytes);
500+
File.WriteAllBytes(Application.dataPath + "/MLAPI/Lib/" + releases[index].assets[i].name, www.downloadHandler.data);
498501

499-
if (releases[index].assets[i].name.EndsWith(".unitypackage"))
500-
{
501-
PendingPackages.Add(releases[index].assets[i].name);
502+
if (releases[index].assets[i].name.EndsWith(".unitypackage"))
503+
{
504+
PendingPackages.Add(releases[index].assets[i].name);
505+
}
506+
507+
yield return null;
502508
}
503-
yield return null;
509+
510+
progress = i;
504511
}
505-
progress = i;
506512
}
507513

508514
yield return null;
@@ -520,85 +526,88 @@ private IEnumerator GetReleases()
520526
{
521527
lastUpdated = DateTime.Now.Ticks;
522528

523-
WWW www = new WWW(API_URL);
524-
isFetching = true;
525-
while (!www.isDone && string.IsNullOrEmpty(www.error))
526-
{
527-
statusMessage = "Fetching releases " + www.progress + "%";
528-
yield return null;
529-
}
530-
531-
if (!string.IsNullOrEmpty(www.error))
529+
using (UnityWebRequest www = UnityWebRequest.Get(API_URL))
532530
{
533-
//Some kind of error
534-
statusMessage = "Failed to fetch releases. Error: " + www.error;
535-
double startTime = EditorApplication.timeSinceStartup;
536-
//Basically = yield return new WaitForSeconds(5);
537-
while (EditorApplication.timeSinceStartup - startTime <= 5f)
531+
www.SendWebRequest();
532+
isFetching = true;
533+
while (!www.isDone && string.IsNullOrEmpty(www.error))
534+
{
535+
statusMessage = "Fetching releases " + www.downloadProgress + "%";
538536
yield return null;
539-
statusMessage = "";
540-
}
541-
else
542-
{
543-
isFetching = false;
544-
isParsing = true;
545-
string json = www.text;
537+
}
546538

547-
//This makes it from a json array to the individual objects in the array.
548-
//The JSON serializer cant take arrays. We have to split it up outselves.
549-
List<string> releasesJson = new List<string>();
550-
int depth = 0;
551-
StringBuilder builder = new StringBuilder();
552-
for (int i = 1; i < json.Length - 1; i++)
539+
if (!string.IsNullOrEmpty(www.error))
553540
{
554-
if (json[i] == '[')
555-
depth++;
556-
else if (json[i] == ']')
557-
depth--;
558-
else if (json[i] == '{')
559-
depth++;
560-
else if (json[i] == '}')
561-
depth--;
562-
563-
if ((depth == 0 && json[i] != ',') || depth > 0)
564-
builder.Append(json[i]);
565-
566-
if (depth == 0 && json[i] == ',')
567-
{
568-
releasesJson.Add(builder.ToString());
569-
builder.Length = 0;
570-
}
571-
572-
//Parse in smaller batches
573-
if (i % (json.Length / 100) == 0)
574-
{
575-
statusMessage = "Splitting JSON " + (i / (float)json.Length) * 100f + "%";
541+
//Some kind of error
542+
statusMessage = "Failed to fetch releases. Error: " + www.error;
543+
double startTime = EditorApplication.timeSinceStartup;
544+
//Basically = yield return new WaitForSeconds(5);
545+
while (EditorApplication.timeSinceStartup - startTime <= 5f)
576546
yield return null;
577-
}
578-
579547
statusMessage = "";
580548
}
549+
else
550+
{
551+
isFetching = false;
552+
isParsing = true;
553+
string json = www.downloadHandler.text;
554+
555+
//This makes it from a json array to the individual objects in the array.
556+
//The JSON serializer cant take arrays. We have to split it up outselves.
557+
List<string> releasesJson = new List<string>();
558+
int depth = 0;
559+
StringBuilder builder = new StringBuilder();
560+
for (int i = 1; i < json.Length - 1; i++)
561+
{
562+
if (json[i] == '[')
563+
depth++;
564+
else if (json[i] == ']')
565+
depth--;
566+
else if (json[i] == '{')
567+
depth++;
568+
else if (json[i] == '}')
569+
depth--;
570+
571+
if ((depth == 0 && json[i] != ',') || depth > 0)
572+
builder.Append(json[i]);
573+
574+
if (depth == 0 && json[i] == ',')
575+
{
576+
releasesJson.Add(builder.ToString());
577+
builder.Length = 0;
578+
}
579+
580+
//Parse in smaller batches
581+
if (i % (json.Length / 100) == 0)
582+
{
583+
statusMessage = "Splitting JSON " + (i / (float) json.Length) * 100f + "%";
584+
yield return null;
585+
}
581586

582-
releases = new GithubRelease[releasesJson.Count];
583-
foldoutStatus = new bool[releasesJson.Count];
587+
statusMessage = "";
588+
}
584589

585-
for (int i = 0; i < releasesJson.Count; i++)
586-
{
587-
releases[i] = JsonUtility.FromJson<GithubRelease>(releasesJson[i]);
588-
if (i == 0)
589-
foldoutStatus[i] = true;
590-
else
591-
foldoutStatus[i] = false;
590+
releases = new GithubRelease[releasesJson.Count];
591+
foldoutStatus = new bool[releasesJson.Count];
592592

593-
if (i % (releasesJson.Count / 30f) == 0)
593+
for (int i = 0; i < releasesJson.Count; i++)
594594
{
595-
yield return null;
596-
statusMessage = "Parsing JSON " + (i / (float)releasesJson.Count) * 100f + "%";
595+
releases[i] = JsonUtility.FromJson<GithubRelease>(releasesJson[i]);
596+
if (i == 0)
597+
foldoutStatus[i] = true;
598+
else
599+
foldoutStatus[i] = false;
600+
601+
if (i % (releasesJson.Count / 30f) == 0)
602+
{
603+
yield return null;
604+
statusMessage = "Parsing JSON " + (i / (float) releasesJson.Count) * 100f + "%";
605+
}
597606
}
598-
}
599607

600-
statusMessage = "";
601-
isParsing = false;
608+
statusMessage = "";
609+
isParsing = false;
610+
}
602611
}
603612
}
604613

@@ -674,4 +683,4 @@ public static Rect GetEditorMainWindowPos()
674683
}
675684
throw new NotSupportedException("Can't find internal main window. Maybe something has changed inside Unity");
676685
}
677-
}
686+
}

0 commit comments

Comments
 (0)