Skip to content

Instantly share code, notes, and snippets.

@nyakagawan
Created September 8, 2017 10:26
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 nyakagawan/1e210ab47787a0369e1c41008e9a033b to your computer and use it in GitHub Desktop.
Save nyakagawan/1e210ab47787a0369e1c41008e9a033b to your computer and use it in GitHub Desktop.
public static class SpriteAtlasScriptIF
{
//SpriteAtlasアセットを新規作成して、作成したアセットのパスを返す
public static string CreateSpriteAtlas(string path, string[] folders, string subDir, FilterMode filterMode, bool enableTightPackage)
{
var insertLines = new List<string>();
foreach (var dir in folders)
{
var guid = AssetDatabase.AssetPathToGUID(dir);
insertLines.Add(string.Format(SpriteAtlasAssetPackableLineTemplate, guid));
}
var contents = SpriteAtlasAssetContentsTemplate.Trim();
contents = contents.Replace("###name###", System.IO.Path.GetFileNameWithoutExtension(path));
contents = contents.Replace("###packables###", string.Join("\n", insertLines));
contents = contents.Replace("###filterMode###", ((int)filterMode).ToString());
contents = contents.Replace("###enableTightPacking###", enableTightPackage ? "1" : "0");
System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(path));
System.IO.File.WriteAllText(path, contents);
AssetDatabase.ImportAsset(path);
return path;
}
public static void ModifyFilterMode(string atlasPath, FilterMode mode)
{
var asset = AssetDatabase.LoadAssetAtPath<UnityEngine.U2D.SpriteAtlas>(atlasPath);
Debug.Log(atlasPath);
var so = new SerializedObject(asset);
var p = so.FindProperty("m_EditorData");
p = p.FindPropertyRelative("textureSettings");
p.FindPropertyRelative("filterMode").intValue = (int)mode;
so.ApplyModifiedProperties();
}
public static void ModifyEnableTightPacking(string atlasPath, bool tightPacking)
{
var asset = AssetDatabase.LoadAssetAtPath<UnityEngine.U2D.SpriteAtlas>(atlasPath);
var so = new SerializedObject(asset);
var p = so.FindProperty("m_EditorData");
p = p.FindPropertyRelative("packingParameters");
p.FindPropertyRelative("enableTightPacking").boolValue = tightPacking;
so.ApplyModifiedProperties();
}
const string SpriteAtlasAssetPackableLineTemplate = " - {{fileID: 102900000, guid: {0}, type: 3}}";
const string SpriteAtlasAssetContentsTemplate = @"%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!687078895 &4343727234628468602
SpriteAtlas:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_Name: ###name###
m_EditorData:
textureSettings:
anisoLevel: 1
compressionQuality: 50
maxTextureSize: 2048
textureCompression: 0
colorSpace: 1
filterMode: ###filterMode###
generateMipMaps: 0
readable: 0
crunchedCompression: 0
platformSettings: []
packingParameters:
paddingPower: 2
blockOffset: 1
allowAlphaSplitting: 0
enableRotation: 1
enableTightPacking: ###enableTightPacking###
packedHash:
serializedVersion: 2
Hash: 00000000000000000000000000000000
variantMultiplier: 1
bindAsDefault: 1
finalFormat: -1
hashString:
totalSpriteSurfaceArea: 0
packables:
###packables###
packedSpriteRenderDataKeys: []
m_MasterAtlas: {fileID: 0}
m_PackedSprites: []
m_PackedSpriteNamesToIndex: []
m_Tag: New Sprite Atlas
m_IsVariant: 0
";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment