Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Last active September 27, 2018 09:23
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsubaki/25701206ca9d8f9b3e74 to your computer and use it in GitHub Desktop.
Save tsubaki/25701206ca9d8f9b3e74 to your computer and use it in GitHub Desktop.
UnityのResourcesに配置したテクスチャのAtlasが持つスプライトを列挙型にする
using System.CodeDom.Compiler;
using System.IO;
using System.Collections.Generic;
public class SpriteEnumGenerator : AssetPostprocessor {
readonly static string exportDirectry = "Assets/Enum/";
readonly static string generateCodeNamespace = "ResourcesEnum";
static public void OnPostprocessAllAssets(
string[] importedAssets,
string[] deletedAssets,
string[] movedAssets,
string[] movedFromAssetPaths)
{
foreach( var asset in importedAssets )
{
var importer = AssetImporter.GetAtPath(asset);
// インポートした物がResources以下のパック済Spriteか判別
var pos = asset.IndexOf("Resources");
TextureImporter texImporter = importer as TextureImporter;
if( texImporter == null ||
texImporter.spriteImportMode != SpriteImportMode.Multiple ||
pos == -1)
continue;
// Sprite名の取得
string fileName = Path.GetFileNameWithoutExtension(asset.Substring(pos + 10));
string filePath = Path.GetDirectoryName(asset.Substring(pos + 10));
List<string> names = new List<string>();
foreach( var name in Resources.LoadAll<Sprite>((string.IsNullOrEmpty(filePath) ? "" : filePath + "/") + fileName) )
{
names.Add(name.name.Replace(".",""));
}
Resources.UnloadUnusedAssets();
GenerateEnum(fileName, names.ToArray());
}
// リフレッシュ
AssetDatabase.Refresh();
}
static void GenerateEnum(string baseSpriteName, string[] spriteName)
{
// 作るコードのファイル名を決定
string fileName = exportDirectry + baseSpriteName + ".cs";
Directory.CreateDirectory(exportDirectry);
// Namespaceを作ってコードに登録
var cnamespace = new CodeNamespace(generateCodeNamespace);
var unit = new CodeCompileUnit();
unit.Namespaces.Add(cnamespace);
// enum型を決定
CodeTypeDeclaration type = new CodeTypeDeclaration(baseSpriteName){ IsEnum = true };
// enumの中身を定義
int i=0;
foreach (var valueName in spriteName)
{
CodeMemberField f = new CodeMemberField(baseSpriteName, valueName); // enum作成
f.InitExpression = new CodePrimitiveExpression(i++); // enumの標準値を設定
type.Members.Add(f); // 型に登録
}
cnamespace.Types.Add(type);
// コード生成
using (StreamWriter sourceWriter = new StreamWriter(fileName, false))
{
var provider = CodeDomProvider.CreateProvider("CSharp");
var options = new CodeGeneratorOptions(){ BracingStyle = "C"};
provider.GenerateCodeFromCompileUnit( unit, sourceWriter, options);
}
}
}
@tsubaki
Copy link
Author

tsubaki commented Dec 15, 2014

Resourcesフォルダ以下に配置したスプライトの持つスプライト名を列挙型に変換する。

例えばResources以下に以下のようなパスのスプライトがあった場合、
Icons
|- Twitter
|-Facebook
|-Game

こんなコードが自動的に作られて…

namespace ResourcesEnum
{
    public enum Icons
    {
        Twitter = 0,
        Facebook = 1,
        Game = 2,
    }
}
using ResourcesEnum;
...
var sprite = Resources.LoadAll<Sprite>("Icons")[(int)Icons.Twitter];

のような形で取得できるようになる。

要Editorフォルダ以下に配置。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment