Search Unity

Do UnityEvents and SendMessage use reflection under the hood?

Discussion in 'Scripting' started by hoesterey, Mar 30, 2017.

  1. hoesterey

    hoesterey

    Joined:
    Mar 19, 2010
    Posts:
    659
    Hi,

    We just started moving a project over to a platform that does not support reflection. Do UnityEvents and SendMessage events use reflection under the hood or are they generating some sort of table/dictionary at build time?

    Thanks!
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    As I understand it UnityEvents are simply an inspector friendly wrapper for vanilla C# events.

    SendMessage uses some sort of arcane magic that may involve reflection. But as far as I'm aware it's supported across all platforms. But in any case there is no real reason to use SendMessage.

    Build a quick test project and find out.
     
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,532
    If the Unity API is available on a platform, then it should work (within reason, bugs may occur and unity would want to fix them). There are platform considerations to make:
    https://docs.unity3d.com/Manual/CrossPlatformConsiderations.html

    WebGL is the most limited in support and has limitations:
    https://docs.unity3d.com/Manual/webgl-browsercompatibility.html

    Aside from that, all platforms should fully support the unity API.

    It's the mono/.net api that may have restrictions to an extent. Different platforms have different parts locked out.

    I can't find the unity 5 compatability list, but here's the unity 4 one. The runtime hasn't really changed much (though will be very soon as unity is currently beta testing updating the runtime), so this list for the most part is semi-accurate. The webplayer being an exception since that is no longer suppported and is replaced by webgl:
    https://docs.unity3d.com/401/Documentation/ScriptReference/MonoCompatibility.html

    Do note, reflection is actually supported for the most part across platforms. Some methods don't exist, but have alternate broader methods. Really the one big thing that is not supported is 'System.Reflection.Emit' which allows generating IL code at runtime (basically generating dynamic code)... which isn't supported on platforms like iOS because it's all AOT.

    Basically the rule isn't that you can't reflect information about a type... you certainly can. You can't use that information to create a new type.

    For example, if you used reflection to define a type like List<SomeType>, but never explicitly referenced this generic type... only implicitly through reflection described the potential of this type (AOT won't know to create the class unless it's explicitly stated somewhere).. then you used Activator to create an instance of it... that would crash, because the class doesn't exist, and the JIT would have to create it, but the JIT is not available.
     
    Meri, Kiwasi and hoesterey like this.
  4. hoesterey

    hoesterey

    Joined:
    Mar 19, 2010
    Posts:
    659
    Really good info. Thanks,
    I no longer injecting coffee into my veins in prep for a week of long nights. :)