Hi! I have a GUIbutton from which i removed the standard background button frame, so the button is essentially a texture. My game is for Android, and I'd like to add the effect that the button is pressed down when you hold down your finger on it. Haven't had any luck with finding out how to do this, anyone got the answer? :)
You store the GUITextures in individual VARS and switch them out when you hold down the GUITexture. or you can manuipulate the Material Color Alpha channal when holding down the GUITexture
He's not talking about a GUITexture, it's a GUIButton, which doesn't contain a material, it contains GUIContent.
ohh ok. I read his question wrong lol
Answer by clunk47
Sep 23, 2013 at 10:01 PM
Here is one solution that works flawlessly on my end. You can use touch code, or mouse code. I prefer unity's mousePosition, since I'm just used to it, and it works for mobile as well. In this example (C#) I check if the Rect for my button contains the mouse or touch position. If it does, I check for touch or click. When the button is clicked or touched, the texture changes. In this example, the two texture choices are public and can be assigned in the inspector. I also set the GUI.skin.button background to match the GUIContent image and text, depending on the touch / click condition.
public class example : MonoBehaviour
public Texture2D tex1;
public Texture2D tex2;
//The mouse code in this example works for touch on android.
buttonContent = new GUIContent();
buttonRect = new Rect(0, 0, 64, 64);
mouse = new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y);
if(buttonRect.Contains(mouse) && Input.GetMouseButton(0))
buttonContent.text = "Touching";
buttonContent.image = tex2;
buttonContent.text = "Not Touching";
buttonContent.image = tex1;
GUI.skin.button.normal.background = (Texture2D)buttonContent.image;
GUI.skin.button.hover.background = (Texture2D)buttonContent.image;
GUI.skin.button.active.background = (Texture2D)buttonContent.image;
if(GUI.Button(new Rect(0, 0, 64, 64), buttonContent))
//Code here what you want to affect by button press.
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
11 People are following this question.
Holding GUI Button Touch to Rotate Object
Why this simple code doesnt work?
Button reaction time problem
Android GUITexture Touch
GUI button in middle of screen