-
-
Save Amitkapadi/53a3282ba05de36e6d6f12b99238b448 to your computer and use it in GitHub Desktop.
Revisions
-
giacomelli revised this gist
Aug 11, 2020 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -11,12 +11,12 @@ /// http://diegogiacomelli.com.br/unitytips-gist-importer /// </summary> [InitializeOnLoad] public static class GistImporter { const string GistsFolder = "Gists"; static readonly Regex _getGistUrlInfoRegex = new Regex("https://gist.github.com/(?<owner>.+)/(?<gistId>[a-z0-9]+)", RegexOptions.IgnoreCase | RegexOptions.Compiled); static readonly Regex _getDescriptionRegex = new Regex(@"\<title\>(?<description>.+)\</title\>", RegexOptions.IgnoreCase | RegexOptions.Compiled); static readonly Regex _getFileUrlRegex = new Regex("href=\"(?<url>.+/raw/[a-z0-9\\./\\-]+)\"", RegexOptions.IgnoreCase | RegexOptions.Compiled); [MenuItem("Tools/Import Gist")] static void ImportGist() @@ -77,8 +77,8 @@ static bool ValidateImportGist() static void DownloadFile(WebClient client, string url, string destinationFolder, string filename) { var fileContent = client.DownloadString(url); // Is an editor script? if (fileContent.IndexOf("using UnityEditor;", StringComparison.OrdinalIgnoreCase) > -1) destinationFolder = Path.Combine(destinationFolder, "Editor"); -
giacomelli revised this gist
Jun 24, 2019 . 1 changed file with 13 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,6 @@ using System; using System.IO; using System.Linq; using System.Net; using System.Text.RegularExpressions; using UnityEditor; @@ -36,19 +37,22 @@ static void ImportGist() var infoMatch = _getGistUrlInfoRegex.Match(gistUrl).Groups; var gistOwner = infoMatch["owner"].Value; var gistId = infoMatch["gistId"].Value; var rawUrls = filesMatches .OfType<Match>() .Select(m => $"https://gist.github.com{m.Groups["url"].Value}") .OrderByDescending(u => u.EndsWith(".cs", StringComparison.OrdinalIgnoreCase)) .ToArray(); var destinationFolder = Path.Combine(Application.dataPath, GistsFolder, gistOwner, $"{Path.GetFileNameWithoutExtension(rawUrls.First())} ({gistId})"); // Downloads and write the Gist files. for (var i = 0; i < rawUrls.Length; i++) { var rawUrl = rawUrls[i]; var filename = Path.GetFileName(rawUrl); EditorUtility.DisplayProgressBar("Importing Gist...", filename, i / (float)filesMatches.Count); DownloadFile(client, rawUrl, destinationFolder, filename); } EditorUtility.ClearProgressBar(); @@ -86,7 +90,7 @@ static void WriteReadme(string gistUrl, string gistPageContent, string folder) { var description = _getDescriptionRegex.Match(gistPageContent).Groups["description"].Value; var readmePath = Path.Combine(folder, "readme.txt"); File.WriteAllText(readmePath, $"{description}\n\nUrl: {gistUrl}\nDate: {DateTime.Now:dd/MM/yyyy HH:mm}\n\nImported using Gist Importer (http://diegogiacomelli.com.br/unitytips-gist-importer)."); // Selects the readme file on project window. AssetDatabase.Refresh(); -
giacomelli revised this gist
Jun 24, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -86,7 +86,7 @@ static void WriteReadme(string gistUrl, string gistPageContent, string folder) { var description = _getDescriptionRegex.Match(gistPageContent).Groups["description"].Value; var readmePath = Path.Combine(folder, "readme.txt"); File.WriteAllText(readmePath, $"{description}\n\nUrl: {gistUrl}\nDate: {DateTime.Now:dd/MM/yyyy HH:mm}\n\nImported using {nameof(GistImporter)}(http://diegogiacomelli.com.br/unitytips-gist-importer)."); // Selects the readme file on project window. AssetDatabase.Refresh(); -
giacomelli created this gist
Jun 24, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,97 @@ using System; using System.IO; using System.Net; using System.Text.RegularExpressions; using UnityEditor; using UnityEngine; /// <summary> /// Gist importer. /// http://diegogiacomelli.com.br/unitytips-gist-importer /// </summary> [InitializeOnLoad] public static class GistImporter { const string GistsFolder = "Gists"; static readonly Regex _getGistUrlInfoRegex = new Regex("https://gist.github.com/(?<owner>.+)/(?<gistId>[a-z0-9]+)", RegexOptions.IgnoreCase | RegexOptions.Compiled); static readonly Regex _getDescriptionRegex = new Regex(@"\<title\>(?<description>.+)\</title\>", RegexOptions.IgnoreCase | RegexOptions.Compiled); static readonly Regex _getFileUrlRegex = new Regex("href=\"(?<url>.+/raw/.+)\"", RegexOptions.IgnoreCase | RegexOptions.Compiled); [MenuItem("Tools/Import Gist")] static void ImportGist() { var gistUrl = EditorGUIUtility.systemCopyBuffer; try { using (var client = new WebClient()) { var gistPageContent = client.DownloadString(gistUrl); // Extract the files raw urls. var filesMatches = _getFileUrlRegex.Matches(gistPageContent); if (filesMatches.Count > 0) { var infoMatch = _getGistUrlInfoRegex.Match(gistUrl).Groups; var gistOwner = infoMatch["owner"].Value; var gistId = infoMatch["gistId"].Value; var destinationFolder = Path.Combine(Application.dataPath, GistsFolder, gistOwner); // Downloads and write the Gist files. for (var i = 0; i < filesMatches.Count; i++) { var url = $"https://gist.github.com{filesMatches[i].Groups["url"].Value}"; var filename = Path.GetFileName(url); if (i == 0) destinationFolder = Path.Combine(destinationFolder, $"{Path.GetFileNameWithoutExtension(filename)} ({gistId})"); EditorUtility.DisplayProgressBar("Importing Gist...", filename, i / (float)filesMatches.Count); DownloadFile(client, url, destinationFolder, filename); } EditorUtility.ClearProgressBar(); WriteReadme(gistUrl, gistPageContent, destinationFolder); } else Debug.LogWarning($"No files found for Gist '{gistUrl}'."); } } finally { EditorUtility.ClearProgressBar(); } } [MenuItem("Tools/Import Gist", true)] static bool ValidateImportGist() { return _getGistUrlInfoRegex.IsMatch(EditorGUIUtility.systemCopyBuffer); } static void DownloadFile(WebClient client, string url, string destinationFolder, string filename) { var fileContent = client.DownloadString(url); // Is an editor script? if (fileContent.IndexOf("using UnityEditor;", StringComparison.OrdinalIgnoreCase) > -1) destinationFolder = Path.Combine(destinationFolder, "Editor"); Directory.CreateDirectory(destinationFolder); File.WriteAllText(Path.Combine(destinationFolder, filename), fileContent); } static void WriteReadme(string gistUrl, string gistPageContent, string folder) { var description = _getDescriptionRegex.Match(gistPageContent).Groups["description"].Value; var readmePath = Path.Combine(folder, "readme.txt"); File.WriteAllText(readmePath, $"Gist imported using {nameof(GistImporter)}:\n\n{description}\n\nUrl: {gistUrl}\nDate: {DateTime.Now:dd/MM/yyyy HH:mm}"); // Selects the readme file on project window. AssetDatabase.Refresh(); var readmeAsset = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>($"Assets{readmePath.Replace(Application.dataPath, string.Empty)}"); Selection.activeObject = readmeAsset; EditorGUIUtility.PingObject(readmeAsset); } }