Created
June 26, 2018 09:12
-
-
Save comoc/39c6f0f1ba220f995d3c1d5a814a616a to your computer and use it in GitHub Desktop.
An editor extension makes easier to add a .gitignore file to the root folder of current Unity project
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 characters
// Please locate this file as Assets/Editor/GitignoreGenerator.cs | |
// Then select "Custom" > "Add .gitignore to project root from menu". | |
using UnityEngine; | |
using UnityEngine.Networking; | |
using UnityEditor; | |
using System.IO; | |
using System; | |
public class GitignoreGenerator : EditorWindow { | |
static readonly string URL = "https://raw.githubusercontent.com/github/gitignore/master/Unity.gitignore"; | |
[MenuItem("Custom/Add .gitignore to project root")] | |
public static void AddGitignore() { | |
string projectFolder = Directory.GetCurrentDirectory(); | |
string gitignoreFile = Path.Combine(projectFolder, ".gitignore"); | |
if (File.Exists(gitignoreFile)) { | |
Debug.Log(".gitignore file is already existing"); | |
return; | |
} | |
UnityWebRequest www = UnityWebRequest.Get(URL); | |
www.SendWebRequest(); | |
while (!www.isDone) { | |
System.Threading.Thread.Sleep(1); | |
} | |
if (www.isNetworkError || www.isHttpError) { | |
Debug.Log("Error " + www.error); | |
return; | |
} | |
try { | |
FileStream fs = File.OpenWrite(gitignoreFile); | |
StreamWriter sw = new StreamWriter(fs); | |
sw.Write(www.downloadHandler.text); | |
sw.Close(); | |
fs.Close(); | |
Debug.Log(".gitignore file was created"); | |
} catch (Exception ex) { | |
Debug.Log(ex.Message); | |
} | |
} | |
void OnGUI() { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment