- Home /
 
Unity 2d mobile android GUI texture button
hello all I am new to unity. I looked up a tutorial on how to set up buttons in the 2d workload but it either turns the hole screen as a button or does not register the touch at all can anyone help me here is the code below thanks in advance :)
this is the controller code:
 using UnityEngine;
 using System.Collections;
 
 public class TouchLogicV2 : MonoBehaviour 
 {
     public static int currTouch = 0;//so other scripts can know what touch is currently on screen
     [HideInInspector]
     public int touch2Watch = 64;
     
     public virtual void Update()//If your child class uses Update, you must call base.Update(); to get this functionality
     {
         //is there a touch on screen?
         if(Input.touches.Length <= 0)
         {
             OnNoTouches();
         }
         else //if there is a touch
         {
             //loop through all the the touches on screen
             for(int i = 0; i < Input.touchCount; i++)
             {
                 currTouch = i;
                 //executes this code for current touch (i) on screen
                 if(this.guiTexture != null && (this.guiTexture.HitTest(Input.GetTouch(i).position)))
                 {
                     //if current touch hits our guitexture, run this code
                     if(Input.GetTouch(i).phase == TouchPhase.Began)
                     {
                         OnTouchBegan();
                     }
                     if(Input.GetTouch(i).phase == TouchPhase.Ended)
                     {
                         OnTouchEnded();
                     }
                     if(Input.GetTouch(i).phase == TouchPhase.Moved)
                     {
                         OnTouchMoved();
                     }
                 }
                 
                 //outside so it doesn't require the touch to be over the guitexture
                 switch(Input.GetTouch(i).phase)
                 {
                 case TouchPhase.Began:
                     OnTouchBeganAnywhere();
                     break;
                 case TouchPhase.Ended:
                     OnTouchEndedAnywhere();
                     break;
                 case TouchPhase.Moved:
                     OnTouchMovedAnywhere();
                     break;
                 case TouchPhase.Stationary:
                     OnTouchStayedAnywhere();
                     break;
                 }
             }
         }
     }
     
     //the default functions, define what will happen if the child doesn't override these functions
     public virtual void OnNoTouches(){}
     public virtual void OnTouchBegan(){print (name + " is not using OnTouchBegan");}
     public virtual void OnTouchEnded(){}
     public virtual void OnTouchMoved(){}
     public virtual void OnTouchBeganAnywhere(){}
     public virtual void OnTouchEndedAnywhere(){}
     public virtual void OnTouchMovedAnywhere(){}
     public virtual void OnTouchStayedAnywhere(){}
 }
 
               this is the button code:
 using UnityEngine;
 using System.Collections;
 
 public class TestButton : TouchLogicV2 
 {
     public bool disableTouches;
     
     public override void Update()
     {
         //you can do some logic before you check for the touches on screen
         
         if(!disableTouches)//(optional) dynamically change whether or not to check for touches
             base.Update();
     }
     
     public override void OnTouchBegan()
     {
         Application.Quit ();
     }
 }
 
              Answer by corriedotdev · Mar 18, 2014 at 09:25 AM
I'm not able to get on unity at the moment. But if your new to unity then make sure that the gui texture has been scaled correctly. I would usually copy this code and see what i make of it.
If its covering the whole screen the chances are that your texture is to high a resolution and you don't need that. If this is the case make a smaller texture in Photoshop to save memory.
Your answer
 
             Follow this Question
Related Questions
Game Over 2D GUI position 0 Answers
Change Texture onHover GUI.Button 1 Answer
HUD with a lot of elements, efficient way? 3 Answers
Android tablet 10.1 and guiTexture gui placement with unity 0 Answers