はじめに
「OnionRing」を Unity プロジェクトに導入することで
画像を 9 スライス用に変換できるようになります
使用例
変換前
変換後
使用例
using System.Collections.Generic; using System.IO; using UnityEngine; using UnityEditor; using OnionRing; public sealed class Example : AssetPostprocessor { private const string INPUT_DIR = "Assets/Textures/In"; private const string OUTPUT_DIR = "Assets/Textures/Out"; private static readonly Dictionary<string, SlicedTexture> m_textures = new Dictionary<string, SlicedTexture>(); public void OnPostprocessTexture( Texture2D source ) { if ( !assetPath.Contains( INPUT_DIR ) ) return; var texture = TextureSlicer.Slice( source ); var png = texture.Texture.EncodeToPNG(); var path = assetPath.Replace( INPUT_DIR, OUTPUT_DIR ); File.WriteAllBytes( path, png ); var filename = Path.GetFileNameWithoutExtension( assetPath ); m_textures[ filename ] = texture; EditorApplication.delayCall += () => AssetDatabase.Refresh(); } private void OnPreprocessTexture() { if ( !assetPath.Contains( OUTPUT_DIR ) ) return; var filename = Path.GetFileNameWithoutExtension( assetPath ); if ( !m_textures.ContainsKey( filename ) ) return; var texture = m_textures[ filename ]; var importer = assetImporter as TextureImporter; importer.spriteBorder = texture.Boarder.ToVector4(); } }
上記のようなエディタ拡張のスクリプトを作成して使用します