Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 fizzy6868 · Feb 24, 2014 at 11:40 PM · android2dguiguitexture

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 ();
     }
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

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.

Follow this Question

Answers Answers and Comments

21 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

UI Prefab not instantiating on build for Android. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges