Simulate a mouse Click?

I am have been making a game solely for pc, i have now come to the point id like to implement joystick, I’ve done so successfully with every key binding, however my game comprises of mouse clicks, OnMouseUp to be precise, and I am stumped how to accomplish this, i merely want to have for example:

if (input.GetButtonDown(“A”) {

//simulate mouse click in general

}

Which would then effect my OnMouseUp scripts through out the game as if the mouse it self was clicked.

This i would have thought was simple as my cursor is locked to the centre of the screen, my mouse enters and exit codes work fine and prove this, its just getting the mouse to click i cant do, please help its like the last thing on the to do list :stuck_out_tongue:

OnMouseUp(); Is just a function. I would think you can just call it as such. if (input.GetButtonDown("A") { //simulate mouse click in general OnMouseUp(); } maybe its as simple as that... I know OnMouseUp() is an event that happens when a mouse button is clicked but it may be as simple as forcing the state by calling the function?

where getRun is pressing the Run button:- This is what i put in a script in JS.. function FixedUpdate(){ if(getRun){ OnMouseUp(); } } function OnMouseUp(){ Debug.Log("mouse"); } And sure enough when i pressed the Run button Debug.Log printed "mouse"

Did that help?

i was a tad vague im afraid to say by joystick i meant I'm using an xbox controller no a touch screen function, I'm also wanting a way that will all me to do so with out having to reedit all my previous codes, the "getRun" function would work, however this means i will have to edit all scripts with the mouse function in it, I'm also quesrioing whether this would mean i could call the mouse function wherever in the scene and not have to be entered with the mouse would it not?

I tested the get run idea and as I said it does click the mouse but it does not require the mouse to be on the object, I can click anywhere in my scene and the function happens

2 Answers

2

This is an old question, but it was quite hard to find this as a solution. So hope this will help someone. Also, with the new Input System, it might be even easier.


To put it simply. You can inherit your own class from StandaloneInputModule. And there you are able to call ProcessTouchPress which will resolve into a clicking action. In general i would recommend looking through the source code of the Unity UI system which contains the EventSystem.


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class TestInputModule : StandaloneInputModule
{
    public void ClickAt(float x, float y)
    {
        Input.simulateMouseWithTouches = true;
        var pointerData = GetTouchPointerEventData(new Touch() 
        {
            position = new Vector2(x, y),
        }, out bool b, out bool bb);

        ProcessTouchPress(pointerData, true, true);
    }

    void Update()
    {
        if(Input.GetKeyDown(KeyCode.F1))
        {
            ClickAt(Screen.width / 2, Screen.height / 2);
        }
    }
}

Hi, is there any possible way to simulate a scrolling?

Thank you so much for this

Seems incomplete: I get this error message: NullReferenceException: Object reference not set to an instance of an object UnityEngine.EventSystems.PointerInputModule.GetTouchPointerEventData (UnityEngine.Touch input, System.Boolean& pressed, System.Boolean& released) (at ./Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/InputModules/PointerInputModule.cs:98) TestInputModule.ClickAt (System.Single x, System.Single y) (at Assets/mouseController.cs:91)

Hey, this code got finalized in this repository https://github.com/kipash/UnityUITests :)

This already have been answered How can I control the mouse (Move and send mouse click signals) via the keyboard (Specifically for PC - For sending mouse event via buttons, see 2nd edit)