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
1
Question by Mz3D · Aug 19, 2012 at 02:48 PM · androidtextureguitexturehittestguielement

use HitTest to check if a screen area is touched

I'm developing a game for Android. I have created various Textures (not GUITextures!) to use as buttons and i've placed them at relative positions on the screen so they can be moved and resized according to screen resolution. Now i need to code this: when the user touches a specific area of the screen (where one of these textures is, but this is not important) something happens. I tried to use HitTest but Unity keeps giving me the error "an istance of type 'UnityEngine.GuiElement' is required to access non static member 'HitTest' ". Is there a way i can use HitTest just to check if an area of the screen has been touched without using GuiTextures?

This is the code i've used (well, it's copied from unity scripting reference)

 function Update () {
 
     if(guiText.HitTest(Vector3(100,100,0))){
 
         //something happens
     }
 
 }
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
1

Answer by aldonaletto · Aug 19, 2012 at 02:57 PM

No, HitTest is GUITexture stuff. Textures drawn with the new GUI system (GUI.DrawTexture) aren't seen by HitTest. You should use GUITexture do display the buttons and be able to use HitTest - this would also allow multitouch capability. Take a look at my answer to this question - I have not tested it (my tablet has broken) but suppose it may be at least a good starting point.

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 Mz3D · Aug 19, 2012 at 03:28 PM 0
Share

Is there another way to check if the touch is inside a rect?

avatar image aldonaletto · Aug 19, 2012 at 03:49 PM 1
Share

You can use Rect.Contains:

 if (rButton.Contains(touch.position)){
   print("button 1 clicked");
 }

But keep in $$anonymous$$d that the rectangles passed to the new GUI system have an inverted Y coordinate (0 is the top, Screen.height is the bottom). You can use the same rectangles that define the texture position/size in DrawTexture, but should invert the touch position y coordinate:

var but1: Texture2D; var but2: Texture2D; var r1 = Rect(10,10,200,30); var r2 = Rect(220,10,200,30);

function OnGUI(){ GUI.DrawTexture(r1, but1); GUI.DrawTexture(r2, but2); }

function Update(){ if (Input.touchCount > 0){ for (var touch: Touch in Input.touches){ var pos = touch.position; // invert Y coordinate: pos.y = Screen.height - pos.y; if (r1.Contains(pos)){ print("button 1 pressed"); } else if (r2.Contains(pos)){ print("button 2 pressed"); } } } }

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

9 People are following this question.

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

Related Questions

GUITexture HitTest not registering 0 Answers

Change GUITexture Using UnityScript 1 Answer

conversion of script guitexture to cube or other primitive (iphone) 2 Answers

[Question Duplicated] conversion of script guitexture to cube or other primitive (iphone) 0 Answers

Mipmapping on Tablets 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