Unity编辑器扩展:如何把asset 格式的资源转成png

Assets Store 上提供了大量的优秀素材给开发者使用,包括模型贴图UI音乐等等。但是一些上传者可能出于特殊原因,上传的素材格式是unity内置的.asset 格式,这样的格式对于直接拿来在unity中使用的开发者当然没有影响,但是遇到素材本身不是完全符合需求,需要做一些修改的情况,特殊的.asset 格式的文件就阻止了我们把这些素材拿到其他第三方工具(PS ,3dMax)修改的可能。所以本篇的小工具就应运而生了。
虽然本篇仅仅讲了如何转换图片资源,但是模型资源等同样适用于这里使用的方法,只是需要自己写或者去网络上搜索如何将unity内模型保存成obj格式等。

首先是我们从AssetStore下载的asset格式的素材,可以看到虽然unity能把这些素材解析成Texture,但是不同于普通贴图,这里我们不能修改贴图的各项属性。

普通图片
Unity编辑器扩展:如何把asset 格式的资源转成png_第1张图片
asset 格式的图片
Unity编辑器扩展:如何把asset 格式的资源转成png_第2张图片

既然要转化格式,自然是要读这个贴图的数据,但是我们发现,asset格式的资源没有Read/Write Enable的勾选框,不用急,我们可以修改该资源到Debug模式下看看

Unity编辑器扩展:如何把asset 格式的资源转成png_第3张图片

然后再来观察Inspector面板,发现有一个Is Readable的勾选

Unity编辑器扩展:如何把asset 格式的资源转成png_第4张图片

可以一次选中所有需要转换格式的图片,统一在Debug模式下把Is Readable属性勾选上。

接下来就是我们的小工具了,工具使用很简单,单个选中需要转换格式的图片或者选中图片所在的文件夹(会把文件夹下所有图片头转换格式),右键然后点击工具新增的菜单

Unity编辑器扩展:如何把asset 格式的资源转成png_第5张图片

然后就可以看到一张张图片都生成在了同级目录的exported目录下(需要刷新下所在文件夹,unity不会立马加载进来)

Unity编辑器扩展:如何把asset 格式的资源转成png_第6张图片

最后就是工具的源代码了,内容很少,可以自行修改右键菜单里显示的名字以及文件导出目录

using UnityEngine;
using System.Collections;
using System.IO;
using System;
#if UNITY_EDITOR
using UnityEditor;

[ExecuteInEditMode]
public class ExportPngFile : MonoBehaviour
{
    [MenuItem("Assets/ExportIamgeTo_exported")]//右键菜单的名称
    public static void ExportImageFile()
    {
        try
        {
            string assetpath = AssetDatabase.GetAssetPath(Selection.activeObject);
            //Debug.Log(assetpath);
            if (Path.GetExtension(assetpath) != "")
            {
                if (Path.GetExtension(assetpath) == ".asset")
                {
                    ExportPng(assetpath);
                }
            }
            else
            {
                foreach (string path in Directory.GetFiles(assetpath))
                {
                    if (Path.GetExtension(path) == ".asset")
                    {
                        ExportPng(path);
                    }
                }
            }
        }
        catch (Exception e)
        {
            Debug.LogError(e.Message + e.StackTrace);
        }

    }

    private static void ExportPng(string srcPath)
    {
        //Debug.Log(srcPath);
        Texture2D assets = AssetDatabase.LoadAssetAtPath(srcPath.Substring(srcPath.IndexOf("Assets")));
        if (assets != null)
        //for (int i = 0; i < assets.Length; i++)
        {

            Texture2D texture = assets;
            string assetPath = AssetDatabase.GetAssetPath(texture);
            //Debug.Log(assetPath);
            var tImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter;
            if (tImporter != null)
            {
                tImporter.textureType = TextureImporterType.Default;

                tImporter.isReadable = true;

                AssetDatabase.ImportAsset(assetPath);
                tImporter.SaveAndReimport();
                AssetDatabase.Refresh();
            }

            Texture2D outputtex = new Texture2D(texture.width, texture.height, TextureFormat.ARGB32, false);

            for (int y = 0; y < outputtex.height; ++y)
            { // each row
                for (int x = 0; x < outputtex.width; ++x)
                {
                    Color color;
                    {
                        color = texture.GetPixel(x, y);
                    }
                    //color.a = alpha;
                    outputtex.SetPixel(x, y, color);
                }
            }
            byte[] pngShot = outputtex.EncodeToPNG();
            string parentPath = Directory.GetParent(srcPath).FullName;

            string exportedPath = parentPath + "/exported/"; //生成的图片的位置
            //Debug.Log(exportedPath);
            if (!Directory.Exists(exportedPath))
            {
                Directory.CreateDirectory(exportedPath);
            }

            string fileName = exportedPath + "/" + texture.name + ".png";
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }

            File.WriteAllBytes(fileName, pngShot);
            System.GC.Collect();
        }
    }

    private Texture2D GetTex2D()
    {
        // Create a texture the size of the screen, RGB24 format
        int width = Screen.width;
        int height = Screen.height;
        Texture2D tex = new Texture2D(width, height, TextureFormat.ARGB32, false);
        // Read screen contents into the texture
        tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
        tex.Apply();
        return tex;
    }
    void Start(){ }
}

#endif

你可能感兴趣的:(unity,工具)