- Home /
reference Gui texture outside script
hey guys, im trying to make some simple gui texture buttons that when clicked, zooms in and out the camera. one button is zoom in, another is zoom out. now im trying to reference the button in a script which is attached to the camera already. ive tried some methods like void onmousedown however im not sure how to reference gui textures outside the script? any help would be great
sorry should of been clearer, i mean the script is not attached to the gui textures, its attached to a camera but id like to reference the textures by checking if they have been clicked
Answer by ArkaneX · Sep 21, 2013 at 03:37 PM
Assuming you have two texture variables (textureZoomIn and textureZoomOut) in your script attached to camera, then you need to do:
void OnGUI()
{
if (GUI.Button(new Rect(0, 0, 100f, 30f), textureZoomIn))
{
ZoomIn();
}
if (GUI.Button(new Rect(0, 32f, 100f, 30f), textureZoomOut))
{
ZoomOut();
}
}
UPDATE: when writing above snippet, I was thinking of more common way of using textures in GUI buttons. For your case different solution is required:
void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (ZoomIN.HitTest(Input.mousePosition))
{
ZoomIn();
}
if (ZoomOUT.HitTest(Input.mousePosition))
{
ZoomOut();
}
}
}
i have them attached to the script by using public GUITexture ZoomIN; public GUITexture ZoomOUT;
and then i have attached guitextures in the editor
also this does work, however not exactly what i meant.
http://i.imgur.com/gnVE5j5.png
as you can see with your method i now have 2 buttons in the upper left which are recognised when clicked. However i want the two buttons on the right to be zoom in and zoom out.
I've just thought about one more thing - transparent parts of GUI texture. Googling a bit I found this question - please take a look at the answer if you need a solution. It's about finding opaqueness of pixel under mouse, and ignoring click if it is fully transparent.