Параметры
tex | Текстура для кодирования. |
Описание
Кодирует указанную текстуру в формате TGA.
Для правильного выполнения этой функции Texture.isReadable должно быть true
.
Текстура в кодировке TGA возвращается в виде массива байтов. Вы можете сохранить закодированную текстуру в виде файла или отправить ее по сети без дальнейшей обработки.
Эта функция работает только с несжатыми форматами текстур без HDR.
Закодированные данные TGA не сжаты и содержат альфа-канал для текстур RGBA32
, ARGB32
и не содержат альфа-канал для Текстуры RGB24
. Для одноканальных красных текстур ( R8
, R16
, RFloat
и RHalf
), закодированные данные TGA будут в 8-битных оттенках серого.
// Saves screenshot as TGA file.
using UnityEngine;
using System.Collections;
using System.IO;
public class TGAScreenSaver : MonoBehaviour
{
// Take a shot immediately
IEnumerator Start()
{
yield return SaveScreenTGA();
}
IEnumerator SaveScreenTGA()
{
// Read the screen buffer after rendering is complete
yield return new WaitForEndOfFrame();
// Create a texture in RGB24 format the size of the screen
int width = Screen.width;
int height = Screen.height;
Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);
// Read the screen contents into the texture
tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
tex.Apply();
// Encode the texture in TGA format
byte[] bytes = tex.EncodeToTGA();
Object.Destroy(tex);
// Write the returned byte array to a file in the project folder
File.WriteAllBytes(Application.dataPath + "/../SavedScreen.tga", bytes);
}
}
Смотрите так же: Texture2D.ReadPixels, WaitForEndOfFrame, LoadImage, EncodeArrayToTGA, EncodeNativeArrayToTGA, EncodeToPNG, EncodeToJPG, EncodeToEXR.