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 Rocotos · Feb 09, 2012 at 05:45 PM · androidtouchguitexturetapseparate

Detect touch on a specific GUITexture

Greetings, guys. While i'm working on my Unity3d Android project, i bumped into one thing, that I can't do properly. I'm creating an interface, and I have 2 GUITextures, each one represents a button. UP and DOWN. When I tap on one, animation plays well, but Unity does not count another GUITexture as separate button. So when I press it, it works like first button. Here is some code:


Code for button UP

 function Update () {
   if(Input.touchCount >= 1)
 {
     var touch : Touch = Input.touches[0];
 
     if(touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
     {
         animation["UP"].speed= 1.0;
       animation.Play("UP");
     }
     else
     {
         animation["UP"].speed= 0;
     }
 }
 }
 



Code for button DOWN

 function Update () {
       if(Input.touchCount >= 1)
     {
         var touch : Touch = Input.touches[0];
     
         if(touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
         {
             animation["UP"].speed= -1.0;
           animation.Play("UP");
         }
         else
         {
             animation["UP"].speed= 0;
         }
     }
     }


Im fact, I know, that i need to use "hit.test" method, but I dunno how to make it right. Thanks for your attention

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by dannyskim · Feb 09, 2012 at 06:43 PM

 function Start()
 {
     var hit : GUILayer = Camera.main.GetComponent(GUILayer);
 }
 
 function Update()
 {
     var hitObject : GUIElement = hit.HitTest( Input.mousePosition );
 
     if( hitObject != null )
     {
         switch( hitObject.name )
         {
             case "guiTexture_name1":
             // do stuff
                 break;
             default:
                 break;
         }
     }
 }
 
     
Comment
Add comment · Show 4 · 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 Rocotos · Feb 10, 2012 at 12:26 PM 0
Share

dannyskim, your method does not work fine. Script plays animation "UP" when i tap any part of my screen.

avatar image syclamoth · Feb 10, 2012 at 12:29 PM 0
Share

How are you using it? You need to have at least two objects, and you need to name them uniquely. What part of the code isn't working?

avatar image Rocotos · Feb 10, 2012 at 01:12 PM 0
Share

Well, I have 2 GUITextures called "up" and "down". I attach this script to each one, but change this line: case "guiTexture_name1" on case "up" and case "down".

case "up": animation["UP"].speed= 1.0; animation.Play("UP");

case "down": animation["UP"].speed= -1.0; animation.Play("UP");

But script "down" doesn't work at all and if I press anywhere on my touchscreen - animation UP start playing.

avatar image Shivansh · Feb 13, 2014 at 11:32 AM 0
Share

Rocotos code provided by dannyskin worked fine. You must check your code before commenting, Check out what you've wrote..

case down - you are playing same animation as in case up. So how do you expect that it will work fine?

avatar image
0

Answer by jacobschellenberg · Feb 09, 2012 at 08:02 PM

If you use the GUI.Button you will have more control over what buttons are being touched. The buttons will automatically react to touch inputs, which is really nice because you don't have to constantly be checking for a touch input. This is what I would do.

 void OnGUI(){
      if(GUI.Button(new Rect(0,0,50,30), "Up"){
           //play animation up
      }
      if(GUI.Button(new Rect(0,40,50,30), "Down"){
           //play animation down
      }
 }
Comment
Add comment · Show 1 · 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 dannyskim · Feb 09, 2012 at 08:06 PM 1
Share

It is pretty much recommended across the board to not use OnGUI in any mobile app / game developed in Unity. There is way too much overhead associated with this method. If you are going to use Unity's GUI System, the only reasonable option for mobile platforms is using GUITextures as they are considered GUIElements. GUIElements are not associated with OnGUI and thus do not exhibit the overhead of OnGUI.

avatar image
0

Answer by Rocotos · Feb 11, 2012 at 03:15 PM

I have another question (as I deal with previous). This script works only once, I mean when I want to play animation again and tap the button...nothing happend. What I need to add to my script?

function Update() {

  var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  var hit: RaycastHit;
     
  if (Input.GetMouseButtonDown(0) && Physics.Raycast(ray, hit, 100))
    {
     if (hit.collider.gameObject.tag == "newgame") 
      {
         animation.Play("newgame");
      }   
     
         }
     
     }
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

7 People are following this question.

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

Related Questions

Android Touch Input GUITexture 2 Answers

Move guiTexture along touch position 0 Answers

GUITexture touch play animation 0 Answers

GUITexture Touch Problem 2 Answers

GUITexture Button Help, Android touch 2 Answers


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