Unity编辑器修改图片的大小

选中的原图要勾选红色框选项

编辑器窗口

public class TextureSizeEditorWindow : EditorWindow
{
    private Texture2D sourceTexture;//原图

    private Texture2D resultTexture;//修改的图

    private string changeWidth;//修改后的宽

    private string changeHeight;//修改后的高

    public string sourceFolerPath; //存储路径

    [MenuItem("Assets/TextureEditor")]
    static void SearchRefrence()
    { 
        TextureSizeEditorWindow window = (TextureSizeEditorWindow)EditorWindow.GetWindow(typeof(TextureSizeEditorWindow), false, "TextureEditor", true);
        window.Show();
    }


    private void OnGUI()
    {
        sourceTexture = EditorGUILayout.ObjectField("添加贴图", sourceTexture, typeof(Texture2D), true) as Texture2D;

        if (sourceTexture == null)
        {
            return;
        }


        EditorGUILayout.LabelField("width = " + sourceTexture.width.ToString(), GUILayout.Width(300));

        EditorGUILayout.LabelField("height = " + sourceTexture.height.ToString(), GUILayout.Width(300));


        changeWidth = EditorGUILayout.TextField("changeWidth = ", changeWidth);

        changeHeight = EditorGUILayout.TextField("changeHeight = ", changeHeight);

        string sourcePath = AssetDatabase.GetAssetPath(sourceTexture).Replace("Assets/", "");;

        string filePathName = sourcePath.Replace("/" + Path.GetFileName(sourcePath), "");

        sourceFolerPath = filePathName + "/Changes/";

        EditorGUILayout.LabelField("存储路径");
        EditorGUILayout.TextField(sourceFolerPath);

        if (GUILayout.Button("生成"))
        {
            SplitTexture();
        }

    }



    private void SplitTexture()
    {
        int resultWidth = 0;
        int resultHeight = 0;
        try
        {
            resultWidth = int.Parse(changeWidth);
            resultHeight = int.Parse(changeHeight);
        }
        catch
        {
            Debug.LogError("input format error. numbers need");
            return;
        }

        resultTexture = new Texture2D(resultWidth, resultHeight, TextureFormat.RGBA32, false);
        resultTexture.name = sourceTexture.name;

        int widthStart = (sourceTexture.width - resultWidth) / 2;
        int heightStart = (sourceTexture.height - resultHeight) / 2;

        Color outColor = new Color(0, 0, 0, 0);

        for (int wIndex = 0; wIndex < resultWidth; wIndex++)
        {
            for (int hIndex = 0; hIndex < resultHeight; hIndex++)
            {
                //超出部分透明填充
                if ((widthStart + wIndex) < 0 || (heightStart + hIndex) < 0 || (heightStart + hIndex )> sourceTexture.height || (widthStart + wIndex) > sourceTexture.width)
                {
                    resultTexture.SetPixel(wIndex, hIndex, outColor);
                }
                else
                {
                    resultTexture.SetPixel(wIndex, hIndex, sourceTexture.GetPixel(widthStart + wIndex, heightStart + hIndex));
                }
            }
        }

        SaveSplitTexture(resultTexture);
    }


    /// <summary>
    /// 保存切图文件
    /// </summary>
    /// <param name="codeText"></param>
    /// <param name="ext"></param>
    private void SaveSplitTexture(Texture2D codeText, string ext = ".png")
    {
        string floderPath = Application.dataPath + "/" + sourceFolerPath;

        if (Directory.Exists(floderPath) == false)//如果不存在就创建file文件夹
        {
            Directory.CreateDirectory(floderPath);
        }

        string foldeName = floderPath + codeText.name + ext;

        byte[] textuteByte = codeText.EncodeToPNG();

        if (File.Exists(foldeName))
        {
            @File.Delete(foldeName);
        }

        using (FileStream filestream = new FileStream(foldeName, FileMode.Create))
        {
            filestream.Write(textuteByte, 0, textuteByte.Length);
            filestream.Flush();
        }

        AssetDatabase.SaveAssets();

        AssetDatabase.Refresh();
    }
}


版权声明:本文为waterdsm原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。