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.

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.

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.