Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ScottYann · Oct 28, 2016 at 09:50 PM · webglreflectiondelegates

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!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Naphier · Oct 29, 2016 at 04:20 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Naphier · Oct 30, 2016 at 06:33 PM 0
Share

Glad you figured it out. Can't wait until Unity and $$anonymous$$ono get some more up to date c# libraries!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

62 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Screen Space Reflection in WebGL? 2 Answers

Do reflection probes work in WebGL? 0 Answers

How to get blurred mirror effect in webGL? 1 Answer

WebGL reflection cubemap artifact on mesh 0 Answers

Finding a method by custom attribute and passing to a UI button 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges