首页 > 其他分享 >生成9宫图

生成9宫图

时间:2022-10-29 01:33:22浏览次数:43  
标签:sprite ninePatchBorder 生成 bottomPadding texImporter var 宫图 rect

# 比如,美术直接给了一张原大小的弹框背景图,但实际该图可以做成九宫可拉伸格形式,但是美术又不愿意再重切一次,

那我们就自己来吧,先用Unity的9宫工具,框出要保留的四个角

# 然后再用工具执行下,就能得到这样一张图片

 

using System.IO;
using UnityEditor;
using UnityEngine;

public class NinePatchTool
{

    [MenuItem("Assets/图片4个角生成9宫图")]
    static void Create9Patch()
    {
        if (null == Selection.objects || Selection.objects.Length <= 0)
            return;

        for (var i = 0; i < Selection.objects.Length; ++i)
        {
            var obj = Selection.objects[i];
            var path = AssetDatabase.GetAssetPath(obj);
            //Debug.Log($"{obj.GetType().Name}: {path}");
            var sprite = AssetDatabase.LoadAssetAtPath<Sprite>(path);
            if (null == sprite)
                continue;

            //Debug.Log($"border:{sprite.border}, rect: {sprite.rect}");
            var srcWdith = (int)sprite.rect.width;
            var srcHeight = (int)sprite.rect.height;

            var srcTempRT = RenderTexture.GetTemporary(srcWdith, srcHeight, 0, RenderTextureFormat.ARGB32);
            Graphics.SetRenderTarget(srcTempRT);
            Graphics.Blit(sprite.texture, srcTempRT);

            var ninePatchBorder = sprite.border;
            if (0 == ninePatchBorder.x || 0 == ninePatchBorder.y || 0 == ninePatchBorder.z || 0 == ninePatchBorder.w)
            {
                Debug.Log($"某个角的没拉9宫线: {ninePatchBorder}");
                continue;
            }

            //多3个像素是用于新的3x3拉伸区域
            var leftPadding = ninePatchBorder.x + 3;
            var topPadding = ninePatchBorder.y + 3;
            var rightPadding = ninePatchBorder.z;
            var bottomPadding = ninePatchBorder.w;

            var resultTex = new Texture2D((int)(leftPadding + rightPadding), (int)(topPadding + bottomPadding), TextureFormat.ARGB32, false);
            resultTex.anisoLevel = 0;
            //注意: Texture2D的(0, 0)为左下角
            resultTex.ReadPixels(new Rect(0, sprite.rect.height - bottomPadding, leftPadding, bottomPadding), 0, 0); //左下角
            resultTex.ReadPixels(new Rect(sprite.rect.width - rightPadding, sprite.rect.height - bottomPadding, rightPadding, bottomPadding), (int)leftPadding, 0); //右下角
            resultTex.ReadPixels(new Rect(0, 0, leftPadding, topPadding), 0, (int)bottomPadding); //左上角
            resultTex.ReadPixels(new Rect(sprite.rect.width - rightPadding, 0, rightPadding, topPadding), (int)leftPadding, (int)bottomPadding); //右上角
            resultTex.Apply();
            var bytes = resultTex.EncodeToPNG();

            Graphics.SetRenderTarget(null);
            RenderTexture.ReleaseTemporary(srcTempRT);

            var resultFilePath = path.Substring(0, path.Length - 4) + "_9.png";
            Debug.Log($"{resultFilePath}");
            File.WriteAllBytes(resultFilePath, bytes);

            AssetDatabase.ImportAsset(resultFilePath, ImportAssetOptions.ForceUpdate);
            //AssetDatabase.ImportAsset(resultFilePath, ImportAssetOptions.Default);
            //修改设置
            var texImporter = AssetImporter.GetAtPath(resultFilePath) as TextureImporter;
            if (texImporter)
            {
                texImporter.textureType = TextureImporterType.Sprite;
                texImporter.mipmapEnabled = false;
                texImporter.alphaIsTransparency = true;

                var setting = new TextureImporterSettings();
                texImporter.ReadTextureSettings(setting);
                setting.spriteGenerateFallbackPhysicsShape = false;
                setting.spriteBorder = ninePatchBorder; //自动处理好9宫区域
                texImporter.SetTextureSettings(setting);

                texImporter.SaveAndReimport();
                //EditorUtility.SetDirty(texImporter);
            }
        }
        //AssetDatabase.Refresh();
        Debug.Log($"生成成功");
    }
}

 

标签:sprite,ninePatchBorder,生成,bottomPadding,texImporter,var,宫图,rect
From: https://www.cnblogs.com/sailJs/p/16836024.html

相关文章