Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How can I get an Event widget in my own inspectors?

Discussion in 'UGUI & TextMesh Pro' started by StarManta, Sep 24, 2014.

  1. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    The thing in the screenshot I've attached - how can I get it in my own scripts?
     

    Attached Files:

  2. ChoMPi

    ChoMPi

    Joined:
    Jul 11, 2013
    Posts:
    112
  3. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,158
  4. ChoMPi

    ChoMPi

    Joined:
    Jul 11, 2013
    Posts:
    112
    To be more specific, you need to define your event and draw the property of the event in an editor script.

    Example MonoBehaviour:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.Events;
    4. using UnityEngine.EventSystems;
    5. using System;
    6. using System.Collections;
    7.  
    8. namespace UnityEngine.UI
    9. {
    10.     public class Example : MonoBehaviour {
    11.    
    12.         [Serializable]
    13.         public class ExampleEvent : UnityEvent {}
    14.    
    15.         public ExampleEvent onEvent = new ExampleEvent();
    16.     }
    17. }
    Example editor script:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using UnityEditor;
    5.  
    6. namespace UnityEditor.UI
    7. {
    8.     [CustomEditor(typeof(Example))]
    9.     public class ExampleEditor : Editor {
    10.    
    11.         public override void OnInspectorGUI()
    12.         {
    13.             this.serializedObject.Update();
    14.             EditorGUILayout.PropertyField(this.serializedObject.FindProperty("onEvent"), true);
    15.             this.serializedObject.ApplyModifiedProperties();
    16.         }
    17.     }
    18. }
     
    Last edited: Sep 29, 2014
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    The UnityEvent class was the magic word I was looking for, thanks :)

    I just tried something, and it turns out, that's definitely overcomplicating it assuming you don't need to do anything super-complicated with them. You don't need an Editor script, nor do you really need to derive from UnityEvent.

    This is basically all I needed to do:

    Code (csharp):
    1.  
    2. public class SomeComponent : MonoBehaviour {
    3. public UnityEvent OnPressButtonA;
    4.  
    5. void Update() {
    6. if (Input.GetKeyDown("a") ) OnPressButtonA.Invoke();
    7. }
    8. }
    9.  
    This works and displays the event widget in the inspector as desired.
     
    Baroni likes this.
  6. ChoMPi

    ChoMPi

    Joined:
    Jul 11, 2013
    Posts:
    112
    I guess the topic title mislead me, i thought you already have an editor script and was wondering how to draw the even property...

    Anyways, im glad you found what you ware looking for.
     
  7. Breyer

    Breyer

    Joined:
    Nov 10, 2012
    Posts:
    412
    BTW if u need extend UnityEvent inspector you could derive from UnityEventDrawer class which behave like custom propertydrawer. That way i created unity timer
     
    lermy3d likes this.
  8. lermy3d

    lermy3d

    Joined:
    Mar 16, 2014
    Posts:
    101
    Hello! Does anybody could draw parameters for the UnityEvent with your own custom editor or drawer, I was looking for the way of adding several parameters to the UI UnityEvents in the inspector but without avail.

    If anyone has already achieved this already with UnityEvents please share, I already was using several parameters when I was using NGUI's EventDelegate but it seems Unity did not take into account having a list of parameters for each event. Right now the UnityEvent editor only shows in the inspector functions with one parameter only... :(

    If anyone is interested there is a cool debate on this thread.
     
  9. crafTDev

    crafTDev

    Joined:
    Nov 5, 2008
    Posts:
    1,818
    I have been looking at this but I don't see where you defined serializedObject.

    Thanks,
    jrDev
     
    Michael_Berna likes this.
  10. hihihihihihihihihi

    hihihihihihihihihi

    Joined:
    Jan 22, 2018
    Posts:
    1
    it helps me so much, thx