Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


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

Properties

Property

Description

activeMipmapLimitThe number of high resolution mipmap levels from the texture that Unity doesn't upload to the GPU. (Read Only)
alphaIsTransparencyIndicates 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.
calculatedMipmapLevelThe 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.
desiredMipmapLevelThe mipmap level that the streaming system would load before memory budgets are applied.
formatThe format of the pixel data in the texture (Read Only).
ignoreMipmapLimitThis property causes a texture to ignore all texture mipmap limit settings.
loadedMipmapLevelThe mipmap level that is currently loaded by the streaming system.
loadingMipmapLevelThe mipmap level that the mipmap streaming system is in the process of loading.
minimumMipmapLevelRestricts the mipmap streaming system to a minimum mip level for this Texture.
mipmapLimitGroupThe name of the texture mipmap limit group that this texture is associated with. (Read Only)
requestedMipmapLevelThe mipmap level to load.
streamingMipmapsDetermines whether mipmap streaming is enabled for this Texture.
streamingMipmapsPrioritySets the relative priority for this Texture when reducing memory size to fit within the memory budget.
vtOnlyReturns 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

blackTextureGets a small Texture with all black pixels.
grayTextureGets a small Texture with all gray pixels.
linearGrayTextureGets a small Texture with all gray pixels.
normalTextureGets a small Texture with pixels that represent surface normal vectors at a neutral position.
redTextureGets a small Texture with all red pixels.
whiteTextureGets a small Texture with all white pixels.

Methods

Method

Description

ApplyCopies changes you've made in a CPU texture to the GPU.
ClearMinimumMipmapLevelResets the minimumMipmapLevel field.
ClearRequestedMipmapLevelResets the requestedMipmapLevel field.
CompressCompress texture at runtime to DXT/BCn or ETC formats.
CopyPixelsCopies pixel data from another texture on the CPU.
GetPixelGets the pixel color at coordinates (
x
,
y
).
GetPixelBilinearGets the filtered color at the normalized coordinates (
u
,
v
).
GetPixelDataGets the raw data from a texture.
GetPixelsGets the pixel color data for a mipmap level as Color structs.
GetPixels32Gets the pixel color data for a mipmap level as Color32 structs.
GetRawTextureDataGets the raw data from a texture, as a copy.
IsRequestedMipmapLevelLoadedChecks to see whether the mipmap level set by requestedMipmapLevel has finished loading.
LoadRawTextureDataSets the raw data of an entire texture in CPU memory.
PackTexturesPacks multiple Textures into a texture atlas.
ReadPixelsReads pixels from the current render target and writes them to a texture.
ReinitializeReinitializes a Texture2D, making it possible for you to replace
width
,
height
,
textureformat
, and
graphicsformat
data for that texture.
SetPixelSets the pixel color at coordinates (
x
,
y
).
SetPixelDataSets the raw data of an entire mipmap level directly in CPU memory.
SetPixelsSets the pixel colors of part of a mipmap level.
SetPixels32Sets the pixel colors of part of a mipmap level.
UpdateExternalTextureUpdates Unity texture to use different native texture object.

Static Methods

Method

Description

CreateExternalTextureCreates a Unity Texture out of an externally created native texture object.
GenerateAtlasPacks a set of rectangles into a square atlas, with optional padding between rectangles.

Enums

Enum

Description

Texture2D.EXRFlagsFlags used to control the encoding to an EXR file.

Inheritance

Inherited Members

Operators

Operator

Description

operator ==Compares two object references to see if they refer to the same object.
boolDetermines whether the object exists.
operator !=Compares if two objects refer to a different object.