![]()
 
#if UNITY_EDITOR
using System;
using System.IO;
using UnityEditor;
using UnityEngine;
public static class SpriteTool
{
    //==================== OpenDirAfterExport
    const string PrefsKey_OpenDirAfterExport = "ExportSelectSpriteTool.OpenDirAfterExport";
    const string MenuItemPath_OpenDirAfterExport = "Assets/Image/Open Dir After Export";
    private static int s_OpenSpriteExportDirFlag = -1; //-1_未初始化, 0_false, 1_true
    public static bool IsOpenDirAfterExport
    {
        get
        {
            if (-1 == s_OpenSpriteExportDirFlag)
            {
                s_OpenSpriteExportDirFlag = 1;
                s_OpenSpriteExportDirFlag = EditorPrefs.GetInt(PrefsKey_OpenDirAfterExport, 1); //默认开启
            }
            return 1 == s_OpenSpriteExportDirFlag;
        }
        set
        {
            var newValue = value ? 1 : 0;
            if (s_OpenSpriteExportDirFlag != newValue)
            {
                s_OpenSpriteExportDirFlag = newValue;
                EditorPrefs.SetInt(PrefsKey_OpenDirAfterExport, s_OpenSpriteExportDirFlag);
            }
        }
    }
    [MenuItem(MenuItemPath_OpenDirAfterExport, true)]
    static bool MenuItemInit_IsOpenDirAfterExport()
    {
        Menu.SetChecked(MenuItemPath_OpenDirAfterExport, IsOpenDirAfterExport);
        return true;
    }
    [MenuItem(MenuItemPath_OpenDirAfterExport, false)]
    static void MenuItem_IsOpenDirAfterExport()
    {
        IsOpenDirAfterExport = !Menu.GetChecked(MenuItemPath_OpenDirAfterExport);
    }
    //====================
    //==================== ExportSelectSprite
    const string Output_Dir_Path = "_Temp/ExportSprite/";
    const string MenuItemPath_ExportSelectSprite = "Assets/Image/Export Select Sprite";
    [MenuItem(MenuItemPath_ExportSelectSprite, true)]
    static bool MenuItemValidator_ExportSelectSprite()
    {
        if (Application.isPlaying)
            return false;
        if (null == Selection.activeObject)
            return false;
        var sp = Selection.activeObject as Sprite;
        return null != sp;
    }
    [MenuItem(MenuItemPath_ExportSelectSprite)]
    static void MenuItem_ExportSelectSprite()
    {
        var sp = Selection.activeObject as Sprite;
        if (null == sp) return;
        if (!Directory.Exists(Output_Dir_Path) && null == Directory.CreateDirectory(Output_Dir_Path))
        {
            Debug.LogError($"{Output_Dir_Path} create fail!");
            return;
        }
        var srcTex = sp.texture;
        var srcTempRT = RenderTexture.GetTemporary(srcTex.width, srcTex.height, 0, RenderTextureFormat.Default);
        Graphics.Blit(srcTex, srcTempRT);
        var oldActiveRT = RenderTexture.active;
        RenderTexture.active = srcTempRT;
        OutputSprite(srcTempRT, sp);
        RenderTexture.active = oldActiveRT;
        RenderTexture.ReleaseTemporary(srcTempRT);
        Debug.Log($"finish: {Output_Dir_Path}");
    }
    static void OutputSprite(RenderTexture srcTempRT, Sprite sp)
    {
        Rect rect = sp.rect; //该Rect的width, height为裁掉空白前的宽高
        Vector2 offset = sp.textureRectOffset;
        Rect texRect = sp.textureRect; //该Rect的width, height为裁掉空白后的宽高
        var spriteTex = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGBA32, false);
        var readRect = new Rect(texRect.x,
        srcTempRT.height - (texRect.y + texRect.height), //RenderTexture-(0, 0)为左上角, 要转换成左下角(0, 0)
        texRect.width, texRect.height);
        spriteTex.ReadPixels(readRect, (int)offset.x, (int)offset.y); //Texture2D-(0, 0)为左下角
        spriteTex.Apply();
        var pngBytes = spriteTex.EncodeToPNG();
        var spriteOutPath = Path.Combine(Output_Dir_Path, $"{sp.name}.png");
        File.WriteAllBytes(spriteOutPath, pngBytes);
        if (IsOpenDirAfterExport)
        {
            OpenDirAndSelectFile(Path.GetFullPath(spriteOutPath));
        }
    }
    //====================
    private static void OpenDirAndSelectFile(string fileFullPath)
    {
        var process = new System.Diagnostics.Process();
        var psi = new System.Diagnostics.ProcessStartInfo("Explorer.exe");
        psi.Arguments = $"/Select,{fileFullPath}";
        process.StartInfo = psi;
        try
        {
            process.Start();
        }
        catch (Exception ex)
        {
            Debug.LogError($"{ex.StackTrace}");
        }
        finally
        {
            process.Close();
        }
    }
}
#endif