Skip to content

Instantly share code, notes, and snippets.

@starikcetin
Last active November 7, 2019 19:21
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 starikcetin/ae9cf662eb8d201f0d0972e858c04a1f to your computer and use it in GitHub Desktop.
Save starikcetin/ae9cf662eb8d201f0d0972e858c04a1f to your computer and use it in GitHub Desktop.
NamespaceFillerAssetProcessor : Fills in #NAMESPACE# variables in Unity script templates. Source: https://stackoverflow.com/a/52395369/6301627
using System;
using UnityEditor;
using UnityEngine;
public class NamespaceFillerAssetProcessor : UnityEditor.AssetModificationProcessor
{
public static void OnWillCreateAsset(string path)
{
path = path.Replace(".meta", "");
var index = path.LastIndexOf(".", StringComparison.Ordinal);
if (index < 0)
{
return;
}
var file = path.Substring(index);
if (file != ".cs" && file != ".js" && file != ".boo")
{
return;
}
index = Application.dataPath.LastIndexOf("Assets", StringComparison.Ordinal);
path = Application.dataPath.Substring(0, index) + path;
file = System.IO.File.ReadAllText(path);
var lastPart = path.Substring(path.IndexOf("Assets", StringComparison.Ordinal));
var _namespace = lastPart.Substring(0, lastPart.LastIndexOf('/'));
_namespace = _namespace.Replace('/', '.');
file = file.Replace("#NAMESPACE#", _namespace);
System.IO.File.WriteAllText(path, file);
AssetDatabase.Refresh();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment