tex | 要编码的纹理。 |
以 TGA 格式编码指定的纹理。
此函数返回一个字节数组,该数组是 TGA 文件数据。您可以将编码数据存储为文件或在无需进一步处理的情况下将其发送到网络。
此函数不适用于任何压缩格式。 Texture.isReadable 必须为 true
。编码的 TGA 数据将是未压缩的 8 位灰度、RGB 或 RGBA(取决于传入的格式)。对于单通道红色纹理(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 = ImageConversion.EncodeToTGA(tex); Object.Destroy(tex);
// Write the returned byte array to a file in the project folder File.WriteAllBytes(Application.dataPath + "/../SavedScreen.tga", bytes); } }