Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

[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.
     
    turbanov and Nanity like this.
  2. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,158
    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:
    753
    Has this been addresses in the meantime? Or is there some other nice way to assign static functions via the inspector?
     
    turbanov likes this.
  4. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,173
    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?
     
  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!
     
  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.
     
  8. Wokarol

    Wokarol

    Joined:
    Aug 1, 2017
    Posts:
    26
    Calling static methods would be really helpful. Making ScriptableObjects for each class just to pass function call is irritating.
     
  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...
     
    James-Sullivan and Haapavuo like this.
  10. Haapavuo

    Haapavuo

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

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    5,135
    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.
     
  12. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    1,159
    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.
     
    funbites and Wokarol like this.
  13. James-Sullivan

    James-Sullivan

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

    Alvarezmd90

    Joined:
    Jul 21, 2016
    Posts:
    144
    Lol still nothing? Hilarious! :D
     
  15. Agent000

    Agent000

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

    GeorgeAdamon

    Joined:
    May 31, 2017
    Posts:
    28
    It would be a very useful feature indeed! +1
     
  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.
     
  18. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,173
    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:
    753
    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.
     
  20. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,173
    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:
    753
    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
  22. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,173
    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.
     
unityunity