Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
1 capture
14 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
0
Question by Rapeseed_Oil · 2 days ago · 2d gameselectionpixelgetpixelselecting objects

How do I select a sprite's pixel by mouse click?

I want to select 2D sprites with the mouse. I use box colliders and Physics2D.OverlapPointAll(); to do this.

I also want to ignore clicks on the fully transparent parts of the sprite inside the box collider. I am trying to accomplish this by using Texture2D.GetPixelBilinear to identify transparent pixels (alpha = 0), but It's not working and I don't understand why. https://docs.unity3d.com/ScriptReference/Texture2D.GetPixelBilinear.html No matter where I click in any box collider it always returns a Color with an alpha of 0. I tried hardcoding the GetPixelBilinear() inputs to specific values but never got an alpha value that wasn't 0. My code is listed below in the GameManager. All sprites in testing have box colliders and are read/write enabled.

 void Update()
 {
     SpriteSelection();
 }

 void SpriteSelection()
 {
     // When mouse 0 is pressed
     if (Input.GetMouseButton(0))
     {
         Vector2 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         Collider2D[] collidersHit = Physics2D.OverlapPointAll(pos);
         GameObject selectedObject = null;

         for (int ii = 0; ii < collidersHit.Length; ii++)
         {
             Sprite sprite = collidersHit[ii].gameObject.GetComponent<SpriteRenderer>().sprite;
             Color pixel = sprite.texture.GetPixelBilinear(pos.x, pos.y);
             Debug.Log(pixel.ToString());
         }
     }
 }
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
0
Best Answer

Answer by Rapeseed_Oil · yesterday

Alright, came up with a solution that lets me select the colour of a pixel in any sprite with a collider no matter where it is in the world, no matter what its PixelsPerUnit is, and no matter the location of sprite on original texture (for textures that are sprite mode: multiple)

Posting it here just in case anyone else has use of it.

 void SpriteSelection()
 {
     // When mouse 0 is pressed
     if (Input.GetMouseButton(0))
     {
         Vector2 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         Collider2D[] collidersHit = Physics2D.OverlapPointAll(pos);
         GameObject selectedObject = null;

         for (int ii = 0; ii < collidersHit.Length; ii++)
         {
             GameObject gameObject = collidersHit[ii].gameObject;
             Sprite sprite = gameObject.GetComponent<SpriteRenderer>().sprite;
             Rect rect = sprite.textureRect;

             // calculate the distance of the mouse from the center of the sprite's transform
             float x = pos.x - gameObject.transform.position.x;
             float y = pos.y - gameObject.transform.position.y;

             // convert the x and y values from units to pixels
             x *= sprite.pixelsPerUnit;
             y *= sprite.pixelsPerUnit;

             // modify so pixel distance from bottom left corner instead of from center
             x += rect.width / 2;
             y += rect.height /2;

             // adjust for location of sprite on original texture
             x += rect.x;
             y += rect.y;

             // mouse position x and y subtract transform position x and y, then multiply by pixels per unit
             Color pixel = sprite.texture.GetPixel(Mathf.FloorToInt(x), Mathf.FloorToInt(y));
             Debug.Log(pixel.ToString());
         }
     }
 }
Comment
Add comment · 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

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

190 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Cannot set Selection.activeObject (no errors, no clues) 0 Answers

getPixel co-ordinates not screen co-ordinates based on mouse position 2 Answers

How to multi-select scene hierarchy objects in code 0 Answers

Should I change the Pixel To Unit ratio in a 2D Project? 0 Answers

Sprites blurring slighly when camera moving, even with Pixel Perfect 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