Last active
December 8, 2021 21:32
-
-
Save Ragzouken/b0ba3b1d602383e02086 to your computer and use it in GitHub Desktop.
simple gist thing for unity/c#
This file contains 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 characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System; | |
using System.Text; | |
namespace MakeGist | |
{ | |
public class Gist | |
{ | |
public static IEnumerator Make(string description, | |
Dictionary<string, string> files, | |
Action<string> onComplete) | |
{ | |
var fileString = new StringBuilder(); | |
fileString.AppendLine("{"); | |
bool first = true; | |
foreach (var file in files) | |
{ | |
if (!first) fileString.AppendLine(","); | |
fileString.AppendFormat("\"{0}\": {{ \"content\": \"{1}\" }}", | |
file.Key, | |
file.Value); | |
if (first) first = false; | |
} | |
fileString.AppendLine(); | |
fileString.AppendLine("}"); | |
var encoding = new UTF8Encoding(); | |
var json = @"{ | |
""description"":""" + description + @""", | |
""public"":true, | |
""files"": " + fileString.ToString() + @" | |
}"; | |
var request = new WWW("https://api.github.com/gists", | |
encoding.GetBytes(json)); | |
yield return request; | |
var response = SimpleJSON.JSON.Parse(request.text); | |
onComplete(response["id"]); | |
} | |
public static IEnumerator Take(string id, | |
Action<Dictionary<string, string>> onComplete) | |
{ | |
var files = new Dictionary<string, string>(); | |
var request = new WWW("https://api.github.com/gists/" + id); | |
yield return request; | |
var response = SimpleJSON.JSON.Parse(request.text); | |
foreach (string file in response["files"].Keys) | |
{ | |
files.Add(file, response["files"][file]["content"]); | |
} | |
onComplete(files); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment