Skip to content

Instantly share code, notes, and snippets.

@KzoNag
Created December 7, 2016 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KzoNag/4b43ca03d36bb43ff26c9a4eb345287d to your computer and use it in GitHub Desktop.
Save KzoNag/4b43ca03d36bb43ff26c9a4eb345287d to your computer and use it in GitHub Desktop.
Editor extention script to download and import "HoloToolkit-Unity"
/*
Copyright (c) 2016 Keizo Nagamine
Released under the MIT license
http://opensource.org/licenses/mit-license.php
*/
/*
* @Usage
* 1. Import this script into "Editor" directory.
* 2. Select "Assets/Import HoloToolkit" menu item.
*
* @Require
* Windows10 (or PowerShell v5 installed PC)
*/
using System.IO;
using UnityEngine;
using UnityEditor;
/// <summary>
/// Download and import "HoloToolkit-Unity"
/// </summary>
public static class HoloToolkitImporter
{
private static WWW www;
private static System.Diagnostics.Process process;
private const string dowoloadUrl = "https://github.com/Microsoft/HoloToolkit-Unity/archive/master.zip";
private static string tempPath { get { return CombinePath(Application.dataPath, "..", "Temp", "HoloToolkitImporter"); } }
private static string zipFilePath { get { return CombinePath(tempPath, "HoloToolkit-Unity.zip"); } }
private static string zipExpandPath { get { return CombinePath(tempPath, "HoloToolkit-Unity"); } }
private static string projectPath { get { return CombinePath(zipExpandPath, "HoloToolkit-Unity-master"); } }
private static string exportPackagePath { get { return CombinePath(tempPath, "HoloToolkit-Unity.unitypackage"); } }
[MenuItem("Assets/Import HoloToolkit")]
public static void Execute()
{
Directory.CreateDirectory(tempPath);
Download();
}
#region Download
// Download "Holotoolkit-Unity" zip file from github.
private static void Download()
{
www = new WWW(dowoloadUrl);
EditorApplication.update += DownloadUpdate;
}
private static void DownloadUpdate()
{
EditorUtility.DisplayProgressBar("Hold on", "Downloading HoloToolkit", www.progress);
if (!string.IsNullOrEmpty(www.error))
{
Debug.LogError(www.error);
FinishDownload();
}
else if(www.isDone)
{
// Save as zip file.
File.WriteAllBytes(zipFilePath, www.bytes);
FinishDownload();
Unzip();
}
}
private static void FinishDownload()
{
www.Dispose();
www = null;
EditorUtility.ClearProgressBar();
EditorApplication.update -= DownloadUpdate;
}
#endregion
#region Unzip
// Unzip by using windows powershell
private static void Unzip()
{
if(!File.Exists(zipFilePath))
{
Debug.LogErrorFormat("{0} is not exist.", zipFilePath);
return;
}
process = new System.Diagnostics.Process();
// Get path to command prompt
process.StartInfo.FileName = System.Environment.GetEnvironmentVariable("ComSpec");
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
// Set unzip command as arguments
process.StartInfo.Arguments = string.Format("/c powershell expand-archive \"{0}\" \"{1}\" -force", zipFilePath, zipExpandPath);
process.Start();
EditorApplication.update += UnzipUpdate;
EditorUtility.DisplayProgressBar("Hold on", "Unzip HoloToolkit", 1);
}
private static void UnzipUpdate()
{
if(process.HasExited)
{
process.Close();
process = null;
EditorUtility.ClearProgressBar();
EditorApplication.update -= UnzipUpdate;
Export();
}
}
#endregion
#region Export
// Export UnityPackage from unzipped directory
private static void Export()
{
if (!Directory.Exists(projectPath))
{
Debug.LogErrorFormat("{0} is not exist.", projectPath);
return;
}
process = new System.Diagnostics.Process();
process.StartInfo.FileName = EditorApplication.applicationPath;
process.StartInfo.Arguments = string.Format("-batchmode -quit -projectPath \"{1}\" -exportPackage Assets \"{2}\"", EditorApplication.applicationPath, projectPath, exportPackagePath);
process.Start();
EditorApplication.update += ExportUpdate;
EditorUtility.DisplayProgressBar("Hold on", "Export HoloToolkit", 1);
}
private static void ExportUpdate()
{
if (process.HasExited)
{
process.Close();
process = null;
EditorUtility.ClearProgressBar();
EditorApplication.update -= ExportUpdate;
Import();
}
}
#endregion
#region Import
// Import UnityPackage
private static void Import()
{
if (!File.Exists(exportPackagePath))
{
Debug.LogErrorFormat("{0} is not exist.", exportPackagePath);
return;
}
AssetDatabase.ImportPackage(exportPackagePath, false);
}
#endregion
#region Utility
private static string CombinePath(params string[] paths)
{
if (paths == null) { return null; }
string combinedPath = "";
foreach (var path in paths)
{
if (path != null)
{
combinedPath = Path.Combine(combinedPath, path);
}
}
return combinedPath;
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment