Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by michael96schmidt · Jan 18, 2015 at 05:29 AM · uitriggereventevent triggering

How would I detect a right click with an event trigger?

I am using event triggers for my games UI menu but there is no option for a right click, only a click. How would I detect a right click on a button?

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

3 Replies

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

Answer by JackofTraes · Aug 22, 2017 at 02:24 AM

So there is an even simpler method. If anyone has a similar issue to mine (4.6 buttons that need to be right clicked), then you're in luck! Basically it involved augmenting your class (or creating a new one) with 'IPointerClickHandler'. Example below:

 using UnityEngine;
 using UnityEngine.EventSystems;
 
 
 public class MyRightClickClass : MonoBehaviour, IPointerClickHandler {
 
     public void OnPointerClick (PointerEventData eventData) {
         if (eventData.button == PointerEventData.InputButton.Right) {
             Debug.Log ("Right Mouse Button Clicked on: " + name);
         }
     }
 
 }

So there it is; a simple alternative to the other examples on this page for those that are seeking a clean (and short) solution.

Comment
Add comment · Show 4 · 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 isgirotto · Oct 02, 2017 at 01:36 PM 0
Share

You sir just saved me tons of work, Thanks!

avatar image JackofTraes isgirotto · Oct 04, 2017 at 11:25 PM 0
Share

No problem, I am glad that this helped someone! :)

avatar image ProfessorCat · Jun 20, 2018 at 10:55 PM 0
Share

Close but not quite....

OnPointerClick takes a BaseEventData param.

You have to cast it inside your handler.

avatar image Projekt-Krieg · Jul 20, 2019 at 09:35 AM 0
Share

there are the 3 solutions :D

the first likes to write, the second get the job quick and dirty done

and there is always the genius that extends short and clean what unity already created.

thanks to all of you, you made my day xD

avatar image
6

Answer by jenci1990 · Jan 18, 2015 at 08:15 AM

Creta a new c# script, call RightButtonEvent and paste it:

 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.EventSystems;
 using UnityEngine.Events;
 using System.Collections;
 [ExecuteInEditMode]
 [AddComponentMenu("Event/RightButtonEvent")]
 public class RightButtonEvent : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler {
     [System.Serializable]public class RightButton : UnityEvent{}
     public RightButton onRightDown;
     public RightButton onRightUp;
     private bool isOver = false;
     void Start () {
     }
 
     void Update () {
         if (Input.GetMouseButtonDown(1)) {
             onRightDown.Invoke();
         }
         if (Input.GetMouseButtonUp(1)) {
             onRightUp.Invoke();
         }
     }
 
     public void OnPointerEnter(PointerEventData eventData) {
         isOver = true;
     }
 
     public void OnPointerExit(PointerEventData eventData) {
         isOver = false;
     }
 }
 

Now you have new component in your menu at "Component/Event/RightButtonEvent" add it to any UIObject.

Comment
Add comment · Show 1 · 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
5

Answer by aphenine · Aug 16, 2016 at 05:04 AM

This is the way (I think) the new EventSystem would like you to do it. (I got stressed figuring this out and there's no easy documentation, so I'm posting it on the stuff I came across to get to this answer):

 using UnityEngine;
 using UnityEngine.EventSystems;
 
     public    void PointerClickHandler( BaseEventData data )
     {
         PointerEventData pointerEventData = data as PointerEventData;
         GameObject pressed = pointerEventData.pointerPress;
 
         //Left click
         if (pointerEventData.button == PointerEventData.InputButton.Left ) {
             if( pointerEventData.clickCount == 1 )
             {
                 OnLeftSingleClick( pressed );
             }
             else if( pointerEventData.clickCount == 2 )
             {
                 OnLeftDoubleClick( pressed );
             }
         }
         //Right click
         else if (pointerEventData.button == PointerEventData.InputButton.Right ) {
             if( pointerEventData.clickCount == 1 )
             {
                 OnRightSingleClick( pressed );
             }
             else if( pointerEventData.clickCount == 2 )
             {
                 OnRightDoubleClick( pressed );
             }
         }
         //Middle click
         else if (pointerEventData.button == PointerEventData.InputButton.Middle ) {
             if( pointerEventData.clickCount == 1 )
             {
                 OnMiddleSingleClick( pressed );
             }
             else if( pointerEventData.clickCount == 2 )
             {
                 OnMiddleDoubleClick( pressed );
             }
         }
     }


Connect that function through an event trigger on the thing you want pressed (in my case, this function is in the camera, because that works for now, and the EventTrigger was on a sphere in the scene)

What's Going On Here

The EventSystem, which is Unity's shiny new method of doing things, sends events out that get captured by EventTrigger, as every tutorial will tell you. So if you put an EventTrigger on your GameObject, it will respond to press and mouse events.

However, what they don't say, is that all the events the EventSystem generates come with event data that the event gets fired with. All the data from EventSystem events subclass BaseEventData.

If you write a function that takes a BaseEventData as a parameter and place it in a script attached to a GameObject, then when you select that GameObject in the EventsTrigger component, it will appear at the top of the available function list in the editor, separate from normal functions talked about here. The EventTrigger will automatically provide that function with stuff that you can then use. That's what I've done in PointerClickHandler(). It handles all Pointer Click events for the EventTrigger I set.

The StandAloneInputModule, which subclasses the PointerInputModule, uses their own pointer events, called the PointerEventData (a subclass of BaseEventData). The contain the good stuff, like what was last pressed, how many clicks there have been and which mouse button was doing the pressing.

The most useful things to know is that pointerPress tells you what GameObject the press was on and clickCount tells you the number of successive clicks within a certain time-out.

Comment
Add comment · Show 1 · 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 SvOzMaS · Apr 18, 2017 at 08:16 AM 0
Share

Very good solution. Now it's very easy to create a superclass with this method and the definition of the "Onclick" methods can be implemented in the childclass hiding all the behaviour.

I had to modify it a little to fit into my project, adding a structure like this:

public struct GameObjectEvent { public BaseEventData baseEventData; public GameObject gameObject; }

And then define the headers of methods with the new type of object (GameObjectEvent).

public abstract void On$$anonymous$$iddleSingleClick(GameObjectEvent goe);

This is usefull if you are interested in get the exact click point coordinates, for example in a point&click game.

Thanks a lot for such a great code snippet.

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

34 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Hide/show UI Button and trigger the OnPointerDown event ?! 0 Answers

How to use the deselect Event Trigger? 1 Answer

Event Trigger doesn't work! 1 Answer

How to create a custom "On..." method? 2 Answers

Event Trigger and Scripting, how to avoid duplicate initialization? 1 Answer


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