Skip to content

Instantly share code, notes, and snippets.

@JohannesMP
Last active June 3, 2017 05:20
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 JohannesMP/74d2b5b58dc89aee03d8515c551943ac to your computer and use it in GitHub Desktop.
Save JohannesMP/74d2b5b58dc89aee03d8515c551943ac to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEditor;
using System.IO;
public class ScriptAssetProcessor : UnityEditor.AssetModificationProcessor
{
public static void OnWillCreateAsset(string path)
{
string filePath = GetAbsoluteFilePath(path.Replace(".meta", ""));
if(!FileIsScript(filePath)) return;
// Add a demo comment to the top of scripts
string fileContent = System.IO.File.ReadAllText(filePath);
fileContent = "// Demo!" + System.Environment.NewLine + fileContent;
System.IO.File.WriteAllText(filePath, fileContent);
AssetDatabase.Refresh(); // Force a script recompile
}
static bool FileIsScript(string filePath)
{
var extension = System.IO.Path.GetExtension(filePath);
return (extension == ".cs" || extension == ".js" || extension == ".boo");
}
static string GetAbsoluteFilePath(string path)
{
var index = Application.dataPath.LastIndexOf("Assets");
return Application.dataPath.Substring(0, index) + path;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment