1
\$\begingroup\$

I have a 2 layers of a different sprites. How can i get a pixel color from lower sprite?

I know how to get touch and world coordinates, but i have no idea what to do next.

Touch touch0 = Input.GetTouch(0); // screen touch coordinates
Vector2 playerPick = GetComponent<Camera>().ScreenToWorldPoint(touch0.position); // world coordinate
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

I sample image data for my color picker utility. Raycasting will effectively return all objects that were clicked on, regardless if they appear underneath another one.

enter image description here

So first, you need to extract the image color data.

Color[] Data;
SpriteRenderer SpriteRenderer;

public int Width { get { return SpriteRenderer.sprite.texture.width; } }
public int Height { get { return SpriteRenderer.sprite.texture.height; } 

void Awake()
{
    // Get renderer you want to probe
    SpriteRenderer = GetComponent<SpriteRenderer> ();       

    // extract color data
    Data = SpriteRenderer.sprite.texture.GetPixels();
}

Then on Touch or MouseDown, you would determine if the click is inside of your image (via a collider and raycast) and then determine what pixel you clicked on inside of your image:

if (Input.GetMouseButton (0)) {

    // Get Mouse position - convert to world position
    Vector3 screenPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);    
    screenPos = new Vector2(screenPos.x, screenPos.y);

    // Check if we clicked on our object
    RaycastHit2D[] ray = Physics2D.RaycastAll(screenPos, Vector2.zero, 0.01f);
    for (int i = 0; i < ray.Length; i++)
    {
        // You will want to tag the image you want to lookup
        if (ray[i].collider.tag == "TAGNAME")
        { 
            // Set click position to the gameobject area
            screenPos -= ray[i].collider.gameObject.transform.position;
            int x = (int)(screenPos.x * Width);
            int y = (int)(screenPos.y * Height) + Height;

            // Get color data
            if (x > 0 && x < Width && y > 0 && y < Height)
            {
                Color = Data[y * Width + x];
            }                   
            break;
        }
    }
}

My picker is opened sourced, so if you want to further investigate this, check out https://github.com/jongallant/UnityColorPicker

\$\endgroup\$
14
  • \$\begingroup\$ Is there any way to get pixel color without any second-sided extension? May be i don't understand something. I do have to use it, right? \$\endgroup\$
    – What Ever
    Feb 23, 2016 at 18:25
  • \$\begingroup\$ No, god no. You can implement this yourself. The code above is all you need. I can try to better explain in a little bit. \$\endgroup\$
    – jgallant
    Feb 23, 2016 at 18:26
  • \$\begingroup\$ I don't know why, but this function does not came in to 'for' cycle. \$\endgroup\$
    – What Ever
    Feb 23, 2016 at 18:35
  • \$\begingroup\$ Do you have any idea what exactly may be wrong? I have camera, sprite and text on my scene, but RaycastHit2D length is 0. \$\endgroup\$
    – What Ever
    Feb 23, 2016 at 18:58
  • 1
    \$\begingroup\$ This is just a test setup. I have camera with attachet script (this script control camera zoom/moving etc). Sprite is right under the camera. i.imgur.com/uXVxlnh.jpg I understand that this is a very bad, but this is my first time using unity and i have to make what i am doing rly fast. I will do everything correct later on, after showing this result. I can't come to chat since my reputation is 1. \$\endgroup\$
    – What Ever
    Feb 23, 2016 at 19:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .