Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 TMK · May 23, 2010 at 02:46 AM · 2dpixelspritemanager2

Detecting mouseclick on pixels in sprite (using SM2) / pixel collisions

Hi,

Sorry if this has been answered before, but I couldn't find a good solution to it yet.

I'm using C# and SpriteManager 2, and I'm trying to detect if a user clicks on a sprite, and I need to check if one clicks on the pixels themselves, and not just a box collider around the sprite, so basically pixel perfect detection.

I'm currently trying to do something like this, but I just haven't been able to get it 100% working yet, or if I'm doing it correctly:

Vector3 pos = Input.mousePosition - Camera.main.WorldToScreenPoint(transform.position);

     Sprite sprite = (Sprite)GetComponent("Sprite");

     // check if alpha value is not 0.0, meaning it has a pixel here
     if (((Texture2D)renderer.material.mainTexture).GetPixel((int)(sprite.lowerLeftPixel.x + pos.x), (int)(sprite.lowerLeftPixel.y + pos.y)).a > 0.1f)
     {
         sprite.SetColor(Color.black);
     }
     else
     {
         sprite.SetColor(Color.white);
     }

Should this work or is there a better way to do this?

Thanks! :)

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

2 Replies

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

Answer by qJake · May 23, 2010 at 05:49 AM

See this post, which details what you'll need to use:

http://answers.unity3d.com/questions/9664/reading-a-color-from-an-image

Specifically, this forum post:

http://forum.unity3d.com/viewtopic.php?t=8452&start=3

contains code that raycasts at an object and gets the color of whatever pixel you're pointing at, and then acts accordingly. You could just check the alpha value of the pixel that you're aiming at, and then go from there.

Here are the two functions you'll find useful as well:

http://unity3d.com/support/documentation/ScriptReference/Texture2D.GetPixel.html

http://unity3d.com/support/documentation/ScriptReference/Texture2D.GetPixelBilinear.html

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
avatar image
1

Answer by TMK · May 24, 2010 at 03:57 AM

Thanks SpikeX! :)

For anyone that's interested, I managed to get it working with SpriteManager 2 by using the following code. It might depend on some particular settings in my project but hopefully someone finds it useful:

public float TheScaleDown; public float TheScaleUp;

void Start() { // Set the scale of the game, the default resolution of your game, so that calculated the mousepositions correctly out of the world positions (when grabbing the pixel from the texture) TheScaleDown = (float)Screen.width / 1024.0f; TheScaleUp = 1024.0f / (float)Screen.width; }

void Update() { Sprite sprite = (Sprite)GetComponent("Sprite");

 // Check the rect around the sprite, scaled to fit the resolution size, so we can see if the mouse is within it
 Rect rect = new Rect(Camera.main.WorldToScreenPoint(transform.position).x, Camera.main.WorldToScreenPoint(transform.position).y - (sprite.height * TheScaleDown), sprite.width * TheScaleDown, sprite.height * TheScaleDown);

 // Is the mouse within the sprite rect?
 if(rect.Contains(Input.mousePosition))
 {
     // Yes, check if we are holding the mouse over a pixel
     Vector3 checkPos = new Vector3(sprite.lowerLeftPixel.x + ((Input.mousePosition.x - rect.x) * TheScaleUp), (renderer.material.mainTexture.height - 1) - sprite.lowerLeftPixel.y - ((rect.y - Input.mousePosition.y) * TheScaleUp));

     Color pixel = ((Texture2D)renderer.material.mainTexture).GetPixel((int)checkPos.x, (int)checkPos.y);

     // Is there a non-transparent pixel here?
     if (pixel.a >= 0.1f)
     {
         // Yes! Draw our sprite as black, just as an indication that it worked
         sprite.SetColor(Color.black);
     }
     else
     {
         // No, draw it normal
         sprite.SetColor(Color.white);
     }
 }
 else
 {
     // No, draw it normal
     sprite.SetColor(Color.white);
 }

}

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

No one has followed this question yet.

Related Questions

How to create 2D pixel water shader ? 0 Answers

2D Animation does not start 1 Answer

How to make shadows in Top Down 2D game 1 Answer

Can I get perfect 2d tile alignment using grid snap AND vertex snap on 1x1x1 unit cubes?? 2 Answers

Is it possible to zoom in or out with the pixel perfect camera? 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