- Home /
Setting pixels on a texture?
I would like to draw a small crater texture on a plane in the spot that an object explodes. I know there are GetPixels() and SetPixels() functions for Texture2D, but what if I am using just a plain Texture? Also what is the best file format for doing stuff like this if I want to "blend" my crater texture onto the existing one. The main thing I need help with is the Texture vs Texture2D part of it. Any help would be appreciated.
Don't use 4 spaces if you don't want you whole question to be one long line formatted as code :) I've fixed it for you this time.
its simply a Texture. If I call GetPixels() it throws an error saying that that can't be done with a texture. And I don't see what the difference is between these two texture types.
What is it - in your project, something you loaded? Can you not cast it to a Texture2D?
Answer by Julien-Lynge · Jun 25, 2012 at 09:54 PM
According to the comments, your image is a jpg (that I'm guessing you imported to Unity and assigned to a public variable).
You have your variable defined as a Texture, but Texture is just a wrapper/base class for all the kinds of textures that Unity supports. Your imported JPG is always a Texture2D, even if you've assigned it to a Texture variable. You'll need to cast to Texture2D (or just define your variable as such in the first place), and then you'll have access to the get and set pixel functions.
BTW, if you want to understand what the various types of textures in Unity are, take a look at:
Texture2D
RenderTexture
MovieTexture
WebcamTexture
Before you actually do anything with a Texture (that's not common to all texture types, like getting the width and height), you have to cast it to one of these types.
Answer by whydoidoit · Jun 25, 2012 at 09:52 PM
File format doesn't really matter - that's what the file is on your computer - it is imported into a different format (much bigger) in your game.
You can just cast your texture to a Texture2D like this:
var myTexture : Texture2D = someTextureYouGot as Texture2D;
That will remove your downcast warning.
then use SetPixels32 and GetPixels32 if you can - it is massively faster.
Your answer
Follow this Question
Related Questions
Draw one texture on to another? Get Set Pixels. 0 Answers
Merging Textures At Runtime - Not Correct? 1 Answer
Resetting a Material's Texture After GetPixels/SetPixels 1 Answer
Setpixels leads to a blank texture? 1 Answer
Can't copy skybox faces to a cubemap sized texture without huge quality loss 0 Answers