Skip to content

Instantly share code, notes, and snippets.

@sokuhatiku
Created April 3, 2019 15:46
Show Gist options
  • Save sokuhatiku/a171130098784d596d0ee333bd19e37d to your computer and use it in GitHub Desktop.
Save sokuhatiku/a171130098784d596d0ee333bd19e37d to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using UnityEditor;
using UnityEngine;
using UnityEngine.Networking;
public class PackageDownloadTest : EditorWindow
{
[MenuItem("Window/PackageDownloadTest")]
static void Open()
{
GetWindow<PackageDownloadTest>();
}
string downloadURL;
private void OnGUI()
{
downloadURL = EditorGUILayout.TextField(downloadURL);
if (GUILayout.Button("Download"))
{
var request = UnityWebRequest.Get(downloadURL);
request.SendWebRequest();
while (!request.isDone && !request.isNetworkError && !request.isHttpError)
{
if (EditorUtility.DisplayCancelableProgressBar("downloading", "downloading package...", request.downloadProgress))
{
request.Abort();
break;
}
Thread.Sleep(1);
}
if (!string.IsNullOrEmpty(request.error))
{
throw new System.Exception(request.error);
}
var tempFilePath = Path.GetTempFileName();
File.WriteAllBytes(tempFilePath, request.downloadHandler.data);
AssetDatabase.ImportPackage(tempFilePath, false);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment