I have several Texture2D images on a GUI. I want the user to be able to click on one of them to trigger an event. Is this possible? If so, how would you accomplish this. Currently, my only idea is to use Raycasting.
You can use raycasting if the textures are on 3D objects within the scene (see this thread for an explanation and some code). However, this doesn’t work for textures rendered with GUI functions. You can detect if the mouse is within a given rectangle using Rect.Contains:-
function OnGUI() {
var texRect: Rect = <whatever>;
GUI.DrawTexture(texRect, texture);
if (texRect.Contains(Input.mousePosition)) {
...
}
}
WIth this information, you can detect clicks on the texture, swap it for another texture on mouseover, etc.