How to detect held down mouse button on a GUI Texture?

Hi

I have just spent the last few hours trying all sorts of combinations to get something that should be easy working.

I have a GUI Texture with a simple function OnMouseDown() event on it. When clicked this rotates a game object by a small amount and works fine

But I also want to continually rotate the gameobject if the user holds down their mouse button over the GUI texture but cant get it to work.

It either detects a single click rather than a held down mouse button or if I use function update to check the mouse button state then it repeats indefinitely even if no mouse is held down.

e.g.

function Update(){

if(Input.GetMouseButtonDown(0) )  
   //rotate += obj here

Could sombody please just post me a small snipit of code to handle mouse clicks and mouse button held down?

Thanks
Anim

What you’re looking for is probably:

function Update() {
if(Input.GetMouseButton(0)  guiTexture.HitTest(Input.mousePosition))
{
   // rotate object
}

}

Thanks Arzi, that worked great.

How would one learn that? I spent ages in the documentation today trying to find a solution to not effect.

Cheers
Anim

That would be a case of applying the information from around the documentation. Input.GetMouseButton is explained in the Input class and HitTest in the GUITexture class respectively.

What one does constantly while programming is breaking down problems into smaller parts which can then be resolved, in this case finding out the mouse button state, the mouse position and the area that on which the mouse button will trigger the desired effect.

I don’t think there’s one and only way to learn things like that, like all things it’s just practice, practice, practice.

Hope this helps :slight_smile:

I agree, there seem to be many ways to “almost” do what you want but only one of them will do it right. That’s the part where I get stuck the most, practice as you say :slight_smile:

Thanks
Anim

Experience, too. After a while, instead of searching through the script reference and tutorials to find each of the pieces you’ll need to accomplish something, you can access your own memory (your internalized script reference, if you will) and say “well, I know how to find a mouse position, and I know how to find the mouse state, and I know a few ways to find the texture’s location. Put together, those will do what I want.”

The ssssssssslow part is when you’re new and learning all of those pieces.

You can also examine the libraries that yield that functionality.

For example, in Visual Studio 2010 (and C# of course), simply typing in the keyword “Input” and then pressing the “period” key will pop up a list of available methods (called Intellisense).

You can go further by pulling up the file that defines the functionality. You won’t get the source code, but you will get the “metadata”, allowing you to browse what the inputs and outputs are.

After that, you just experiment with the inputs.

Self describing code (and type reflection) has been a blessing these recent years. Now, if I can just convince the Unity devs to add some XML comments to their methods, I won’t even need to go to the help file, as they will also popup in the intellisense.

(Yes, I did add that to the wish list). :smile: