Skip to content

Instantly share code, notes, and snippets.

@ciro-unity
Last active December 1, 2016 16:45
Show Gist options
  • Save ciro-unity/e60db6f4e055f7995925cb1b2a70336a to your computer and use it in GitHub Desktop.
Save ciro-unity/e60db6f4e055f7995925cb1b2a70336a to your computer and use it in GitHub Desktop.
An Editor script that speeds up delivery of code-heavy workshops. It that allows to pull different versions of the code directly inside the Unity Project, by selecting a step from the top bar menu.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEditor;
using UnityEditor.SceneManagement;
using System;
using System.IO;
public class InjectionTool : MonoBehaviour
{
// These provide the name of the source and destination script folders
static string versionedScriptsFolder = "Sources/Step";
static string destinationScriptsFolder = "Assets/Scripts";
static string currentLocation;
static bool isRootFolder;
const string menuName = "Code Version"; //the name of the menu item at the top
#region MENU ITEMS
// Example method to load a version of the code
// Note that you will need to manually duplicate these methods for as many steps as you need
[MenuItem(menuName + "/Step 0 - The Beginning")]
static public void StepZero()
{
Version(0);
//you can put custom actions here that modify the scene to show the version that was just loaded?
GameObject.FindObjectOfType<Text>().text = "Version 0";
}
[MenuItem(menuName + "/Step 1 - Something is Moving")]
static public void StepOne()
{
Version(1);
GameObject.FindObjectOfType<Text>().text = "Version 1";
}
/*
// Example method to load a version of a scene
[MenuItem(menuName + "/Scene Version/Step 0")]
static public void SceneOne()
{
EditorSceneManager.OpenScene("Assets/Scenes/EmptyScene.unity", OpenSceneMode.Single);
}
*/
#endregion
#region FUNCTIONALITY
// This method is called by all code versioning methods above
// It scans the Sources folder for all .cs files, and puts them into the Scripts folder
// Sources and Scripts folder names are taken from the variables at the beginning of this file
static private void Version(int stepNumber)
{
DirectoryInfo directoryInfo;
FileInfo[] fileInfoArray;
isRootFolder = true;
//1: delete the files in the destination to clean the step, in case the user is going back in the history
directoryInfo = new DirectoryInfo(destinationScriptsFolder);
fileInfoArray = directoryInfo.GetFiles();
DirectoryInfo[] dirs = directoryInfo.GetDirectories();
foreach(FileInfo f in fileInfoArray)
{
f.Delete();
}
foreach(DirectoryInfo d in dirs)
{
d.Delete(true); //delete all sub-directories recursively
}
//2: write the files from the source folder to the destination
directoryInfo = new DirectoryInfo(versionedScriptsFolder + stepNumber);
currentLocation = destinationScriptsFolder; //start from the Scripts folder in Assets
CopyFolder(directoryInfo);
//3: force a recompile
AssetDatabase.Refresh();
}
// This method scans a directory, copies all the files, then does the same for each sub-directory found
static private void CopyFolder(DirectoryInfo directoryInfo)
{
//Debug.Log("Scanning " + directoryInfo.Name);
string currentSubDir;
//create the directory (only if it's not the root directory)
if(isRootFolder)
{
currentSubDir = currentLocation;
isRootFolder = false;
}
else
{
currentSubDir = currentLocation + "/" + directoryInfo.Name;
Directory.CreateDirectory(currentSubDir);
//Debug.Log("Created " + currentSubDir);
}
//copy all the files in the new destination
FileInfo[] fileInfoArray = directoryInfo.GetFiles();
foreach (FileInfo f in fileInfoArray)
{
if(f.Extension == ".cs")
{
//read source file
StreamReader sr = File.OpenText(f.FullName);
string contents = sr.ReadToEnd();
sr.Close();
//write in the destination file
//(will be created if it doesn't exist)
string destinationPath = currentSubDir + "/" + f.Name;
StreamWriter sw = new StreamWriter(destinationPath);
sw.Write(contents);
sw.Close();
//Debug.Log("Copied " + f.FullName + " to " + destinationPath);
}
}
//recursive call for each directory included in this one
DirectoryInfo[] dirs = directoryInfo.GetDirectories();
foreach(DirectoryInfo d in dirs)
{
CopyFolder(d);
}
currentLocation = currentSubDir;
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment