- Home /
How to use part of an image for a button
I am trying to create a region select type thing with a map of a country. Below is similar to the image I will be using. I was wondering how you would create each color(region) on the map do a different function, so basically make each color its own button. The image is currently in unity as a texture.
Answer by robertbu · May 25, 2013 at 12:28 AM
One solution is to switch based on the color at the position clicked. Use a form of Physics.Raycast() that takes a RaycastHit parameter. Use the textureCoord property in the RaycastHit to access the pixel of the texture hit. Execute appropriate code based on the color.
Note if your graphics are not pure colors like you have in the diagram above, you can put a simplified version of the map on top on using a separate plane with a very slight alpha.
Thankyou, so the other image would have very slightly different alpha for each region?
You could use alpha, or you could use a color. The reason I indicate a "slight" alpha is because of a bug or optimization in Unity. I was playing with a similar concept a while back using the color. I found that if I did a GetPixel() on a pixel that had 0 for an alpha, the color returned was white (1.0, 1.0, 1.0, 0.0) no matter what the color in the original texture. But if I assigned an alpha of 1 (in the range of 0 to 255) in Photoshop, GetPixel() would return the correct color. So that is like having an alpha of .004 in terms of the floats Unity uses, so the texture was not visible.
Your answer

Follow this Question
Related Questions
How to have correct color on imGUI Buttons (as dynamic textures) avoiding multiply effect ? 1 Answer
It is possible to use texture atlases like button? 1 Answer
How to make texture fit in GUI button? 1 Answer
How to change the color of predefine portion of the texture? 1 Answer
Making a button in unity iphone? 3 Answers