• 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
0
Question by NecrosDk · Oct 31, 2012 at 04:20 PM · androidguibuttontouch

Switching a GUIbuttons texture when holding your finger on it?

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? :)

Comment
Add comment · Show 3
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 Triqy · Sep 23, 2013 at 09:37 PM 0
Share

You store the GUITextures in individual VARS and switch them out when you hold down the GUITexture. or you can manuipulate the $$anonymous$$aterial Color Alpha channal when holding down the GUITexture

avatar image clunk47 · Sep 23, 2013 at 09:41 PM 0
Share

He's not talking about a GUITexture, it's a GUIButton, which doesn't contain a material, it contains GUIContent.

avatar image Triqy · Sep 26, 2013 at 09:48 PM 0
Share

ohh ok. I read his question wrong lol

1 Reply

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

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.

 using UnityEngine;
 using System.Collections;
 
 public class example : MonoBehaviour
 {
     GUIContent buttonContent;
     Rect buttonRect;
     public Texture2D tex1;
     public Texture2D tex2;
     
     //The mouse code in this example works for touch on android.
     Vector2 mouse;
     
     void Awake()
     {
         buttonContent = new GUIContent();
         buttonRect = new Rect(0, 0, 64, 64);    
     }
     
     void Update()
     {
         mouse = new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y);
         if(buttonRect.Contains(mouse) && Input.GetMouseButton(0))
         {
             buttonContent.text = "Touching";
             buttonContent.image = tex2;
         }
         
         else
         {
             buttonContent.text = "Not Touching";
             buttonContent.image = tex1;
         }
     }
     
     void OnGUI()
     {
         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.
         }
     }
 }



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.

Welcome to Unity Answers

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.



Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Android GUITexture Touch 0 Answers

Button reaction time problem 1 Answer

Why this simple code doesnt work? 0 Answers

Holding GUI Button Touch to Rotate Object 0 Answers

Create GUItexture Touch Button 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges