Search Unity

[UnityEvent] Static function call. Feature request.

Discussion in 'UGUI & TextMesh Pro' started by Dampire, Sep 29, 2014.

  1. Dampire

    Dampire

    Joined:
    Jan 18, 2014
    Posts:
    7
    Unity 4.6.0b19.
    This code works great.
    Code (CSharp):
    1.  
    2. ...
    3.     [System.Serializable]
    4.     public class ClickEvent : UnityEvent<bool> {}
    5.     public ClickEvent OnClick = new ClickEvent();
    6.  
    7.     void Start()
    8.     {
    9.         image.color = normal;
    10.         image.sprite = Checked?on:off;
    11.         OnClick.AddListener(GameManager.Test);
    12.         OnClick.Invoke(Checked);
    13.     }
    14. ...
    15.  
    But why I can't add this listener via editor? Tried static class with static method, singleton with static method.

    Upd.
    GameManager is not property, it is a class with static method Test.
     
    IbrahimRashwan, turbanov and Nanity like this.
  2. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,225
    We don't have a nice way to hook up statics via the editor currently (we need to store some instance that is the target). We want to address this in the future.
     
    James-Sullivan likes this.
  3. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,188
    Has this been addresses in the meantime? Or is there some other nice way to assign static functions via the inspector?
     
    IbrahimRashwan and turbanov like this.
  4. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    No it hasn't been addressed and still no nice way to assign static functions
     
  5. RyuHayaboosa

    RyuHayaboosa

    Joined:
    Mar 8, 2015
    Posts:
    14
    Any update to this?
     
    IbrahimRashwan likes this.
  6. whogas

    whogas

    Joined:
    Oct 18, 2013
    Posts:
    46
    Just piling on in 2018 that this would be a nice feature! Doesn't seem to be in place as far as I can tell (2017.3)

    Thanks!
     
    IbrahimRashwan likes this.
  7. leveledgames_dev

    leveledgames_dev

    Joined:
    Jul 27, 2016
    Posts:
    44
    We'd like to throw our hat into this ring as well. This thread has been here three and a half years, it could probably use all the support it can get.
     
    IbrahimRashwan likes this.
  8. Wokarol

    Wokarol

    Joined:
    Aug 1, 2017
    Posts:
    34
    Calling static methods would be really helpful. Making ScriptableObjects for each class just to pass function call is irritating.
     
    IbrahimRashwan likes this.
  9. CoderNation1

    CoderNation1

    Joined:
    May 23, 2016
    Posts:
    2
    @Tim-C

    Are we still going to get static calls through unity events?
    It's been awhile...
     
  10. Haapavuo

    Haapavuo

    Joined:
    Sep 19, 2015
    Posts:
    97
    +1 for this! Been waiting for it since 2014 (5 years)!
     
  11. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    Did anyone create a feedback item for it? That's how Unity Technologies wants us to report suggestions:
    https://feedback.unity3d.com

    Once a feedback item exists, people can vote on it and Unity Technologies is able to see it's demand.
     
    IbrahimRashwan likes this.
  12. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    Anyone who is still looking for this functionality should check out my free UltEvents plugin which serves the same purpose as UnityEvents, but has superior features (including the ability to call static methods) with fewer restrictions and an improved Inspector interface that requires fewer clicks to perform individual tasks.
     
  13. James-Sullivan

    James-Sullivan

    Joined:
    Jun 15, 2015
    Posts:
    128
    Adding my +1 for this feature. Is this on the road map?
     
    IbrahimRashwan likes this.
  14. Alvarezmd90

    Alvarezmd90

    Joined:
    Jul 21, 2016
    Posts:
    151
    Lol still nothing? Hilarious! :D
     
    IbrahimRashwan likes this.
  15. Agent000

    Agent000

    Joined:
    Feb 12, 2015
    Posts:
    9
    I'll just bump this thread again and agree this would be a nice feature.
     
    IbrahimRashwan likes this.
  16. GeorgeAdamon

    GeorgeAdamon

    Joined:
    May 31, 2017
    Posts:
    48
    It would be a very useful feature indeed! +1
     
    IbrahimRashwan likes this.
  17. Natpokey

    Natpokey

    Joined:
    Oct 8, 2018
    Posts:
    2
    I would love to have this feature to help reference functions inside singletons. For now, I just reference a non-static function in a different script that then calls the static method inside a singleton, which makes me cringe in pain every time I see either function.
     
    IbrahimRashwan likes this.
  18. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    Someone else at Unity did static functions for UnityEvents, I'm not sure where it landed check out 19.3 maybe its there...
     
  19. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,188
    That sounds like it's already there or soon to be. Nice!

    That's a problem a lot of users have, I guess. It's very difficult to find specific changes in the release notes because there's no official search functionality other than Google. Also, personally, I have a feeling that a lot of small changes and additions to the API are missing from the big release notes. For example, the ConfigObject API in EditorBuildSettings and a couple of other things like callbacks to add into the Scene context menu have totally slipped my eye although I always read through the release notes.
     
    IbrahimRashwan likes this.
  20. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    Yeah i have the same issue if not a little worse, i struggle at times to keep what feature releases when as i can't always make a cut off so the feature needs to wait, but then as it was done 2-3 months ago (depending on how close to the cut off it was) if feels like it should be in a earlier version then it actually is. This particular features is worse as even though my team owns UnityEvents we didnt do the improvement so its another layer of tracking that got lost.

    If you really want to know a firm version i can try to dig through old notes and see.
     
  21. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,188
    Please do! It's one of those long requested awesome features, but it doesn't seem to be in 19.3.

    Edit: Nevermind! I just found-out that I can call a static function via UnityEvent in 19.3, but it's not the feature I and probably most of the users in this thread were hoping for. UnityEvent still requires an object reference set as a target. So this means, I can drag and drop one of my MonoBehaviour components into the target field and then select one of its static functions, but that only solves part of what we would like it to do. A static function exists without the need for any instances of the class, so it makes no sense to connect them in this way. Also, most of the time, if a function is static, it's very likely that the entire class is static, for example, we would want to call Debug.Log from UnityEvent.
     
    Last edited: Feb 5, 2020
    IbrahimRashwan likes this.
  22. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    Sorry for the delay had to build a new machine.

    So i think you can do what your asking via code. I think that likely the inspector wasn't updated to look for pure static methods without the object reference. Looking at the code when it goes to call the static function it doesnt use the target object. I think it was left that way on purpose to make finding functions easier.
     
  23. sharpshot124

    sharpshot124

    Joined:
    Apr 7, 2012
    Posts:
    18
    Would it be possible to utilize the script itself as the reference? As of now a script reference is referenced as a MonoScript by the event drawer. But Since the UnityEventDrawer uses Reflection to populate the fields, adding a special case to reference static members of a script should be realistically possible with a direct reference to the code asset.

    This isn't necessarily the reference-less feature most are asking for BUT it would allow us to make function calls without an object instance which is a huge feature imo.
     
    tobsam, IbrahimRashwan and Engborg like this.
  24. Engborg

    Engborg

    Joined:
    Jun 12, 2019
    Posts:
    3
    I second this, would make the project a lot cleaner not having to have an object just to trigger a static function(ironic) from a UnityEvent. Code asset should definitely be enough.
     
    tobsam and IbrahimRashwan like this.
  25. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    Code assets don't exist at runtime so that won't work.
     
    IbrahimRashwan likes this.
  26. Engborg

    Engborg

    Joined:
    Jun 12, 2019
    Posts:
    3
    The static class still exists, you would use the code asset to get relevant information to trigger said static function in runtime. I'm probably overlooking something here but nevertheless it would be a nice feature to have and I'm sure someone smarter than me at Unity could figure it out.
     
    IbrahimRashwan likes this.
  27. IbrahimRashwan

    IbrahimRashwan

    Joined:
    Oct 21, 2018
    Posts:
    4
    Any updates to this feature? Would be a very nice one to have ....

    Also functions should appear if they have one required paratemer if the others are optional we can call them with thieir defaults (ignore them basicly) i guess this should be in there too.
     
    Last edited: Dec 9, 2021
  28. billw2011

    billw2011

    Joined:
    Apr 5, 2014
    Posts:
    15
    Would like this also. Scriptable objects don't make much sense when you aren't putting data in them, a static class is easier, and the static class even shows up selectable as a target for events, but I can't see my static functions listed.
     
  29. gooby429

    gooby429

    Joined:
    Aug 13, 2019
    Posts:
    135
    This actually does make sense, i use scriptable objects for events and stuff like this all the time. You can assign them to unity's UI events and call w/e function you need!

    For example, I use them for triggering hover/click sounds. I have a button as a prefab that i can customize. I add the onClick/Hover event listeners to it. Then i reference a scriptable object in the fields (AudioManagerObject) and call the method (PlayHoverSound) that calls the static AudioManager.PlayHoverSound.

    it would be nice if we could just straight up use static classes as references but this is just one little extra step to make it work
     
  30. dccoo

    dccoo

    Joined:
    Oct 3, 2015
    Posts:
    161
    9 years already and there's no simple way to have a way to reference a static function in inspector yet... there was a lot of times I wanted to do this to avoid creating useless stuff just to drag and call non-static methods.
    Please make this happen!
     
  31. tobsam

    tobsam

    Joined:
    Aug 23, 2017
    Posts:
    3
    How on earth is this still not a feature?
    This regularly creates the need for a useless additional script that just forwards events...
    Whenever you assign UnityEvents through the inspector, you want to be able to run scene independent tasks such as printing a Debug.Log(). In the current state this will always need to happen through an instance of a class that is defined within the scene before runtime...

    My use-case:
    I have a button script that has UnityEvents for OnDown, OnUp and OnClick.
    I have a Singleton called AudioManager that is scene independent and persistent.
    I have a button prefab in which i want to make the button events call a method inside the AudioManager.
    And i absolutely expected to be able to just drag the AudioManager asset in and select either the Singleton Instance or at least a static method...
     
    Last edited: Jul 1, 2023
    dccoo likes this.
  32. WryMim

    WryMim

    Joined:
    Mar 16, 2019
    Posts:
    69
    Alas, as usual, only promises
     
    joey_unity967 likes this.