Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Zephrim · Oct 04, 2014 at 06:14 PM · reflection4.6listenerpersistentselectable

Unity 4.6 - Editor Scripting - Add Reflection Method as a Persistent Listener

What am I trying to do?

I'm current trying to use an EditorWindow to quickly add all of the OnClick Events to my Selectable components. Rather then me having to do it all manually.

I'm using UnityEventTools and AddPersistentListener, but I'm running into a few issues when I try to add a Reflection Method as a PersistentListener.

Error

 ArgumentException: Could not register callback <>m__1 on MenuWindow+<OnGUI>c__AnonStorey0. The class MenuWindow+<OnGUI>c__AnonStorey0 does not derive from UnityEngine.Object
 UnityEngine.Events.UnityEventBase.ValidateRegistration (System.Reflection.MethodInfo method, System.Object targetObj, PersistentListenerMode mode, System.Type argumentType) (at C:/BuildAgent/work/d63dfc6385190b60/Runtime/Export/UnityEvent.cs:783)
 UnityEngine.Events.UnityEventBase.ValidateRegistration (System.Reflection.MethodInfo method, System.Object targetObj, PersistentListenerMode mode) (at C:/BuildAgent/work/d63dfc6385190b60/Runtime/Export/UnityEvent.cs:772)
 UnityEngine.Events.UnityEventBase.RegisterVoidPersistentListener (Int32 index, UnityEngine.Events.UnityAction call) (at C:/BuildAgent/work/d63dfc6385190b60/Runtime/Export/UnityEvent.cs:850)
 UnityEngine.Events.UnityEventBase.AddVoidPersistentListener (UnityEngine.Events.UnityAction call) (at C:/BuildAgent/work/d63dfc6385190b60/Runtime/Export/UnityEvent.cs:840)
 UnityEditor.Events.UnityEventTools.AddVoidPersistentListener (UnityEngine.Events.UnityEventBase unityEvent, UnityEngine.Events.UnityAction call) (at C:/BuildAgent/work/d63dfc6385190b60/Editor/Mono/Utils/UnityEventTools.cs:110)
 MenuWindow.OnGUI () (at Assets/_UnitySimplificationLibrary/Editor/Windows/MenuWindow.cs:79)
 System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:222)
 


My Editor OnGUI Button Script

 if (_menus.Count > 0)
 {
     foreach (MenuHelper menu in _menus.Where(temp => temp.MyMenu != null))
     {
         menu.MyMenu.name = EditorGUILayout.TextField("Name: ", menu.MyMenu.name);
     }
 
     if (GUILayout.Button("Detect Selectable Event Methods"))
     {
         foreach (MenuHelper menu in _menus)
         {
             if (menu.MyMenu == null)
                 continue;
 
             foreach (Selectable selectable in menu.MyMenu.Selectables.Values)
             {
                 if (selectable == null)
                     continue;
 
                 Type type = menu.MyMenu.GetType();
                 MethodInfo methodInfo = type.GetMethod("On" + selectable.name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
 
                 if (methodInfo == null)
                     continue;
 
                 Debug.Log(string.Format("[{0}] - On{1} [{2}]", type.Name, selectable.name, methodInfo));
 
                 if (selectable is Button)
                 {
                     var button = selectable as Button;
                     methodInfo.Invoke(menu.MyMenu, null);
                     UnityEventTools.AddVoidPersistentListener(button.onClick, () => methodInfo.Invoke(menu.MyMenu, null));
                 }
             }
                     
         }
     }
 }



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 Zephrim · Oct 04, 2014 at 08:06 PM 0
Share

So I've figured out part of my issue, essentially because I'm making a delegate It isn't working properly, unfortunately I don't know of any way of using a reflection method in this instance without making it a delegate...

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by unimechanic · Oct 06, 2014 at 09:30 PM

You can ask in the Beta group, where our developers could help.

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 Zephrim · Oct 06, 2014 at 09:38 PM 0
Share

Thanks, I may do that. Although I think what I was trying to accomplish is impossible atleast in the method I was using to do it. $$anonymous$$y reasoning being in order to use a method obtained via reflection I would need to use an anonymous function to call it.

i.e.

 () => methodInfo.Invoke(menu.$$anonymous$$y$$anonymous$$enu, null) 

Which is completely contradictory to what the Persistent Listener is used for.

The persistent listener is used to store a function it knows exists. Whereas I was just creating a fictitious function that doesn't really exist. There may be a way of doing this, but as of right now I have no idea what it is.

Anyways, I'm just relieved that someone finally responded.

Cheers and hope you have a great day!

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

28 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

Related Questions

Unity 4.6 UI change children color selectable 0 Answers

How can I invoke a Method Property from a MethodInfo (Reflection) while keeping it's properties and types generic? 2 Answers

What is the best practice for using multiple reflection probes 0 Answers

Reflection code to test method override not working after updating Unity from 2017.1.0p4 to 2018.1.0f2 1 Answer

il2cpp generics and overloads 0 Answers


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