Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
0
Question by BillyJazz · Mar 01, 2014 at 11:07 PM · inputbuttonmousejoystickclick

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 :P

Comment
Add comment · Show 6
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 Dizmiester · Mar 02, 2014 at 01:01 AM 0
Share

On$$anonymous$$ouseUp(); Is just a function. I would think you can just call it as such.

 if (input.GetButtonDown("A") {
 
 //simulate mouse click in general
 On$$anonymous$$ouseUp();
 
 }

maybe its as simple as that... I know On$$anonymous$$ouseUp() 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?

avatar image Dizmiester · Mar 02, 2014 at 01:08 AM 1
Share

where getRun is pressing the Run button:- This is what i put in a script in JS..

 function FixedUpdate(){
 
     if(getRun){
         On$$anonymous$$ouseUp();
     }
 }
 function On$$anonymous$$ouseUp(){
     Debug.Log("mouse");
 
 }

And sure enough when i pressed the Run button Debug.Log printed "mouse"

avatar image Dizmiester · Mar 02, 2014 at 01:15 AM 0
Share

Did that help?

avatar image BillyJazz · Mar 02, 2014 at 01:40 AM 0
Share

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?

avatar image BillyJazz · Mar 02, 2014 at 01:00 PM 0
Share

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

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by vexe · Mar 02, 2014 at 05:07 PM

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)

Comment
Add comment · 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
2

Answer by ChrissTman · Aug 28, 2021 at 01:23 PM

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);
         }
     }
 }


Comment
Add comment · Show 3 · 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 VolkovSS · Jan 14 at 07:35 AM 0
Share

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

avatar image ChrissTman VolkovSS · Jan 14 at 07:55 AM 0
Share

Hey, it is way more complicated to simulate drag or scroll. If you want to achieve that, you should dive into the code and try to understand it and replace some functionality.

But, i think that's just for the old input system. The new one has a nice API. https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/Testing.html

So perhaps, revaluate if you don't want to move to the new Input System.

avatar image VolkovSS ChrissTman · Jan 14 at 08:00 AM 0
Share

No, new InputSystem available from 2020 version. But thank you anyway)

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

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

GetMouseButtonDown(1) true in multiple frames 1 Answer

Button.onClick.AddListener 1 Answer

Steelseries 3gc controller key mappings 0 Answers

Keyboard/Joystick Inputs Do Not Work until Mouse Clicked 0 Answers

Making one directional virtual joystick OR smoothly transitioning between buttons 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