-3

I'm working with google cardboard in unity. In my main scene I have a skybox with an image as texture. How can I get the color of the pixel I'm looking? The skybox is an element of mainCamera, that is child of "Head". I put also GvrReticle as child of head; is it useful for my purpose? Thanks

luke nnn
  • 5
  • 3

1 Answers1

0

Basically you wait for the end of the frame so that the camera has rendered. Then you read the rendered data into a texture and get the center pixel.

edit Be aware that if you have a UI element rendered in the center it will show the UI element color not the color behind.

private Texture2D tex;
public Color center;

void Awake()
{
    StartCoroutine(GetCenterPixel());
}


private void CreateTexture()
{
    tex = new Texture2D(1, 1, TextureFormat.RGB24, false);
}

private IEnumerator GetCenterPixel()
{
    CreateTexture();
    while (true)
    {
        yield return new WaitForEndOfFrame();
        tex.ReadPixels(new Rect(Screen.width / 2f, Screen.height / 2f, 1, 1), 0, 0);
        tex.Apply();
        center = tex.GetPixel(0,0);
    }
}
Chad Wentz
  • 16
  • 1
  • Ok, thanks. It worked. But now I have another question: How can I get the center pixel of the skybox texture of another Camera (not main) that is running but is not rendered on screen? This camera is "sister" of main camera. I think I should start from the rotation of "Head" but I don't know how – luke nnn Dec 05 '16 at 11:16