Texture2D
The Texture2D class in Unity is like your canvas for drawing in the digital realm. A powerhouse for handling 2D textures and images, it is used for constructing and customizing visual elements in your game world, one pixel at a time. From generating a fresh texture dynamically, tailoring pre-existing textures, to applying those as the backdrop or developing intricate sprite animations, the Texture2D class gives you the reins.
Beyond its basic form, the Texture2D class dives deeper into the realm of advanced graphical rendering. Paired with Shader programming, it allows you to create diverse visual effects, playing with lighting and transforming visuals in complex ways.
To unravel the vast applications and techniques you can achieve with the Texture2D class and carve detailed, interactive, and visually appealing experiences in your games, head over to the in-depth guide in the official Unity Texture2D Documentation.
Properties
The ignoreMipmapLimit property is a Boolean that allows you to bypass mipmap limits when generating mipmaps. This is helpful when you are looking for a higher level of detail in your textures. For more info: Unity's Texture2D.ignoreMipmapLimit Documentation
The mipmapLimitGroup property is a string that is differential in controlling the mipmap display details, by categorizing the different mipmaps of a single texture into various groups. Each mipmap group can then be rendered separately, facilitating various visualization techniques. More details at Unity's Texture2D.mipmapLimitGroup Documentation
The activeMipmapLimit property allows you to determine the highest level of mipmaps that should be rendered for the texture. This is especially useful to optimize performance by controlling the level of texture detail, and balancing it against the processing power. For more about this, check Unity's Texture2D.activeMipmapLimit Documentation
The isReadable property is an indication of whether the pixel data of the texture can be read or written via script. This gives developers control over direct pixel manipulation which can be useful for custom graphics effects. Learn more from Unity's Texture2D.isReadable Documentation
The vtOnly property, when set to true, facilitates Virtual Texturing behavior for the Texture2D object. Virtual Texturing improves rendering performance by only loading visible textures, helpful for devices with limited graphic memory. Find out more in Unity's Texture2D.vtOnly Documentation
The streamingMipmaps property, when enabled, allows relative mipmap levels to be loaded asynchronously, optimizing the resources used for texture details. For more on this, see Unity's Texture2D.streamingMipmaps Documentation
The streamingMipmapsPriority property assigns a priority level to the Texture2D for mipmap streaming. Textures with higher priority will be streamed first. Read more at Unity's Texture2D.streamingMipmapsPriority Documentation
The requestedMipmapLevel property lets you request a specific level of detail for mipmaps to be loaded. This gives you control over the texture quality and performance trade-off. More can be found at Unity's Texture2D.requestedMipmapLevel Documentation
The minimumMipmapLevel property sets the lowest mipmap level that can be used. By controlling the mipmap levels, you can ensure the right balance between texture detail and memory usage. For more details, check out Unity's Texture2D.minimumMipmapLevel Documentation
The calculatedMipmapLevel property retrieves the currently calculated most suitable mipmap level based on texture usage. This can be convenient for optimizing texture detail in dynamic situations. Read more at Unity's Texture2D.calculatedMipmapLevel Documentation
The desiredMipmapLevel property returns the desired mipmap level calculated by Unity. This is determined based on texture usage and the system's conditions. More about this at Unity's Texture2D.desiredMipmapLevel Documentation
The loadingMipmapLevel property indicates the highest mipmap level currently being loaded. This can be useful for monitoring and managing texture detail level during runtime. Read more at Unity's Texture2D.loadingMipmapLevel Documentation
The loadedMipmapLevel property returns the highest fully loaded mipmap level. This can be leveraged to guarantee a certain level of texture quality by ensuring that high enough mipmap levels are fully loaded. Detailed info at Unity's Texture2D.loadedMipmapLevel Documentation
The whiteTexture property is a convenient way of getting a 2x2 white texture. This is commonly used for single color texturing or debugging. Official details at Unity's Texture2D.whiteTexture Documentation
The blackTexture property is a readily available 2x2 black texture. Similar to whiteTexture, this can be useful for single color texturing or debugging. Learn more at Unity's Texture2D.blackTexture Documentation
The redTexture property gives you a 2x2 red texture. This can be used for simple color assignments in texturing tasks or debugging. More at Unity's Texture2D.redTexture Documentation
The grayTexture property returns a 2x2 gray texture. This can come in handy for simple color texturing or debugging. Detailed information at Unity's Texture2D.grayTexture Documentation
The linearGrayTexture property provides a 2x2 gray texture with no gamma correction applied. This can be important for tasks requiring linear color space textures. To know more about this, refer to Unity's Texture2D.linearGrayTexture Documentation
The normalTexture property allows quick access to a 2x2 pixel texture where all pixels are set to 'RGB(0, 0.5, 0.5)', commonly used as a default for normal maps. To understand more about its use, check Unity's Texture2D.normalTexture Documentation
Methods
The Compress method compresses texture data in memory, decreasing the memory footprint at the expense of image quality. This can be significant to optimize game performance, especially on mobile platforms or devices with lower specs. Detailed info at Unity's Texture2D.Compress Documentation
Parameters
highQuality
Determines whether the compression process strives for higher quality or smaller footprint.
Returns
This method does not return a value.
The ClearRequestedMipmapLevel method discards all pending mipmap level requests for the texture. This may be important in dynamic scenes where mipmap level requirements can change rapidly. For more, visit Unity's Texture2D.ClearRequestedMipmapLevel Documentation
Returns
This method does not return a value.
The IsRequestedMipmapLevelLoaded method checks if the requested mipmap level has been loaded into memory. This is useful to ensure desired level of detail is ready before being applied. Detailed info at Unity's Texture2D.IsRequestedMipmapLevelLoaded Documentation
Returns
Returns true if the requested mipmap level has been loaded, false otherwise.
The ClearMinimumMipmapLevel method clears the minimum level of mipmaps that need to be loaded for the texture. This can be used along with related mipmap methods to fine-tune the balance between texture quality and memory usage. For more, see Unity's Texture2D.ClearMinimumMipmapLevel Documentation
Returns
This method does not return a value.
The SetPixel method lets you change a pixel's color at a specific position on the texture. This is a critical tool for runtime texture manipulation or applying custom drawn effects on textures. To understand more about it, see Unity's Texture2D.SetPixel Documentation
Parameters
x
The x-coordinate of the pixel.
y
The y-coordinate of the pixel.
color
ColorThe desired color for the pixel.
Returns
This method does not return a value.
The GetPixel method fetches the color of a pixel at a given position on the texture. This can be useful to create color-based logic or to read color information from textures. To learn more, refer to Unity's Texture2D.GetPixel Documentation
Parameters
x
The x-coordinate of the pixel.
y
The y-coordinate of the pixel.
Returns
Returns the color of the specified pixel.
The GetPixelBilinear method retrieves the color of a pixel using bilinear filtering. Bilinear filtering considers the four pixels surrounding specific coordinates and returns a blended color, producing a smoother result. More about this at Unity's Texture2D.GetPixelBilinear Documentation
Parameters
u
The u-coordinate (horizontal) of the pixel.
v
The v-coordinate (vertical) of the pixel.
Returns
Returns the color of the pixel at the specified coordinates, blended using bilinear filtering.
The Apply method updates the texture with any changes made since the last time Apply was called. If the texture is used in a material, it is necessary to call this method for the changes to be rendered on screen. For more details, visit Unity's Texture2D.Apply Documentation
Parameters
updateMipmaps
If true, the mipmap levels are regenerated automatically.
makeNoLongerReadable
If true, system memory copy of the texture is released after uploading it to the GPU.
Returns
This method does not return a value.
The Reinitialize method recreates the texture with a new size, preserving existing texture content. It is designed for dynamic operations like texture size changes in response to screen resizing. Learn more at Unity's Texture2D.Reinitialize Documentation
Parameters
width
The new width for the texture.
height
The new height for the texture.
Returns
Returns true if the re-initialization is successful.
The ReadPixels method reads pixel data from the framebuffer and stores it into a region of the Texture2D. This operation can be particularly helpful when implementing features like screenshots. Detailed info can be found at Unity's Texture2D.ReadPixels Documentation
Parameters
source
RectThe region of the screen from which pixels are read.
destX
The x-coordinate of the lower left corner of the region where pixels are written in the Texture2D.
destY
The y-coordinate of the lower left corner of the region where pixels are written in the Texture2D.
recalculateMipMaps
If true, mipmaps are automatically recalculated for this texture after reading pixels.
Returns
This method does not return a value.