33 lines
1.1 KiB
C#
33 lines
1.1 KiB
C#
using System;
|
|
using System.IO;
|
|
using UnityEngine;
|
|
using Object = UnityEngine.Object;
|
|
|
|
public static class AssetExtractor
|
|
{
|
|
public static string ExtractTextureAtPath(Texture2D source, string filePath)
|
|
{
|
|
try
|
|
{
|
|
if (!File.Exists(filePath))
|
|
{
|
|
var renderTex = RenderTexture.GetTemporary(source.width, source.height);
|
|
Graphics.Blit(source, renderTex);
|
|
var texture = new Texture2D(renderTex.width, renderTex.height, TextureFormat.RGBA32, false);
|
|
RenderTexture.active = renderTex;
|
|
texture.ReadPixels(new Rect(0, 0, renderTex.width, renderTex.height), 0, 0);
|
|
RenderTexture.active = null;
|
|
RenderTexture.ReleaseTemporary(renderTex);
|
|
File.WriteAllBytes(filePath, texture.EncodeToPNG());
|
|
Object.DestroyImmediate(texture);
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogError(e.ToString());
|
|
filePath = string.Empty;
|
|
}
|
|
|
|
return filePath;
|
|
}
|
|
} |