I'm creating tool, custom Unity editor window and I'm wondering if there is anyway to get some update function just when mouse position is in window? (Update void works only in play mode)
void OnGUI(){
// Code from here only "runs" when mouse is in window and mouse moves, so if
// I'm checking if mouse position is in some box rect, it will return true only once
// - at start when user will move mouse to box, that won't detect "holding" and not
// moving mouse on object
}
Cheers!
Related
need a little help with something. My scene consists of 4 spheres, and what I'm trying to achieve is when you click on one sphere, the colour of the sphere changes, and as you drag the mouse (while it's still held down) onto the other spheres, the other spheres change colour too. So far what I've managed to do is change the colour of the sphere when it's clicked, but I'm not able to change the colour of the other spheres when I drag the mouse onto them. I assume this is obviously because the code is only run when the mouse is first clicked. Is there a way to change this so a sphere changes colour when the mouse is clicked and dragged onto it? I've tried playing around with OnMouseDrag but with no success. Here's a link to a video of what I've got so far and what I'm trying to go for: https://youtu.be/KOBqnH0je6A
(All spheres have the same script attached) And here's my code:
`
public GameObject sphere;
public Material material;
void OnMouseDown()
{
sphere.GetComponent<MeshRenderer>().material = material;
}
`
Managed to achieve what I wanted by using a static boolean. The boolean is set to true when the mouse button is clicked, then an OnMouseEnter function changes the material of the object if the boolean is true. Then the boolean is set to false when the mouse is let go of!
Display the UI of the setting screen in the scene using the FPSController, And I want to be able to set up the game (for example, the camera's FOV).
the mouse cursor is appeared by pushed ESC key.
However, when the FPSController is active, the mouse cursor disappears when clicked and setting can not be made.
I checked the script, FirstPersonController.cs (script for FPSController gameObject), but there was no description about mouse cursor.
The FirstPersonController.cs has not been rewritten. There is a code at the link below.
https://qiita.com/ABCDEEEEEE/items/4f2452a88fb1050684fc
I want to keep the mouse cursor from disappearing even when FPSController is active.
I am having some problem with mouse position in MonoGame and I can't really figure out what I am doing wrong. I want the game window to be at the position of my cursor, so I made this simple line:
protected override void Update(GameTime gameTime)
{
Window.Position = new Point(Mouse.GetState().X, Mouse.GetState().Y);
base.Update(gameTime);
}
But if I do this, the game window flickers between two positions. What is the problem ? Am I doing something wrong ?
Thank you!
Well it actually makes sense when you think about it. Let me make an example:
Cursor is 500,500 (in relation to Window)
Window is 300,300 (in relation to Screen)
The first time the code runs, it moves the window to 500,500.
Meaning the cursor will now have a location of 300,300 (in relation to Window).
The second time the code runs, it will detect the mouse is now at 300,300, and thus move the Window back to 300,300.
As such it runs forever.
To accomplish what I think you want, you'd need to account for the current position of the Window.
Which means you'll have to "lock" the position of the mouse (in relation to the Window), and whenever this changes, move the Window the amount that the mouse position moved. And of course move the mouse back again
I have a 2D game in which I use only the mouse as input.
How can I make it so that my when the mouse hovers over a Texture2D object, the Texture2D and the mouse cursor change, and when the texture is clicked it moves to another place.
Simply put, I want to know how to do something when I hover over or click on a Texture2D.
In XNA you can use the Mouse class to query user input.
The easiest way of doing it is to check the mouse state for each frame and react accordingly. Is the mouse position inside a certain area? Display a different cursor. Is the right button pressed during this frame? Show a menu. etc.
var mouseState = Mouse.GetState();
Get the mouse position in screen coordinates (relative to the top left corner):
var mousePosition = new Point(mouseState.X, mouseState.Y);
Change a texture when the mouse is inside a certain area:
Rectangle area = someRectangle;
// Check if the mouse position is inside the rectangle
if (area.Contains(mousePosition))
{
backgroundTexture = hoverTexture;
}
else
{
backgroundTexture = defaultTexture;
}
Do something while the left mouse button is clicked:
if (mouseState.LeftButton == ButtonState.Pressed)
{
// Do cool stuff here
}
Remember though that you will always have information of the current frame. So while something cool may happen during the time the button is clicked, it will stop as soon as released.
To check for a single click you would have to store the mouse state of the last frame and compare what has changed:
// The active state from the last frame is now old
lastMouseState = currentMouseState;
// Get the mouse state relevant for this frame
currentMouseState = Mouse.GetState();
// Recognize a single click of the left mouse button
if (lastMouseState.LeftButton == ButtonState.Released && currentMouseState.LeftButton == ButtonState.Pressed)
{
// React to the click
// ...
clickOccurred = true;
}
You could make it even more advanced and work with events. So you would still use the snippets from above, but instead of directly including the code for the action you would fire events: MouseIn, MouseOver, MouseOut. ButtonPush, ButtonPressed, ButtonRelease, etc.
I would just like to add that the Mouse Click code could be simplified so that you don't have to make a variable for it:
if (Mouse.GetState().LeftButton == ButtonState.Pressed)
{
//Write code here
}
I need to freeze the windows mouse cursor on the screen, so it stays hovered over a specific UI element. While the mouse is in this frozen state, I would still like to be able to interact with the UI using a "fake" mouse pointer.
Currently, I've got a low level mouse hook that prevents WM_MOUSEMOVE messages from being passed along, effectively stopping all real mouse movement. However, when I don't pass along the updated coordinates, windows actually sends me the old coordinates in a separate WM_MOUSEMOVE message, as if to correct for the fact that the mouse didn't move.
Any idea on either how to prevent Windows from sending me these corrected coordinates, or another approach of how I can freeze the actual mouse curosr and still allow the physical mouse to control a "Fake" cursor?
You can suppress mouse cursor movement by using DirectInput exclusive mode.
You should acquire mouse input device when cursor movement must be suppressed. This will block windows cursor movement but you can get messages for your fake cursor using DirectInput API.
If you move the mouse cursor position in response to a WM_MOUSEMOVE message, to put the mouse back to where you want it, you will get another mouse move message because the mouse has been moved again (because you moved it). To stop this, don't set the mouse position if it is already in the right place.
You may also want to clip the mouse position to your window and get exclusive access to it.
Why don't you just draw an image of the cursor at the place you want it frozen, then set the real mouse cursor to be hidden (Cursor.Hide IIRC).
It's not clear to me what you are trying to do, perhaps making a tutorial for your app and you want to show mouse movements, etc. Perhaps this library will be useful:
http://www.codeproject.com/KB/system/globalmousekeyboardlib.aspx