private static string SliceSprite(string asset) { var directoryName = Path.GetFileName(Path.GetDirectoryName(asset)); var directoryPath = Path.Combine(EditorUtil.GetBaumSpritesPath(), directoryName); var fileName = Path.GetFileName(asset); var noSlice = fileName.EndsWith("-noslice.png", StringComparison.Ordinal); fileName = fileName.Replace("-noslice.png", ".png"); var newPath = Path.Combine(directoryPath, fileName); var texture = AssetDatabase.LoadAssetAtPath <Texture2D>(asset); var slicedTexture = new SlicedTexture(texture, new Boarder(0, 0, 0, 0)); if (!noSlice) { slicedTexture = TextureSlicer.Slice(texture); } if (PreprocessTexture.SlicedTextures == null) { PreprocessTexture.SlicedTextures = new Dictionary <string, SlicedTexture>(); } PreprocessTexture.SlicedTextures[fileName] = slicedTexture; byte[] pngData = slicedTexture.Texture.EncodeToPNG(); File.WriteAllBytes(newPath, pngData); if (!noSlice) { Object.DestroyImmediate(slicedTexture.Texture); } // Debug.LogFormat("[Baum2] Slice: {0} -> {1}", EditorUtil.ToUnityPath(asset), EditorUtil.ToUnityPath(newPath)); return(EditorUtil.ToUnityPath(newPath)); }
private static void SliceSprite(string asset) { var directoryName = Path.GetFileName(Path.GetDirectoryName(asset)); var directoryPath = Path.Combine(EditorUtil.GetBaumSpritesPath(), directoryName); var fileName = Path.GetFileName(asset); var newPath = Path.Combine(directoryPath, fileName); var texture = AssetDatabase.LoadAssetAtPath <Texture2D>(asset); var slicedTexture = TextureSlicer.Slice(texture); if (PreprocessTexture.SlicedTextures == null) { PreprocessTexture.SlicedTextures = new Dictionary <string, SlicedTexture>(); } PreprocessTexture.SlicedTextures[fileName] = slicedTexture; byte[] pngData = slicedTexture.Texture.EncodeToPNG(); File.WriteAllBytes(newPath, pngData); Object.DestroyImmediate(slicedTexture.Texture); // Debug.LogFormat("[Baum2] Slice: {0} -> {1}", EditorUtil.ToUnityPath(asset), EditorUtil.ToUnityPath(newPath)); }
private static void Setup ( IEnumerable <Texture2D> textureList, DisplayProgressBarCallback onDisplayProgressBarPreprocess = default, DisplayProgressBarCallback onDisplayProgressBarProcessing = default, ClearProgressBarCallback onClearProgressBar = default, Action onComplete = default ) { var list = textureList .Select(c => new TextureData(c)) .ToArray() ; var count = list.Length; try { AssetDatabase.StartAssetEditing(); foreach (var(index, val) in list.Select((val, index) => (index, val))) { onDisplayProgressBarPreprocess?.Invoke(index + 1, count, val.Path); var importer = val.Importer; importer.isReadable = true; importer.SaveAndReimport(); } } finally { AssetDatabase.StopAssetEditing(); onClearProgressBar?.Invoke(); } // isReadable を true にしてからじゃないと TextureSlicer.Slice が使用できないため // isReadable を true にして 1 フレーム待機してから // TextureSlicer.Slice を使用しています EditorApplication.delayCall += () => { try { AssetDatabase.StartAssetEditing(); foreach (var(index, val) in list.Select((val, index) => (index, val))) { onDisplayProgressBarProcessing?.Invoke(index + 1, count, val.Path); var slicedTexture = TextureSlicer.Slice(val.Texture); var importer = val.Importer; importer.spriteBorder = slicedTexture.Boarder.ToVector4(); importer.isReadable = false; importer.SaveAndReimport(); } } finally { AssetDatabase.StopAssetEditing(); onClearProgressBar?.Invoke(); onComplete?.Invoke(); } }; }
/// <summary> /// アセットのイメージをスライスする /// 戻り地は、変換リザルトメッセージ /// </summary> /// <param name="outputPath"></param> /// <param name="sourceImagePath"></param> /// <returns></returns> public static string SliceSprite(string outputPath, string sourceImagePath) { // オプションJSONの読み込み Dictionary <string, object> json = null; var imageJsonPath = sourceImagePath + ".json"; if (File.Exists(imageJsonPath)) { var text = File.ReadAllText(imageJsonPath); json = Json.Deserialize(text) as Dictionary <string, object>; } // PNGを読み込み、同じサイズのTextureを作成する var sourceTexture = CreateTextureFromPng(sourceImagePath); var optionJson = json.GetDic("copy_rect"); var readableTexture = CreateReadableTexture2D(sourceTexture, optionJson?.GetInt("offset_x"), optionJson?.GetInt("offset_y"), optionJson?.GetInt("width"), optionJson?.GetInt("height") ); if (readableTexture == null) { Debug.LogError($"readableTextureがNULLです{sourceImagePath}"); } // LoadAssetAtPathをつかったテクスチャ読み込み サイズが2のべき乗になる JPGも読める // var texture = CreateReadableTexture2D(AssetDatabase.LoadAssetAtPath<Texture2D>(asset)); if (PreprocessTexture.SlicedTextures == null) { PreprocessTexture.SlicedTextures = new Dictionary <string, SlicedTexture>(); } var slice = json?.Get("slice").ToLower(); switch (slice) { case null: case "auto": { var slicedTexture = TextureSlicer.Slice(readableTexture); return(CheckWriteSpriteFromTexture(outputPath, slicedTexture.Texture, slicedTexture.Boarder)); } case "none": { return(CheckWriteSpriteFromTexture(outputPath, readableTexture, new Boarder(0, 0, 0, 0))); } case "border": { var border = json.GetDic("slice_border"); if (border == null) { break; // borderパラメータがなかった } // 上・右・下・左の端から内側へのオフセット量 var top = border.GetInt("top") ?? 0; var right = border.GetInt("right") ?? 0; var bottom = border.GetInt("bottom") ?? 0; var left = border.GetInt("left") ?? 0; return(CheckWriteSpriteFromTexture(outputPath, readableTexture, new Boarder(left, bottom, right, top))); } } Debug.LogError($"[{Importer.NAME}] SliceSpriteの処理ができませんでした"); return(null); }
public static string SliceSprite(string assetPath) { var directoryName = Path.GetFileName(Path.GetDirectoryName(assetPath)); var directoryPath = Path.Combine(EditorUtil.GetOutputSpritesPath(), directoryName); var fileName = Path.GetFileName(assetPath); // PNGを読み込み、同じサイズのTextureを作成する var texture = CreateReadableTexture2D(CreateTextureFromPng(assetPath)); // LoadAssetAtPathをつかったテクスチャ読み込み サイズが2のべき乗になる JPGも読める //var texture = CreateReadabeTexture2D(AssetDatabase.LoadAssetAtPath<Texture2D>(asset)); if (PreprocessTexture.SlicedTextures == null) { PreprocessTexture.SlicedTextures = new Dictionary <string, SlicedTexture>(); } var noSlice = fileName.EndsWith("-noslice.png", StringComparison.Ordinal); if (noSlice) { var slicedTexture = new SlicedTexture(texture, new Boarder(0, 0, 0, 0)); fileName = fileName.Replace("-noslice.png", ".png"); var newPath = Path.Combine(directoryPath, fileName); PreprocessTexture.SlicedTextures[fileName] = slicedTexture; var pngData = texture.EncodeToPNG(); var imageHash = texture.imageContentsHash; Object.DestroyImmediate(slicedTexture.Texture); return(CheckWrite(newPath, pngData, imageHash)); } const string pattern = "-9slice,([0-9]+)px,([0-9]+)px,([0-9]+)px,([0-9]+)px\\.png"; var matches = Regex.Match(fileName, pattern); if (matches.Length > 0) { // 上・右・下・左の端から内側へのオフセット量 var top = Int32.Parse(matches.Groups[1].Value); var right = Int32.Parse(matches.Groups[2].Value); var bottom = Int32.Parse(matches.Groups[3].Value); var left = Int32.Parse(matches.Groups[4].Value); var slicedTexture = new SlicedTexture(texture, new Boarder(left, bottom, right, top)); fileName = Regex.Replace(fileName, pattern, ".png"); var newPath = Path.Combine(directoryPath, fileName); PreprocessTexture.SlicedTextures[fileName] = slicedTexture; var pngData = texture.EncodeToPNG(); var imageHash = texture.imageContentsHash; Object.DestroyImmediate(slicedTexture.Texture); return(CheckWrite(newPath, pngData, imageHash)); } { var slicedTexture = TextureSlicer.Slice(texture); var newPath = Path.Combine(directoryPath, fileName); PreprocessTexture.SlicedTextures[fileName] = slicedTexture; var pngData = slicedTexture.Texture.EncodeToPNG(); var imageHash = texture.imageContentsHash; Object.DestroyImmediate(slicedTexture.Texture); return(CheckWrite(newPath, pngData, imageHash)); } // Debug.LogFormat("[XdUnityUI] Slice: {0} -> {1}", EditorUtil.ToUnityPath(asset), EditorUtil.ToUnityPath(newPath)); }