Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Sorade · Apr 21, 2017 at 05:55 PM · inspectoreventeventsystem

Adding custom BaseEventData to UnityEngine.EventSystems NOT Unity.Events

Hi all,

I am currently trying to use the UnityEngine.EventSystems (developed for the UI but which also has a 3D Physics Raycaster) in a game. The reason I want to use it is to make use of all the already available (an handy) editor features of this system over the UnityEngine.Events.

However, the system has been developed for the UI and I would need to be able to add events to the system which trigger on certain game actions.

For example, in the EventTrigger component I would like to see in the dropdown menu a "OnUKeyPressed" events in addition to all the others (eg: PointerClick).

There is a wealth of pretty good tutorials out there for the UnityEngine.Events but not that much for the UnityEngine.EventSystems.

Any help would be much appreciated !

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 ExtinctSpecie · Apr 21, 2017 at 08:52 PM 0
Share

so you want a custom event like button has "OnClick" you want your own am i right ?

avatar image Sorade ExtinctSpecie · Apr 21, 2017 at 09:17 PM 0
Share

Yes, for example I would like an "OnU$$anonymous$$eyPressed" event to register when the U key is pressed when the mouse is over an object which would trigger the Use() method on the object.

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by ExtinctSpecie · Apr 21, 2017 at 11:05 PM

as i understood from your comment you need something that can register events and use them

ill show you 2 examples try to play with them since i dont have the time

//first example

  using UnityEngine;
  using System.Collections;
  using System;
  using UnityEngine.Events;
  using UnityEngine.EventSystems;
  
  
  public class MyClickTrigger : MonoBehaviour , IPointerClickHandler
  {
      #region IPointerClickHandler implementation
  
      public void OnPointerClick (PointerEventData eventData)
      {
          MyOwnEventTriggered ();
      }
  
      #endregion
  
      //my event
      [Serializable]
      public class MyOwnEvent : UnityEvent { }
  
      [SerializeField]
      private MyOwnEvent myOwnEvent = new MyOwnEvent();
      public MyOwnEvent onMyOwnEvent { get { return myOwnEvent; } set { myOwnEvent = value; } }
  
      public void MyOwnEventTriggered()
      {
          onMyOwnEvent.Invoke();
      }
  
  }
  ///add a collider to the object as well so the OnPointerClick can work



//second example

create a script like this one

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI.Extensions;
 
 using UnityEngine.Events;
 
 
 
 public class MyEventManager : MonoBehaviour {
 
 
     private Dictionary<string,UnityEvent> eventDictionary;
     private static MyEventManager eventManager;
 
     public static MyEventManager instance
     {
         get
         {
             if (!eventManager)
             {
                 eventManager = FindObjectOfType (typeof (MyEventManager)) as MyEventManager;
 
                 if (!eventManager)
                 {
                     Debug.LogError ("There needs to be one active EventManger script on a GameObject in your scene.");
                 }
                 else
                 {
                     eventManager.Init (); 
                 }
             }
 
             return eventManager;
         }
     }
     void Init ()
     {
         if (eventDictionary == null)
         {
             eventDictionary = new Dictionary<string, UnityEvent>();
         }
     }
     public static void StartListening (string eventName, UnityAction listener)
     {
         UnityEvent thisEvent = null;
         if (instance.eventDictionary.TryGetValue (eventName, out thisEvent))
         {
             thisEvent.AddListener (listener);
         } 
         else
         {
             thisEvent = new UnityEvent ();
             thisEvent.AddListener (listener);
             instance.eventDictionary.Add (eventName, thisEvent);
         }
     }
 
     public static void StopListening (string eventName, UnityAction listener)
     {
         if (eventManager == null) return;
         UnityEvent thisEvent = null;
         if (instance.eventDictionary.TryGetValue (eventName, out thisEvent))
         {
             thisEvent.RemoveListener (listener);
         }
     }
 
     public static void TriggerEvent (string eventName)
     {
         UnityEvent thisEvent = null;
         if (instance.eventDictionary.TryGetValue (eventName, out thisEvent))
         {
             thisEvent.Invoke ();
         }
     }
 }


and then you register an event this way

 public class LastPageTriggers : MonoBehaviour {
 
     void OnEnable()
     {
         MyEventManager.StartListening ("lastPage",LastPage);
 
     }
     void OnDisable()
     {
         MyEventManager.StopListening ("lastPage", LastPage);
     }
     void LastPage()
     {
         Debug.Log ("Last Page function");
     }
     public static void TriggerLastPage()
     {
         MyEventManager.TriggerEvent ("lastPage");
     }
 }
 
 //do not forget to stop listening on disable
 //if you want to invoke the method LastPage from anywhere you just call //the static method of TriggerLastPage
 //so somewhere else you would have
 
     void Update () {
 
         if (Input.GetKeyDown ("u"))
         {
             MyEventManager.TriggerEvent ("lastPage");
 //or LastPageTriggers.TriggerLastPage();
         }
 
     }

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 Sorade · Apr 22, 2017 at 08:09 AM 0
Share

Cheers thanks !

I like it but what I am really trying to achieve is the following:

alt text

As you can see I want to be able to literally add a customise event to the Unity EventSystem and be able to access it along with all the others in the drop down menu.

$$anonymous$$aybe this is not possible or too complicated, but I would thing that it is easier that making a event system from scratch.

I suppose I will have to trigger my event independently from where the events from the EventSystem are triggered (I assume using the BaseInput$$anonymous$$odule) but I would still be able to find a way to feed in a new event into the system.

unity-capture.png (66.2 kB)
avatar image ExtinctSpecie · Apr 22, 2017 at 10:57 AM 0
Share

i dont know if thats possible i've only seen that in swift language where you can create extensions what you can do tho is create a script called OnU$$anonymous$$eyPressed with an update function that checks for "u" if pressed and then invoke the method

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

70 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

Related Questions

How i can subscribe to an Event through Inspector 0 Answers

How to capture onBeginDrag() onEndDrag() events for screen,Capture UI events onDrag for screen not for object 0 Answers

EventSystem not recognizing child objects 0 Answers

How to call an implemented custom Interface 1 Answer

The type or namespace name 'EventSystems' does not exist in the namespace 'UnityEngine' 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