I’m using C# events in my game, but they don’t seem to work on iOS. It fails to attach a handler to the event, after the += call the event slot still is null. It works in editor, web player and Android, only fails on iOS. Any suggestions to make it work with events, or I have to re-write it with interfaces?
Events do work on iOS. Though there might be corner cases (like combining them with generics) when they don’t work. If you provide some sample code it would be easier to tell what’s going wrong.
Thanks, the case is just that - combined with generics. I used a HashSet of delegates to send the events to and it works now. I wonder why on Android events works with generics and on iOS they don’t.
Long story short: Android uses JIT (just in time compilation) and iOS uses AOT (ahead of time compilation). AOT design requires all code paths to be known ahead of time, but sometimes it’s not possible.
Thanks for the smart answer!
I have something like that.
The Clicked event should be known before compilation, shouldn’t it ?
xCode and the application crashes at the first Clicked hook-up
(anyway, I will just remove generics, if I really need…)
(NB: that’s not a real MVC pattern, it’s a kind of MVC pattern for webservices ^^')
///
/// Base class for every View
///
/// View type
/// Controller type
[RequireComponent(typeof(RectTransform))]
public abstract class View<TView, TCtl> : ApplicationBehaviour
where TView : View<TView, TCtl>
{
///
/// View controller
///
protected TCtl controller;
///
/// View main button
///
protected Button ViewButton;
///
/// Event triggered when the view is clicked
///
public event Action<TView, TCtl> Clicked;
As a quick fix, I will use
#if AOT
public event Action<object,object> Clicked
#else
public event Action<TView, TCtl> Clicked
#endif
and cast the hook-ups parameters (errors can be checked with removing AOT define symbol)
The other solution is to break the legacy… I don’t want that.
Edit: seems it doesn’t work at all in a generic class, in fact…