• 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
2
Question by hawken-2 · Jul 06, 2011 at 09:42 AM · buttoniphoneguitexturetap

Making a GUI Texture tappable on iPhone

Disclaimer: I've searched everywhere for this, including the Unity documentation.

In the Unity simulator, this works fine if I add a script like this to my GUITexture in the hierarchy:

 function OnMouseUp (){
         //do stuff
 }

However when run on the iPhone, this doesn't have any effect. After a bit of searching I tried:

 if(Input.touchCount>0){
 //do stuff
 }

This doesn't appear to do anything in the simulator or on the handset.

One thing that does work fine on the iPhone is building a GUIButton like this:

 function OnGUI() {
     
         if(GUI.Button(Rect(0,400,320,44),"I'm a code generated button!")) {
             //do stuff        
             }
     }

However this is code for dynamically drawing a button, not code for placing on a GUITexture game object.

Any clues? :(

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

4 Replies

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

Answer by hawken-2 · Jul 06, 2011 at 09:58 AM

Actually, I fixed this by looking at some Android questions!!

Here's the ballache answer (you need to do this per button... ):

 function Update(){
     if (Input.touchCount == 1)
     {
     var touch: Touch = Input.touches[0]; 
     
     if(touch.phase == TouchPhase.Began && guiTexture.HitTest(touch.position))
     
         {
         // do amazing things
         } 
     }
 }
Comment
Add comment · Show 2 · 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
avatar image reptilebeats · Jan 18, 2012 at 10:12 PM 0
Share

good stuff works great just what i was looking for simple code for a simple question which no can answer and come up with some complicated method which takes up more speed or better know as lag, some guy actually wanted to assign each one to adifferent layer

avatar image moghes · Mar 13, 2013 at 05:14 PM 0
Share

nice one! simple and clear

avatar image
0

Answer by Annihilator · Apr 05, 2012 at 12:39 PM

,how do you use this with multiple touches?

Lets say you have 2 GUITextures and you want each of them to have independent touches, how do you modify the code so that you

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

Answer by Annihilator · Apr 05, 2012 at 12:39 PM

how do you use this with multiple touches?

Lets say you have 2 GUITextures and you want each of them to have independent touches, how do you modify the code so that you if you touch the 1 texture if activates, touching the other activates that one aswell and releasing either of them deactivates the one released... Same works if they are touched independently

Comment
Add comment · Show 2 · 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
avatar image reptilebeats · Apr 05, 2012 at 01:04 PM 0
Share

i actually from the code above modded it to act perfect with multiple touches, so say in my game i can hold the acceleration and turn.

basically all you have to do is check each touch in a loop{

and in this write the code, i can post a snipit of the code later if you need it but i have to go now so i havent got time

}

avatar image save · May 28, 2012 at 09:02 PM 0
Share
 if (Input.touchCount>0) {
     for (var touch : Touch in Input.touches) {
         if (touch.phase == TouchPhase.Began && guiTexture.HitTest(touch.position)) {
             
         }
     } 
 }
avatar image
0

Answer by Orthomyxovirus · Sep 11, 2012 at 04:59 AM

I have some problem, i want do something like this:

When i tap guitexture it change from texture1 to texture2, so i combine two scripts this for tap and this:

 var texture1 : GUITexture;
 var texture2 : GUITexture;
 
 var someTexture : GUITexture;
 someTexture = texture1;
 guiTexture.texture = texture1.texture;
 
 function Update () {
 }
 
 function OnGUI() {
 
     if(GUI.Button(Rect(0,0,100,40),"Cambia fondo")){
         if(someTexture == texture1) {
             someTexture = texture2;
         } else if (someTexture == texture2) {
             someTexture = texture1;
         }   
 
     }
 }

for change GUITexture

but first what i do is check this script, it work but GUITexture are not change on screen in inspector i see changes texture1 to texture2

I want do something like this:

 var texture1 : GUITexture;
 var texture2 : GUITexture;
 
 var someTexture : GUITexture;
 someTexture = texture1;
 guiTexture.texture = texture1.texture;
 
 
 function Update(){
     if (Input.touchCount == 1)
     {
     var touch: Touch = Input.touches[0]; 
 
     if(touch.phase == TouchPhase.Began && guiTexture.HitTest(touch.position))
 
        {
         if(someTexture == texture1) {
             someTexture = texture2;
         } else if (someTexture == texture2) {
             someTexture = texture1;
         }   
        } 
     }
 }

I search 2h for something and i cant find anything, so… somebody can help me?

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

6 People are following this question.

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

Related Questions

Strange GUITexture Bug?? 0 Answers

"The name does not exist in the current context" error 1 Answer

how to check if touching an guitexture android 0 Answers

Button does not work when another is active? iPhone 5 Answers

Making GUI Buttons on a GUI Texture 1 Answer

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