Answers for "sample color from pixel on screen unity"

C#
0

sample color from pixel on screen unity

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;
        }
    }
}
Posted by: Guest on August-19-2021

Code answers related to "sample color from pixel on screen unity"

C# Answers by Framework

Browse Popular Code Answers by Language