unity 将.asset资源转成png

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


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


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


可以一次选中所有需要转换格式的图片,统一在Debug模式下把Is Readable属性勾选上。
接下来就是我们的小工具了,工具使用很简单,单个选中需要转换格式的图片或者选中图片所在的文件夹(会把文件夹下所有图片头转换格式),右键然后点击工具新增的菜单


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


最后就是工具的源代码了,内容很少,可以自行修改右键菜单里显示的名字以及文件导出目录
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<Texture2D>(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


原文:https://blog.csdn.net/u013746357/article/details/78723579 

猜你喜欢

转载自blog.csdn.net/u012909508/article/details/84344743