Skip to content

Instantly share code, notes, and snippets.

@anchan828
Created February 11, 2013 07:55
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 anchan828/3cf9014a8493ab023925 to your computer and use it in GitHub Desktop.
Save anchan828/3cf9014a8493ab023925 to your computer and use it in GitHub Desktop.
Tag名を定数で扱えるように
using UnityEngine;
using UnityEditor;
using System.Linq;
using UnityEditorInternal;
using System.Collections.Generic;
using System;
[InitializeOnLoad]
public class TagNameCreator
{
private const string TAGNAME_HASH_KEY = "TagName_Hash";
[MenuItem("Assets/TagNameCreator")]
public static void _TagNameCreator ()
{
if (EditorApplication.isPlaying || Application.isPlaying)
return;
BuildTagName ();
}
static TagNameCreator ()
{
if (EditorApplication.isPlaying || Application.isPlaying)
return;
BuildTagName ();
}
static void BuildTagName ()
{
System.Text.StringBuilder builder = new System.Text.StringBuilder ();
builder = WriteTagManagerClass (builder);
string text = builder.ToString ().Replace (",}", "}");
string assetPath = currentFolderPath + "../TagName.cs";
if (AssetDatabase.LoadAssetAtPath (assetPath.Replace ("/Editor/..", ""), typeof(UnityEngine.Object)) != null && EditorPrefs.GetInt (TAGNAME_HASH_KEY, 0) == text.GetHashCode ())
return;
System.IO.File.WriteAllText (assetPath, text);
EditorPrefs.SetInt (TAGNAME_HASH_KEY, text.GetHashCode ());
AssetDatabase.Refresh (ImportAssetOptions.ImportRecursive);
}
static System.Text.StringBuilder WriteTagManagerClass (System.Text.StringBuilder builder)
{
List<string> tagNames = InternalEditorUtility.tags.ToList ();
builder.AppendLine ("public class TagName");
builder.AppendLine ("{");
{
WriteTagNameFunction (builder, tagNames);
}
{
WriteTagNameArray (builder, tagNames);
}
builder.AppendLine ("}");
return builder;
}
static void WriteTagNameFunction (System.Text.StringBuilder builder, List<string> tagNames)
{
tagNames.ToList ().ForEach (tagName =>
{
builder.Append ("\t").AppendLine ("/// <summary>");
builder.Append ("\t").AppendFormat ("/// return \"{0}\"", tagName).AppendLine ();
builder.Append ("\t").AppendLine ("/// </summary>");
builder.Append ("\t").AppendFormat (@"public static string @{0} = ""{1}"";", Replace (tagName), tagName).AppendLine ();
});
}
static void WriteTagNameArray (System.Text.StringBuilder builder, List<string> tagNames)
{
builder.Append ("\t").Append ("public static readonly string[] TAGS = new string[]{");
tagNames.ForEach (tagName => builder.AppendFormat (@"""{0}"",", tagName));
builder.AppendLine ("};");
}
static string Replace (string name)
{
string[] invalidChar = new string[] {" ", "!", "\"", "#", "$", "%", "&", "\'", "(", ")", "-", "=", "^", "~", "¥", "|", "[", "{", "@", "`", "]", "}", ":", "*", ";", "+", "/", "?", ".", ">", ",", "<"};
invalidChar.ToList ().ForEach (s => name = name.Replace (s, string.Empty));
return name;
}
static string currentFolderPath {
get {
string currentFilePath = new System.Diagnostics.StackTrace (true).GetFrame (0).GetFileName ();
return "Assets" + currentFilePath.Substring (0, currentFilePath.LastIndexOf ("/") + 1).Replace (Application.dataPath, string.Empty);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment