1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
| using System.IO; using UnityEditor; using UnityEngine;
public class SpriteImporterTool : UnityEditor.Editor { public static int continuousColNum = 10; //continuousColNum:连续的列数,大于等于这个值判定为横向可以九宫处理 public static int continuousRowNum = 10; //continuousRowNum:连续的行数,大于等于这个值判定为纵向可以九宫处理 //自动计算Sprite Border数据 [MenuItem("Assets/SpriteBorder/AutoSetSpriteBorder", false, 500)] public static void AutoSetSpriteBorder() { foreach (var spriteAsset in Selection.objects) { AutoSetSpriteBorder(AssetDatabase.GetAssetPath(spriteAsset)); } }
//设置Sprite Border为Zero [MenuItem("Assets/SpriteBorder/Reset Zero", false)] public static void AutoSetSpriteBorderZero() { foreach (var spriteAsset in Selection.objects) { SetSpriteBorder(AssetDatabase.GetAssetPath(spriteAsset), Vector4.zero); } }
private static void AutoSetSpriteBorder(string spriteAssetPath) { TextureImporter textureImporter = AssetImporter.GetAtPath(spriteAssetPath) as TextureImporter; if (textureImporter != null && textureImporter.textureType == TextureImporterType.Sprite) { Sprite sprite = AssetDatabase.LoadAssetAtPath<Sprite>(spriteAssetPath);
Vector4 border = Vector4.zero; GetBorderSliceInfo(sprite.texture, continuousColNum, continuousRowNum, ref border);
if (border != Vector4.zero) { textureImporter.spriteBorder = border; EditorUtility.SetDirty(sprite); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } } }
private static void SetSpriteBorder(string spriteAssetPath, Vector4 border) { TextureImporter textureImporter = AssetImporter.GetAtPath(spriteAssetPath) as TextureImporter; if (textureImporter != null && textureImporter.textureType == TextureImporterType.Sprite) { Sprite sprite = AssetDatabase.LoadAssetAtPath<Sprite>(spriteAssetPath);
textureImporter.spriteBorder = border; EditorUtility.SetDirty(sprite); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } }
public static void GetBorderSliceInfo(Texture2D texture, int continuousColNum, int continuousRowNum, ref Vector4 border) { int width = texture.width; int height = texture.height;
//咳咳,这里的命名规则请忽略,从另一个python工具拷贝过来 int cur_begin_col_index = 0; int cur_end_col_index = 0; int slice_begin_col_index = 0; int slice_end_col_index = 0;
Texture2D readTexture2D = GetReadTexture2D(texture); for (int col = 0; col < width - 1; col++) { if (EqualColPixel(readTexture2D, col, col + 1) == false) { if ((cur_begin_col_index != cur_end_col_index) && (cur_end_col_index - cur_begin_col_index >= continuousColNum) && (cur_end_col_index - cur_begin_col_index > slice_end_col_index - slice_begin_col_index)) { slice_begin_col_index = cur_begin_col_index; slice_end_col_index = cur_end_col_index; }
cur_begin_col_index = col + 1; cur_end_col_index = col + 1; } else { cur_end_col_index = col + 1; } }
int cur_begin_row_index = 0; int cur_end_row_index = 0; int slice_begin_row_index = 0; int slice_end_row_index = 0;
for (int row = 0; row < height - 1; row++) { if (EqualRowPixel(readTexture2D, row, row + 1) == false) { if ((cur_begin_row_index != cur_end_row_index) && (cur_end_row_index - cur_begin_row_index >= continuousRowNum) && (cur_end_row_index - cur_begin_row_index > slice_end_row_index - slice_begin_row_index)) { slice_begin_row_index = cur_begin_row_index; slice_end_row_index = cur_end_row_index; }
cur_begin_row_index = row + 1; cur_end_row_index = row + 1; } else { cur_end_row_index = row + 1; } }
if (slice_end_col_index - slice_begin_col_index < continuousColNum) { slice_begin_col_index = 0; slice_end_col_index = width - 1; }
if (slice_end_row_index - slice_begin_row_index < continuousRowNum) { slice_begin_row_index = 0; slice_end_row_index = height - 1; }
//LTRB //row 是纵向的 T B //col 是横向的 L R border = new Vector4(slice_begin_col_index, slice_begin_row_index, width - slice_end_col_index - 1, height - slice_end_row_index - 1); }
public static string GetAssetFullPath(UnityEngine.Object asset) { return System.Environment.CurrentDirectory + Path.DirectorySeparatorChar + AssetDatabase.GetAssetPath(asset); }
public static Texture2D GetReadTexture2D(Texture texture) { string textureFilePath = GetAssetFullPath(texture); FileStream fileStream = new FileStream(textureFilePath, FileMode.Open, FileAccess.Read); fileStream.Seek(0, SeekOrigin.Begin); byte[] bytes = new byte[fileStream.Length]; fileStream.Read(bytes, 0, (int) fileStream.Length); fileStream.Close(); fileStream.Dispose(); Texture2D new_texture = new Texture2D(texture.width, texture.height); new_texture.LoadImage(bytes); return new_texture; }
/// <summary> /// 比较两行像素是否完全相等 /// </summary> /// <param name="texture">isReadable为true,可读的texture</param> /// <param name="row1"></param> /// <param name="row2"></param> /// <returns></returns> public static bool EqualRowPixel(Texture2D texture, int row1, int row2) { Color[] row1Colors = texture.GetPixels(0, row1, texture.width, 1); Color[] row2Colors = texture.GetPixels(0, row2, texture.width, 1); for (int i = 0; i < row1Colors.Length; i++) { if (!row1Colors[i].Equals(row2Colors[i])) { return false; } }
return true; }
/// <summary> /// 比较两列像素是否完全相等 /// </summary> /// <param name="texture">isReadable为true,可读的texture</param> /// <param name="col1"></param> /// <param name="col2"></param> /// <returns></returns> public static bool EqualColPixel(Texture2D texture, int col1, int col2) { Color[] col1Colors = texture.GetPixels(col1, 0, 1, texture.height); Color[] col2Colors = texture.GetPixels(col2, 0, 1, texture.height); for (int i = 0; i < col1Colors.Length; i++) { if (!col1Colors[i].Equals(col2Colors[i])) { return false; } }
return true; } }
|