Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by NivekJump · Oct 23, 2014 at 11:10 PM · coordinatescoordinate-system

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. :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NivekJump · Oct 24, 2014 at 02:15 PM 0
Share

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!!

avatar image robertbu · Oct 24, 2014 at 11:28 PM 0
Share

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);
     }
    
    
    
    
    
    
    
    
avatar image AhmadYusaf robertbu · Jul 14, 2021 at 11:41 AM 0
Share

@robertbu Can you please modify the above code for sprite that has pivot set to its Center rather than Bottom Left?

thanks in advance

avatar image AhmadYusaf · Jun 05, 2021 at 10:43 AM 0
Share

This was helpful . Thank you very much. this works

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges