- Home /
Return the position(x,y) of the pixel when Clicked a Sprite
Hi Guys, i have a Question i hope you could help me. This is my problem, when i click an Sprite2D the mouse gives me WOLRD Coordinates, but i want the Sprite Coordinates. Example: Y have an 50x50 Sprite and i click on the middle of it. I want a return that indicates me the coordinate of the sprite, that will be 24,24. I Hope you understand.
Thnks Guys for your time. :)
Answer by robertbu · Oct 24, 2014 at 04:26 AM
If you are willing to:
Supply the pixel width and pixel height to the script
Supply the units to pixels value in the spript
Use an Orthographic camera
Set the Pivot of the sprite to the Bottom left
Single texture (not an atlas)
Then you can do something simple like this:
#pragma strict
var pixelWidth = 76.0;
var pixelHeight = 150.0;
var unitsToPixels = 100.0;
function OnMouseDown() {
// assumes an orthographic camera
var pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
pos.z = transform.position.z;
pos = transform.InverseTransformPoint(pos);
var xPixel : int = Mathf.RoundToInt(pos.x * unitsToPixels);
var yPixel : int = Mathf.RoundToInt(pos.y * unitsToPixels);
Debug.Log("("+xPixel+", "+yPixel+")");
}
The more you stray from the above list of criteria (or want to automate so that you don't have to input the date) the more complicated the script will be to calculate the position. Note that using a Quad with a MeshCollider makes this functionality trivial.
Im Using a Sprite, and i Just need pixel color when i click in the Sprite... A Var Color32 and i need to be acurrate actually i can take the pixel color but when i Build the project for Windows, because the resolution change the script doesnt work fine. And when i used GetPixelColor() The method needs a position, but the Input$$anonymous$$ouse gives me the world coordinates, and i need the sprite coordinates. (If i click the pixel in the middle it would be 24x,24y) And this wont work... But thnks!!
i need the sprite coordinates. (If i click the pixel in the middle it would be 24x,24y) And this wont work.
This code, properly setup would give you (24,24) if you click on the sprite. $$anonymous$$aking the conversion from world to pixels for a sprite is not an easy thing to do. Here is the above code taken a bit further. It assumes/requires the following:
Orthographic camera
No rotation of the sprite at the start of the game
Single texture sprite (won't work for an atlas or if the sprite is otherwise mapped into a texture.
The texture must be read/write enabled.
The pivot of the sprite needs to be the bottom left. Other pivots could be supported but would require code changes.
private var pixelWidth : int; private var pixelHeight : int; private var unitsToPixels : float; private var tex : Texture2D;
function Start() { var sr : SpriteRenderer = GetComponent.(); if (sr == null || sr.sprite.packed || !Camera.main.orthographic) { Debug.Log("Setup not correct to use this code"); return; }
pixelWidth = sr.sprite.rect.width; pixelHeight = sr.sprite.rect.height; unitsToPixels = pixelWidth / sr.bounds.size.x * transform.localScale.x; tex = sr.sprite.texture; } function On$$anonymous$$ouseDown() { // assumes an orthographic camera var pos = Camera.main.ScreenToWorldPoint(Input.mousePosition); pos.z = transform.position.z; pos = transform.InverseTransformPoint(pos); var xPixel : int = $$anonymous$$athf.RoundToInt(pos.x * unitsToPixels); var yPixel : int = $$anonymous$$athf.RoundToInt(pos.y * unitsToPixels); var color32 : Color32 = tex.GetPixel(xPixel, yPixel); Debug.Log("("+xPixel+", "+yPixel+") " + color32); }
@robertbu Can you please modify the above code for sprite that has pivot set to its Center rather than Bottom Left?
thanks in advance
Your answer
Follow this Question
Related Questions
Internal coordinates for each object 2 Answers
Add borders to landscape 0 Answers
My ingame avatar's worldspace does not coincide with other objects 1 Answer
Lost in translation... and by that I mean coordinate systems 1 Answer
Is there any way to set a Gameobject's transform matrix to be the World Space? 0 Answers