• 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 /
This question was closed Jan 02, 2015 at 01:17 PM by Landern for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by J-Roossien · Dec 18, 2014 at 05:37 PM · uibuttonsevent4.6

4.6 UI Dynamic button event system PointerEnter, PointerExit, PointerUp etc.

I'm trying to make a dynamic inventory system with the new UI. The issue I have with the new UI is the way buttons work and that you have to set them up inside the editor. I have tried several methods but not$$anonymous$$ng seems to be working properly.

With my inventory system I can just add slots wit$$anonymous$$n the editor. Each slot has a script to specify the slot ID and type. I made the entire inventory system with items and a editor to set up items and saving etc. I just need to make the InventoryControl script but I'm having lots of trouble with it. The system uses a lot components and everyt$$anonymous$$ng works on it's own and everyt$$anonymous$$ng is really dynamic with only a few settings.

I don't wanna have to set up a EventTrigger and set them up for each slot separate. I found t$$anonymous$$s http://gamedev.stackexchange.com/questions/83027/unity-4-6-gui-dynamically-change-the-functions-called-by-gui-elements but the problem with it is that it doesn't work properly for other events then pointerdown. The object you get with baseEvent.selectedObject is Null for the other events.

So what I basically need is a script I can attach to all slots that will detect mouse events on the 4.6 UI button. Then I can just dispatch a event with the mouse event and the slot and use that in my InventoryControl script. Another good alternative is just 1 script that detects mouse events on all UI buttons but I need to be able to get the gameobject with it and with the code in the link above that doesn't work.

The only option I haven't tested and found online is using a ray to detect it. But that seems inefficient and a terrible way to do t$$anonymous$$s.

T$$anonymous$$s is the broken code from the link above modified to work with my event listener and stuff. The baseEvent.selectedObject is Null for all Pointer events.

 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.EventSystems;
 using System.Collections;
 
 public class ButtonEvent : MonoBehaviour {
 
     private EventTrigger eventTrigger;
 
     void Start() {
         eventTrigger = GetComponent<EventTrigger>();
 
         if (eventTrigger == null) {
             return;
         }
 
         setupEventListener(EventTriggerType.PointerClick, new UnityEngine.Events.UnityAction<BaseEventData>(ClickEvent));
         setupEventListener(EventTriggerType.PointerDown, new UnityEngine.Events.UnityAction<BaseEventData>(PointerDownEvent));
         setupEventListener(EventTriggerType.PointerUp, new UnityEngine.Events.UnityAction<BaseEventData>(PointerUpEvent));
         setupEventListener(EventTriggerType.PointerEnter, new UnityEngine.Events.UnityAction<BaseEventData>(PointerEnterEvent));
         setupEventListener(EventTriggerType.PointerExit, new UnityEngine.Events.UnityAction<BaseEventData>(PointerExitEvent));
     }
 
     private void setupEventListener(EventTriggerType eventID, UnityEngine.Events.UnityAction<BaseEventData> listener) {
         EventTrigger.Entry entry = new EventTrigger.Entry();
         entry.eventID = eventID;
         entry.callback = new EventTrigger.TriggerEvent();
 
         entry.callback.AddListener(listener);
         eventTrigger.delegates.Add(entry);
     }
 
 
     public void ClickEvent(BaseEventData baseEvent) {
         EventListener.dispatch<GameObject>("ui_click", baseEvent.selectedObject);
     }
 
     public void PointerDownEvent(BaseEventData baseEvent) {
         EventListener.dispatch<GameObject>("ui_pointerdown", baseEvent.selectedObject);
     }
 
     public void PointerUpEvent(BaseEventData baseEvent) {
         EventListener.dispatch<GameObject>("ui_pointerup", baseEvent.selectedObject);
     }
 
     public void PointerEnterEvent(BaseEventData baseEvent) {
         EventListener.dispatch<GameObject>("ui_pointerenter", baseEvent.selectedObject);
     }
 
     public void PointerExitEvent(BaseEventData baseEvent) {
         EventListener.dispatch<GameObject>("ui_pointerexit", baseEvent.selectedObject);
     }
     
 }
Comment
Add comment · Show 1
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 J-Roossien · Jan 02, 2015 at 01:17 PM 0
Share

The answer below from BoredMormon works perfectly. Just implement the interfaces of the events you need and override the methods.

1 Reply

  • Sort: 
avatar image
4
Best Answer

Answer by Kiwasi · Dec 18, 2014 at 05:45 PM

I just finished a youtube video on t$$anonymous$$s.

The key solution you are after is implementing the event system via script. Add using UnityEngine.EventSystems to the top of your script. Then implement the interfaces from there. T$$anonymous$$s eliminates the need to add listeners altogether. For t$$anonymous$$s to work it should be attatched to the actual UI element detecting the events.

 public class ButtonEvent : MonoBehaviour, IClickHandler, IPointerEnterHandler {

http://docs.unity3d.com/Manual/SupportedEvents.html

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 J-Roossien · Dec 18, 2014 at 10:17 PM 0
Share

Thanks a lot for this. I didn't know about those interfaces and I think I can make something with this. I will try it out tomorrow :)

avatar image c_andrews · Feb 06, 2015 at 11:41 AM 0
Share

Thanks a lot, this was perfect and a lot easier than adding the EventTrigger and events dynamically.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Getting on screen sizes of a UI element in Unity 4.6 (while using CanvasScaler),Get the real size on screen of an UI element in Unity 4 0 Answers

4.6 UI "image" is capturing clicks - how to prevent? 3 Answers

Scrolling Scroll Rect with Buttons 4 Answers

Infinite ScrollRect? 1 Answer

HELP! Buttons stop working when made a child of a "Canvas Group" empty in 4.6.3f1? 4 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