Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Genome · Jul 21, 2016 at 09:52 PM · textureguitexturedetectionrect2dtexture

Rect, Texture, GUI, Detection.

So I've been wanting to add full controller support to a current project (which is coming along fairly quickly).

However I've created a custom mouse using GUI texture and use the controller axis to move it (since Unity doesn't support editing windows cursor position etc.)

My issue now is detecting when the texture is over another uGUI? I'd rather not remake the menu system if I don't have to. There 3 buttons that each have a text child. these 3 buttons are all under the same parent canvas.

How would I go about detecting if the custom 2Dtexture is within any of the buttons locations?

I've seen something about Rect.Contains(); But is this going to help?

Do I need to convert the button objects from world to screen point then check if my rect contains both the texture and the button?

Or similarly convert button to screen point and check the X/Y to the textures X/Y?

Comment
Add comment · Show 1
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 Genome · Jul 21, 2016 at 10:34 PM 0
Share

Well. After messing around with it a bit I have found that:

I can get a new Rect based on the Rect transform values from the gameobjects inside the canvas. Then use Rect.Contains() on that to see if my custom cursor point x/y is inside that rect.

First run sets the X/Y locations all weird but I'm sure I can eventually find out how to get them to be correct in relation to the actual object.

^ For clarification. $$anonymous$$Y button is in middle of screen yet the rect that I pull says its values are @ like -153, -50, and 100, 50. Essentially the correct size but nowhere near the correct position.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Genome · Jul 22, 2016 at 05:32 AM

Shout out to this guy: http://www.oguzkonya.com/2016/01/18/converting-unitys-recttransform-to-a-rectangle-in-screen-coordinates/

So with using this extension below and taking my custom cursor position and passing that in the Rect.Contains() I now have a correct position for each button that I can interact with.

 using UnityEngine;
 using System.Collections;
 
 public static class RectTransformExtension {
    
     public static Rect GetScreenRect(this RectTransform rectTransform, Canvas canvas) {
         
         Vector3[] corners = new Vector3[4];
         Vector3[] screenCorners = new Vector3[2];
 
         rectTransform.GetWorldCorners(corners);
 
         if (canvas.renderMode == RenderMode.ScreenSpaceCamera || canvas.renderMode == RenderMode.WorldSpace)
         {
             screenCorners[0] = RectTransformUtility.WorldToScreenPoint(canvas.worldCamera, corners[1]);
             screenCorners[1] = RectTransformUtility.WorldToScreenPoint(canvas.worldCamera, corners[3]);
         }
         else
         {
             screenCorners[0] = RectTransformUtility.WorldToScreenPoint(null, corners[1]);
             screenCorners[1] = RectTransformUtility.WorldToScreenPoint(null, corners[3]);
         }
 
         screenCorners[0].y = Screen.height - screenCorners[0].y;
         screenCorners[1].y = Screen.height - screenCorners[1].y;
 
         return new Rect(screenCorners[0], screenCorners[1] - screenCorners[0]);
     }
 
 }

And in my code:

         Vector2 mouseVector = new Vector2(currentX, currentY);
         RectTransform newRectT = buttonGameObject.GetComponent<RectTransform>();
         Rect newRect = RectTransformExtension.GetScreenRect(newRectT, myParentCanvas);
 
         if (newRect.Contains(mouseVector))
         {
             Debug.Log("Mouse Over:");
         }

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

59 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 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 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

Activating a GUITexture by clicking on an object (script in c#)? 2 Answers

How to script GUI Texture Window to see it in Script (Object's component) 1 Answer

GUITexture placement - Screen Resolution 1 Answer

Why can't I change the Texture of my GUITexture? 1 Answer

Mirroring GUITexture While Preserving Rect.Contains() 0 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