# SetPixels

> Sets the pixel colors of part of a mipmap level.

## Definition

* **Type:** Method
* **Namespace:** [UnityEngine](/engine/6000.5/script-reference/unityengine.md)
* **Assembly:** UnityEngine.CoreModule

## SetPixels(int, int, int, int, Color\[], int)

Sets the pixel colors of part of a mipmap level.

```csharp
public void SetPixels(int x, int y, int blockWidth, int blockHeight, Color[] colors, int miplevel)
```

### Parameters

**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): The x coordinate to place the block of pixels at. The range is `0` through (texture width - 1).**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): The y coordinate to place the block of pixels at. The range is `0` through (texture height - 1).**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): The width of the block of pixels to set.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): The height of the block of pixels to set.**** (\[Color\[]]\(/engine/6000.5/script-reference/unityengine/color)): The array of pixel colours to use. This is a 2D image flattened to a 1D array. Must be `blockWidth` x `blockHeight` in length.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): The mipmap level to write `colors` to. The range is `0` through the texture's [Texture.mipmapCount](/engine/6000.5/script-reference/unityengine/texture/mipmapcount.md). The default value is `0`.

### Remarks

This version of `SetPixels` sets part of a mipmap level instead of the whole mipmap level.

## SetPixels(Color\[], int)

Sets the pixel colors of an entire mipmap level.

```csharp
public void SetPixels(Color[] colors, int miplevel)
```

### Parameters

**** (\[Color\[]]\(/engine/6000.5/script-reference/unityengine/color)): The array of pixel colours to use. This is a 2D image flattened to a 1D array.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): The mipmap level to write `colors` to. The range is `0` through the texture's [Texture.mipmapCount](/engine/6000.5/script-reference/unityengine/texture/mipmapcount.md). The default value is `0`.

### Remarks

This method sets pixel data for the texture in CPU memory. [Texture.isReadable](/engine/6000.5/script-reference/unityengine/texture/isreadable.md) must be `true`, and you must call [Texture2D.Apply](/engine/6000.5/script-reference/unityengine/texture2d/apply.md) after `SetPixels` to upload the changed pixels to the GPU.

[Texture2D.Apply](/engine/6000.5/script-reference/unityengine/texture2d/apply.md) 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.

`colors` must contain the pixels row by row, starting at the bottom left of the texture. The size of the array must be the width × height of the mipmap level.

A single call to `SetPixels` is usually faster than multiple calls to [Texture2D.SetPixel](/engine/6000.5/script-reference/unityengine/texture2d/setpixel.md), especially for large textures.

`SetPixels` might be slower than some other texture methods because it converts the [Color](/engine/6000.5/script-reference/unityengine/color.md) struct into the format the texture uses. To set pixel data more quickly, use [Texture2D.SetPixelData](/engine/6000.5/script-reference/unityengine/texture2d/setpixeldata.md) instead.

You can use `SetPixels` with the following [texture formats](/engine/6000.5/script-reference/unityengine/textureformat.md):

* 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, `SetPixels` fails. Unity throws an exception when `SetPixels` fails.

Additional Resources: [Texture2D.GetPixels](/engine/6000.5/script-reference/unityengine/texture2d/getpixels.md), [Texture2D.SetPixels32](/engine/6000.5/script-reference/unityengine/texture2d/setpixels32.md), [Texture2D.SetPixelData](/engine/6000.5/script-reference/unityengine/texture2d/setpixeldata.md), [Texture2D.Apply](/engine/6000.5/script-reference/unityengine/texture2d/apply.md), [Texture2D.GetRawTextureData](/engine/6000.5/script-reference/unityengine/texture2d/getrawtexturedata.md), [Texture2D.LoadRawTextureData](/engine/6000.5/script-reference/unityengine/texture2d/loadrawtexturedata.md), [mipmapCount](/engine/6000.5/script-reference/unityengine/texture/mipmapcount.md).

### Examples

```csharp
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);
    }
}
```
