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
2
Question by JanVog · Mar 25, 2015 at 10:56 AM · raycastmouseraycasthit2dmouseoverhover

New UI ~ Check for mouse-hover

In my Scene I have a main canvas "UICanvas", a UI Panel with ScrollRect, Image and Mask Components in this canvas (second picture) and an UI image in the GameObject which contains a large image (larger than the ScrollRect-Panel) and my main script!


alt text


alt text


My map is moveable and scrollable, but you can also scroll when the mouse isn't over the ScrollRect-Panel - I want to change this, but how? I don't know how to do it, I tried it via MouseOver function, but it didn't work, raycasting just gave me errors and the tooltip-method didn't work aswell, cause I use New UI.

The most promising method seemed to be rect.Contains(), but there was another problem: The size of my Panel is relative to the size of the canvas, because I use Screen Space - Overlay. So the size of this Panel wasn't the same as the real size it has on the screen... Now the problem is that when my mouse is over the Panel, I'm just sometimes able to scroll and on the left part of the screen, I'm able to scroll the Image although there is just empty space... It seems like the Panel-Container is a bit left-down-shifted... (in fact the distance between the origin of the Panel and the left-bottom corner of the screen - in other words: The half of the size of the Panel + the Min-Anchor*screen size)

Here's my script on the UI Image:

 #pragma strict
 
 //var minSize: float = 10.0;
 //var maxSize: float = 130.0;
 var sensitivity: float = 1;
 
 private var map: RectTransform;
 var scrollRect: RectTransform;
 
 private var xOffset:float;
 private var yOffset:float;
 
 function Awake () {
     map = gameObject.GetComponent(RectTransform);
 }
 
 function Start(){
     //xOffset = Screen.width*scrollRect.anchorMin.x; 
     //yOffset = Screen.height*scrollRect.anchorMin.y;
     
     
 }
 
  //TODO: Dahin zoomen, wo die Maus ist
 function Update () {
     
     if(scrollRect.rect.Contains(Vector2(Input.mousePosition.x, Screen.Width - Input.mousePosition.y))){
     //if(scrollRect.(Vector2(Input.mousePosition.x-(scrollRect.anchorMax.x  - scrollRect.anchorMin.x)*Screen.width - xOffset, Input.mousePosition.y-(scrollRect.anchorMax.y -scrollRect.anchorMin.y)*Screen.height - yOffset))){
         
         if(Input.GetAxis("Mouse ScrollWheel") < 0){
             map.sizeDelta.x /= Input.GetAxis("Mouse ScrollWheel")*-1 * (1/sensitivity) + 1;
             map.sizeDelta.y /= Input.GetAxis("Mouse ScrollWheel")*-1 * (1/sensitivity) + 1;
         }
         if(Input.GetAxis("Mouse ScrollWheel") > 0){
             map.sizeDelta.x *= Input.GetAxis("Mouse ScrollWheel") * (1/sensitivity) + 1;
             map.sizeDelta.y *= Input.GetAxis("Mouse ScrollWheel") * (1/sensitivity) + 1;
         }
         
     }//Debug.Log(Input.mousePosition);
 }

It would be very kind of you all, if you could help me with this :D

PS: I'd like to have all my code in (Unity-)Javascript

scene.jpg (101.2 kB)
scrollrectpanel.png (38.5 kB)
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
6
Best Answer

Answer by ParanoidSnail · Mar 25, 2015 at 12:22 PM

I use this method, works okay for me.

 RectTransform map;
 
 public bool isMouseOverMap() {
         Vector2 mousePosition = Input.mousePosition;
         Vector3[] worldCorners = new Vector3[4];
         map.GetWorldCorners(worldCorners);
 
         if(mousePosition.x >= worldCorners[0].x && mousePosition.x < worldCorners[2].x 
            && mousePosition.y >= worldCorners[0].y && mousePosition.y < worldCorners[2].y) {
             return true;
         }
     }    
     return false;
 }


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 JanVog · Mar 26, 2015 at 06:15 PM 0
Share

Works fine! Thanks man :D

avatar image JanVog · Mar 26, 2015 at 06:41 PM 0
Share

But I have another question now... How can I see in which point my mouse is in this Panel?

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Raycast for mouseover isn't working 0 Answers

How do I detect when mouse passes over an object? 4 Answers

How to make text appear only when over button(highlighted) 2 Answers

Raycast2D only detects objects directly over origin 0 Answers

2d raycast enemy detection problem. 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