- Home /
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!
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
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;
}
But I have another question now... How can I see in which point my mouse is in this Panel?