栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > 其他

Unity编辑器修改图片的大小

其他 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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);
    }


    /// 
    /// 保存切图文件
    /// 
    /// 
    /// 
    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();
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/905688.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号