SetPixels
Sets the pixel colors of part of a mipmap level.
Read time 3 minutesLast updated 5 days ago
Definition
- Type: Method
- Namespace: UnityEngine
- Assembly: UnityEngine.CoreModule
SetPixels(int, int, int, int, Color[], int)
Sets the pixel colors of part of a mipmap level.
public void SetPixels(int x, int y, int blockWidth, int blockHeight, Color[] colors, int miplevel)
Parameters
The x coordinate to place the block of pixels at. The range is through (texture width - 1).
0The y coordinate to place the block of pixels at. The range is through (texture height - 1).
0The width of the block of pixels to set.
The height of the block of pixels to set.
The array of pixel colours to use. This is a 2D image flattened to a 1D array. Must be x in length.
blockWidthblockHeightThe mipmap level to write to. The range is through the texture's Texture.mipmapCount. The default value is .
colors00Remarks
This version of sets part of a mipmap level instead of the whole mipmap level.
SetPixelsSetPixels(Color[], int)
Sets the pixel colors of an entire mipmap level.
public void SetPixels(Color[] colors, int miplevel)
Parameters
The array of pixel colours to use. This is a 2D image flattened to a 1D array.
The mipmap level to write to. The range is through the texture's Texture.mipmapCount. The default value is .
colors00Remarks
This method sets pixel data for the texture in CPU memory. Texture.isReadable must be , and you must call Texture2D.Apply after to upload the changed pixels to the GPU.
trueSetPixelsTexture2D.Apply is an expensive operation because it copies all the pixels in the texture even if you've only changed some of the pixels, so change as many pixels as possible before you call it.
colorsA single call to is usually faster than multiple calls to Texture2D.SetPixel, especially for large textures.
SetPixelsSetPixelsYou can use with the following texture formats:
SetPixels- Alpha8
- ARGB32
- ARGB4444
- BGRA32
- R16
- R16_SIGNED
- R8
- R8_SIGNED
- RFloat
- RG16
- RG16_SIGNED
- RG32
- RG32_SIGNED
- RGB24
- RGB24_SIGNED
- RGB48
- RGB48_SIGNED
- RGB565
- RGB9e5Float
- RGBA32
- RGBA32_SIGNED
- RGBA4444
- RGBA64
- RGBA64_SIGNED
- RGBAFloat
- RGBAHalf
- RGFloat
- RGHalf
- RHalf
For all other formats, fails. Unity throws an exception when fails.
SetPixelsSetPixelsAdditional Resources: Texture2D.GetPixels, Texture2D.SetPixels32, Texture2D.SetPixelData, Texture2D.Apply, Texture2D.GetRawTextureData, Texture2D.LoadRawTextureData, mipmapCount.
Examples
using UnityEngine;using System.Collections;public class ExampleClass : MonoBehaviour{ void Start() { Renderer rend = GetComponent<Renderer>(); // Duplicate the original texture and assign to the material Texture2D texture = Instantiate(rend.material.mainTexture) as Texture2D; rend.material.mainTexture = texture; // Create the colors to use Color[] colors = new Color[3]; colors[0] = Color.red; colors[1] = Color.green; colors[2] = Color.blue; int mipCount = Mathf.Min(3, texture.mipmapCount); // For each mipmap level, use GetPixels to fetch an array of pixel data, and use SetPixels to fill the mipmap level with one color. for (int mip = 0; mip < mipCount; ++mip) { Color[] cols = texture.GetPixels(mip); for (int i = 0; i < cols.Length; ++i) { cols[i] = Color.Lerp(cols[i], colors[mip], 0.33f); } texture.SetPixels(cols, mip); } // Copy the changes to the GPU. and don't recalculate mipmap levels. texture.Apply(false); }}