从assetbundle中获取的Texture2D 的 isReadable 属性是 false,无法直接读取,下面的方法可以将 isReadable 为false的Texture2D提取出来
using System.IO;
using UnityEditor;
using UnityEngine;
public class SaveUnReadableTexture
{
[MenuItem(("Tools/Save UnReadable Texture"))]
static void Save()
{
var texture = EditorGUIUtility.TrIconContent("d_AnimatorController Icon").image;
RenderTexture tmp = RenderTexture.GetTemporary(
texture.width,
texture.height,
0,
RenderTextureFormat.Default,
RenderTextureReadWrite.Linear);
Graphics.Blit(texture, tmp);
RenderTexture previous = RenderTexture.active;
RenderTexture.active = tmp;
Texture2D newTexture2D = new Texture2D(texture.width, texture.height);
newTexture2D.ReadPixels(new Rect(0, 0, tmp.width, tmp.height), 0, 0);
newTexture2D.Apply();
Debug.LogError(newTexture2D.isReadable);
RenderTexture.active = previous;
var imgSavePath = Path.Combine(Application.dataPath, texture.name + ".png");
var bytes = newTexture2D.EncodeToPNG();
var file = File.Open(imgSavePath, FileMode.CreateNew);
var binary = new BinaryWriter(file);
binary.Write(bytes);
file.Close();
}
}
提取如下:
相关应用:https://www.bzetu.com/371/.html
本文参考:https://support.unity.com/hc/en-us/articles/206486626-How-can-I-get-pixels-from-unreadable-textures-
文章评论