- Home /
Problem with delegates inside of a WebGL player?
Hello,
I have inherited a project that was originally completed in 2008 that had it's own custom GUI layout system. Amazingly it still kind of works in the most recent version of Unity. It uses a delegate system for most of its internal widget communications. In the editor and standalone players it works great, nothing wrong at all. We need it to run in the WebGL player and this bit of code is holding up the whole project because it is complaining of a null reference exception. I have verified that it isn't being fed nulls as my prints prove I think. I've read that Unity's WebGL implementation doesn't support System.Reflection.Emit. I have actually never used reflection so I am unable to determine if the following lines of code use the Emit part or not:
List<HandlerInfo> CreateWidgetEventHandlers (Widget w)
{
var t = w.GetType ();
var handlers = new List<HandlerInfo> ();
//if (Application.platform != RuntimePlatform.IPhonePlayer && Application.platform != RuntimePlatform.WebGLPlayer)
//{
var events = t.GetEvents ().Where (e => e.DeclaringType == t);
foreach (var e in events) {
var h = new HandlerInfo { Widget = w, Event = e, UI = this };
var et = e.EventHandlerType;
var mi = et.GetMethod ("Invoke");
var args = mi.GetParameters ();
var b = new WidgetEventBroadcaster () { Handler = h };
if (args.Length == 0)
h.Handler = Delegate.CreateDelegate (et, b, "Go0");
else if (args.Length == 1) {
var pt = args [0].ParameterType;
var gmi = typeof(WidgetEventBroadcaster).GetMethod ("Go1");
var method = gmi.MakeGenericMethod (new[] { pt });
h.Handler = Delegate.CreateDelegate (et, b, method);
} else
continue;
print ("e " + e + "w " + w + " h.handler " + h.Handler);
e.AddEventHandler (w, h.Handler);//this is the problem here. it complains of a null reference though these parameters are definately not null.
}
//}
return handlers;
}
w ViewControl (Crucible.UI.Widgets.RepeatButton) h.handler Crucible.UI.Widgets.RepeatButton+MouseDragEventHandler UnityEngine.MonoBehaviour:print(Object) Crucible.UI.UserInterface:CreateWidgetEventHandlers(Widget) Crucible.UI.UserInterface:RegisterNewWidget(Widget) Crucible.UI.WidgetHandler:Invoke(Widget) Crucible.UI.Layout:RegisterWidgets(Widget) Crucible.UI.Layout:Start() Crucible.UI.UserInterface:Awake()
NullReferenceException: A null value was found where an object instance was required. System.Reflection.EventInfo.CreateAddEventDelegate (System.Reflection.MethodInfo method) System.Reflection.EventInfo.AddEventHandler (System.Object target, System.Delegate handler) Crucible.UI.UserInterface.CreateWidgetEventHandlers (Crucible.UI.Widget w) Crucible.UI.UserInterface.RegisterNewWidget (Crucible.UI.Widget w) Crucible.UI.Layout+WidgetHandler.Invoke (Crucible.UI.Widget w) Crucible.UI.Layout.RegisterWidgets (Crucible.UI.Widget root) Crucible.UI.Layout.Start () Crucible.UI.UserInterface.Awake ()
If you notice there is a condition commented out to prevent the code from executing on iPhone or WebGL. Originally it was to block only iPhone, I put in WebGL in there so that it would execute and get me to the point where I could see the interface. Otherwise on WebGL when it runs, it bombs and the errors prevent the drawing of the screen. iPhone doesn't like anything but AOT compilation just like WebGL so I figure the problem must be related to those two platforms. Unfortunately, I do need this code to work because the buttons will do nothing without it.
This is a really obscure issue I think. If you have any thoughts please share in comments and if you have a solution, I'd love to read it. Thank you!
Neat, but not cool it doesn't work in WebGL. I bet it's a Reflection issue that no one's really hit before. I don't see "e" in your console statement, maybe you left it out? I'm surprised Linq even works in WebgL :P
I'm a bit baffled as to what's actually going on here. $$anonymous$$aybe you could try rewriting this to use UnityActions or UnityEvents? I'm not sure why you're using reflection, so it's hard to tell if that would help (maybe you're accessing some native library). Is there some equivalent JS that you could do this with?
Sorry that might not help at all, but I find this interesting and maybe some idea bouncing will help.
Answer by ScottYann · Oct 29, 2016 at 09:27 PM
The problem was indeed system.reflection.emit
It was hard to tell because in the code I got the keyword emit occurs no where in it and the error itself didn't mention 'emit'.
The two problematic issues in my script were the getmethod and makegenericmethod usages. It didn't like being passed strings. It was able to deal with delegates just fine so I did this instead:
if (e.EventHandlerType.ToString () == "Crucible.UI.Widgets.Button+ClickedEventHandler") {
Crucible.UI.Widgets.Button aButton = (Crucible.UI.Widgets.Button)w as Crucible.UI.Widgets.Button;
myWidgetBroadcaster.Handler = h;
aButton.Clicked += myWidgetBroadcaster.Go0;
}
if (e.EventHandlerType.ToString () == "Crucible.UI.Widgets.RepeatButton+MouseDownEventHandler") {
Crucible.UI.Widgets.RepeatButton aButton = (Crucible.UI.Widgets.RepeatButton)w as Crucible.UI.Widgets.RepeatButton;
myWidgetBroadcaster.Handler = h;
aButton.MouseDown += myWidgetBroadcaster.Go0;
}
if (e.EventHandlerType.ToString () == "Crucible.UI.Widgets.RepeatButton+MouseUpEventHandler") {
Crucible.UI.Widgets.RepeatButton aButton = (Crucible.UI.Widgets.RepeatButton)w as Crucible.UI.Widgets.RepeatButton;
myWidgetBroadcaster.Handler = h;
aButton.MouseUp += myWidgetBroadcaster.Go0;
}
if (e.EventHandlerType.ToString () == "Crucible.UI.Widgets.RepeatButton+MouseDragEventHandler") {
Crucible.UI.Widgets.RepeatButton aButton = (Crucible.UI.Widgets.RepeatButton)w as Crucible.UI.Widgets.RepeatButton;
myWidgetBroadcaster.Handler = h;
aButton.MouseDrag += myWidgetBroadcaster.Go1;
}
if (e.EventHandlerType.ToString () == "Crucible.UI.Widgets.Toggle+ChangeHandler") {
Crucible.UI.Widgets.Toggle aToggle = (Crucible.UI.Widgets.Toggle)w as Crucible.UI.Widgets.Toggle;
myWidgetBroadcaster.Handler = h;
aToggle.Changed += myWidgetBroadcaster.Go1;
}
if (e.EventHandlerType.ToString () == "Crucible.UI.Widgets.Toggle+ClickHandler") {
Crucible.UI.Widgets.Toggle aToggle = (Crucible.UI.Widgets.Toggle)w as Crucible.UI.Widgets.Toggle;
myWidgetBroadcaster.Handler = h;
aToggle.Clicked += myWidgetBroadcaster.Go0;
}
Thank you Naphier for your participation.
Glad you figured it out. Can't wait until Unity and $$anonymous$$ono get some more up to date c# libraries!