Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 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 KurtisWeb · Jun 06, 2019 at 06:49 PM · eventsysteminterface

Using IPointerDownHandler to check what object was pressed

Hello, I have used IPointer methods before however I have only ever used them on the object which is being pressed, so I can tell which object is being interacted with. I am now implementing an interface into my project called IInteractable which will handle all interactions from the player, and there is a class called Interactable which implements this interface. I have another class called Player_Input which deals with all player input and this is where IPointer methods are used. My problem is that I need to find out what object is being pressed with IPointerDownHandler() from Player_Input so I can call the necessary methods on it if it implements IInteractable. I might not have explained very well but here are my scripts:


Player_Input

 using UnityEngine;
 using UnityEngine.EventSystems;
 
 public class Player_Input : MonoBehaviour, IPointerDownHandler
 {
 
     public virtual void OnPointerDown(PointerEventData eventData)
     {
         //check if clicked object implements IInteractable, then call OnPointerDown on that script
     }
 
 }


Interactable

 using UnityEngine;
 
 public class Interactable : MonoBehaviour, IInteractable
 {
 
     public Interactable_Type interactableType;
 
     public void OnPointerDown()
     {
         switch(interactableType)
         {
             case Interactable_Type.button:
                 Debug.Log("Button Pressed");
                 break;
         }
     }
     
 }
 

Comment
Add comment · Show 2
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 MacDx · Jun 06, 2019 at 11:17 PM 0
Share

So.. what you want for your code, correct me if I'm wrong, is to know which object was clicked, when you are inside the OnPointerDown method of the Player_Input class?

avatar image KurtisWeb MacDx · Jun 07, 2019 at 06:14 AM 0
Share

Yes that is exactly right

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Kennai · Jun 07, 2019 at 08:31 AM

Hi, @KurtisWeb ! Maybe so?

Player_Input

  using UnityEngine;
  public class Player_Input : MonoBehaviour
  {
      public static void CustomClick(Interactable obj)
      {
          switch(obj.interactableType)
          {
               case Interactable_Type.button:
                       Debug.Log("Button pressed");
                       break;
          }
      }
  
  }

Interactable

  using UnityEngine;
  using UnityEngine.EventSystems;
  
  public class Interactable : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler
  {
  
      public Interactable_Type interactableType;
 
      public void OnPointerDown(PointerEventData pointerEventData)
      { 
             Player_Input.CustomClick(this);
      }
 
      public void OnDrag(PointerEventData eventData)
      { }
 
      public void OnPointerUp(PointerEventData pointerEventData)
      { }
  }

p.s. You need to implement all three methods OnPointerDown, OnDrag, OnPointerUp, otherwise it wont work correctly.

Comment
Add comment · Show 2 · 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 KurtisWeb · Jun 07, 2019 at 12:47 PM 0
Share

How is CustomClick() detecting input?

avatar image Kennai KurtisWeb · Jun 10, 2019 at 09:16 AM 0
Share

CustomClick would be called from Interactable.OnPointerDown it means when you click on Interactable object, it will trigger OnPointerDown. And OnPointerDown will "invoke" Player_Input.CustomClick.

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

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

Related Questions

uUI - OnSelect: From Mouse/Pointer or Keyboard/Controller? 0 Answers

Use first person camera to trigger IPointer events,Lock mouse without disabling mouse event triggers 1 Answer

How to offset Pointer in EventSystem Interface 0 Answers

Canvas-prefab elements are not interactable 2 Answers

How to call an implemented custom Interface 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