x


How do I create a texture dynamically in Unity?

For example I want to create a 2 x 2 texture.

The upper left pixel would be White.
The upper right pixel would be Black.
The lower left pixel would be 50% transparent Black.
The lower right pixel would be 100% transparent.

Thanks,
Sammual

more ▼

asked Jan 09 '10 at 02:42 AM

Sammual12 gravatar image

Sammual12
63 2 3 8

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first
function Start () {
    // Create a new 2x2 texture ARGB32 (32 bit with alpha) and no mipmaps
    var texture = new Texture2D(2, 2, TextureFormat.ARGB32, false);

    // set the pixel values
    texture.SetPixel(0, 0, Color(1.0, 1.0, 1.0, 0.5));
    texture.SetPixel(1, 0, Color.clear);
    texture.SetPixel(0, 1, Color.white);
    texture.SetPixel(1, 1, Color.black);

    // Apply all SetPixel calls
    texture.Apply();

    // connect texture to material of GameObject this script is attached to
    renderer.material.mainTexture = texture;
}
more ▼

answered Jan 09 '10 at 02:55 AM

Jaap Kreijkamp gravatar image

Jaap Kreijkamp
6.4k 20 26 70

Other then changing texture.SetPixel(0, 0, Color(1.0, 1.0, 1.0, 0.5)); to texture.SetPixel(0, 0, Color(0, 0, 0, 0.5)); that was exactly what I needed.
Thank you!

Jan 09 '10 at 04:42 AM Sammual12
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2202
x300

asked: Jan 09 '10 at 02:42 AM

Seen: 11754 times

Last Updated: Jan 09 '10 at 02:51 PM