Texture2D
Class that represents textures in C# code.
Read time 9 minutesLast updated 4 days ago
Definition
- Type: Class
- Namespace: UnityEngine
- Assembly: UnityEngine.CoreModule
- Inherits from: Texture
[HelpURL("texture-type-default")]public sealed class Texture2D : Texture
Remarks
Use this class to create textures, or to modify existing texture assets.
The ImageConversion class provides extension methods to this class that handle image encoding functionality. For details on those methods, see the ImageConversion documentation.
Do not assume that the texture will be created and available in Awake. All texture uploads are synchronized on the Main thread at Start. Perform texture operations in Start.
Constructors
Constructor | Description |
|---|---|
| Texture2D(System.Int32,System.Int32) | Create a new empty texture. |
| Texture2D(System.Int32,System.Int32,UnityEngine.TextureFormat,System.Boolean) | Create a new empty texture. |
| Texture2D(System.Int32,System.Int32,UnityEngine.TextureFormat,System.Boolean,System.Boolean) | Create a new empty texture. |
| Texture2D(System.Int32,System.Int32,UnityEngine.TextureFormat,System.Boolean,System.Boolean,System.Boolean) | Create a new empty texture. |
| Texture2D(System.Int32,System.Int32,UnityEngine.TextureFormat,System.Int32,System.Boolean) | Create a new empty texture. |
| Texture2D(System.Int32,System.Int32,UnityEngine.TextureFormat,System.Int32,System.Boolean,System.Boolean) | Create a new empty texture. |
| Texture2D(System.Int32,System.Int32,UnityEngine.TextureFormat,System.Int32,System.Boolean,System.Boolean,UnityEngine.MipmapLimitDescriptor) | Create a new empty texture. |
Properties
Property | Description |
|---|---|
| activeMipmapLimit | The number of high resolution mipmap levels from the texture that Unity doesn't upload to the GPU. (Read Only) |
| alphaIsTransparency | Indicates whether this texture was imported with TextureImporter.alphaIsTransparency enabled. This setting is available only in the Editor scripts. Note that changing this setting will have no effect; it must be enabled in TextureImporter instead. |
| calculatedMipmapLevel | The mipmap level calculated by the streaming system, which takes into account the streaming Cameras and the location of the objects containing this Texture. This is unaffected by Texture2D.requestedMipmapLevel or Texture2D.minimumMipmapLevel. |
| desiredMipmapLevel | The mipmap level that the streaming system would load before memory budgets are applied. |
| format | The format of the pixel data in the texture (Read Only). |
| ignoreMipmapLimit | This property causes a texture to ignore all texture mipmap limit settings. |
| loadedMipmapLevel | The mipmap level that is currently loaded by the streaming system. |
| loadingMipmapLevel | The mipmap level that the mipmap streaming system is in the process of loading. |
| minimumMipmapLevel | Restricts the mipmap streaming system to a minimum mip level for this Texture. |
| mipmapLimitGroup | The name of the texture mipmap limit group that this texture is associated with. (Read Only) |
| requestedMipmapLevel | The mipmap level to load. |
| streamingMipmaps | Determines whether mipmap streaming is enabled for this Texture. |
| streamingMipmapsPriority | Sets the relative priority for this Texture when reducing memory size to fit within the memory budget. |
| vtOnly | Returns true if the VTOnly checkbox was checked when the texture was imported; otherwise returns false. For additional information, see TextureImporter.vtOnly. |
Static Properties
Property | Description |
|---|---|
| blackTexture | Gets a small Texture with all black pixels. |
| grayTexture | Gets a small Texture with all gray pixels. |
| linearGrayTexture | Gets a small Texture with all gray pixels. |
| normalTexture | Gets a small Texture with pixels that represent surface normal vectors at a neutral position. |
| redTexture | Gets a small Texture with all red pixels. |
| whiteTexture | Gets a small Texture with all white pixels. |
Methods
Method | Description |
|---|---|
| Apply | Copies changes you've made in a CPU texture to the GPU. |
| ClearMinimumMipmapLevel | Resets the minimumMipmapLevel field. |
| ClearRequestedMipmapLevel | Resets the requestedMipmapLevel field. |
| Compress | Compress texture at runtime to DXT/BCn or ETC formats. |
| CopyPixels | Copies pixel data from another texture on the CPU. |
| GetPixel | Gets the pixel color at coordinates ( |
| GetPixelBilinear | Gets the filtered color at the normalized coordinates ( |
| GetPixelData | Gets the raw data from a texture. |
| GetPixels | Gets the pixel color data for a mipmap level as Color structs. |
| GetPixels32 | Gets the pixel color data for a mipmap level as Color32 structs. |
| GetRawTextureData | Gets the raw data from a texture, as a copy. |
| IsRequestedMipmapLevelLoaded | Checks to see whether the mipmap level set by requestedMipmapLevel has finished loading. |
| LoadRawTextureData | Sets the raw data of an entire texture in CPU memory. |
| PackTextures | Packs multiple Textures into a texture atlas. |
| ReadPixels | Reads pixels from the current render target and writes them to a texture. |
| Reinitialize | Reinitializes a Texture2D, making it possible for you to replace |
| SetPixel | Sets the pixel color at coordinates ( |
| SetPixelData | Sets the raw data of an entire mipmap level directly in CPU memory. |
| SetPixels | Sets the pixel colors of part of a mipmap level. |
| SetPixels32 | Sets the pixel colors of part of a mipmap level. |
| UpdateExternalTexture | Updates Unity texture to use different native texture object. |
Static Methods
Method | Description |
|---|---|
| CreateExternalTexture | Creates a Unity Texture out of an externally created native texture object. |
| GenerateAtlas | Packs a set of rectangles into a square atlas, with optional padding between rectangles. |
Enums
Enum | Description |
|---|---|
| Texture2D.EXRFlags | Flags used to control the encoding to an EXR file. |
Inheritance
Operators
Operator | Description |
|---|---|
| operator == | Compares two object references to see if they refer to the same object. |
| bool | Determines whether the object exists. |
| operator != | Compares if two objects refer to a different object. |