1

I'm trying to get an average color from a specific 4x4 area in a video texture on every update. It can either be black or white, but because I'm streaming the video and the resolution differs from time to time it might also be grayish. I need to find out more precisely if it's "almost black" or more on the white/light gray side of the spectrum.

I'm very new to c# and unity. I found Texture2D.GetPixel or Texture2D.GetPixels32 might be better, but I'm really not sure how I can do that from a video texture.

Thanks to anyone who can help.

KeepCool
  • 497
  • 1
  • 6
  • 24

1 Answers1

1

Take a look at this post. It looks like you are doing the-same thing as that person which is to get Texture from an image and average the pixel.

The reason I don't consider this a duplicate is because you want to get a pixels from specific(4x4) area not all pixels from the texture.

Texture2D.GetPixel or Texture2D.GetPixels32 might be better,

Because you want to do this on speficic pixels, you can't do it with Texture2D.GetPixel or Texture2D.GetPixels32. You don't even need the UniversalMediaPlayer plugin you are using.

You can do this with the GetPixels function. Notice the 's' at the end.

This is the function prototype:

Color[] GetPixels(int x, int y, int blockWidth, int blockHeight);

Depending on the position of the pixels you want to read from the Texture it should look something like this:

Color[] texColors = tex.GetPixels(0, 0, 4, 4);

You need to use that instead of Color32[] texColors = tex.GetPixels32(); from this answer.

Note:

You can still use your UniversalMediaPlayer video plugin if you prefer it over Unity's new VideoPlayer. The GetPixels solution should still work.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • 1
    Thank you so much Programmer! It's so nice to have people like you help out beginners like myself and not treat us like idiots for not learning the language inside out first. I wish there was a way to repay you more than just a "checkmark". – KeepCool Mar 22 '17 at 19:53
  • 1
    Also, thank you for explaining the code really well. – KeepCool Mar 22 '17 at 19:55
  • You are welcome. Don't forget to add code(if necessary) to your future questions so that your question wont be down-voted. – Programmer Mar 22 '17 at 20:25