Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by GeoffreyFreudenberg · May 05, 2019 at 04:11 AM · buttonbuttonseventeventslistener

Any way to pass the function of a listener as a string?

alt text

Thank you!

helpimage.png (19.7 kB)
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by DiegoSLTS · May 05, 2019 at 05:41 PM

If you're doing this on runtime you can do that with some UnityBaseEvent methods and Reflection. You can do something like this:

 Button b = GetComponent<Button>();
 string method = b.onClick.GetPersistentMethodName(0);
 Object target = b.onClick.GetPersistentTarget(0);
 System.Type[] types = new System.Type[1] {typeof(string)};
 MethodInfo info = UnityEventBase.GetValidMethodInfo(target,method,types);
 Debug.Log(info.ReflectedType.Name + "." + method);

It's not perfect since you need to know the argument type of the method, but it creates the same string you want.

I guess you could use more Reflection features to avoid the need of a hardcoded Type, but it gets more complex. You might be able to iterate over all components of the target object, then get all it's method's MethodInfo with their ParameterInfo[] and test if any of them works with UnityEventBase.GetValidMethodInfo.

If you're doing this on the editor you can get the SerializedProperty of the onClick event and use that info to get the type and the method name, then you can do something similar to the code above. This is how the events are serialized:

 m_OnClick:
 m_PersistentCalls:
   m_Calls:
   - m_Target: {fileID: 1535238718}
     m_MethodName: LookAt
     m_Mode: 2
     m_Arguments:
       m_ObjectArgument: {fileID: 0}
       m_ObjectArgumentAssemblyTypeName: UnityEngine.Transform, UnityEngine
       m_IntArgument: 0
       m_FloatArgument: 0
       m_StringArgument: somestring
       m_BoolArgument: 0
     m_CallState: 2

The Mode property is the type of the argument (1 = empty method, 2 = object reference, 3 = int value, 4 = float, 5 = string, 6 = bool).

Comment
Add comment · Show 3 · 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 GeoffreyFreudenberg · May 06, 2019 at 12:25 AM 0
Share

Thank you . Sorry, I should have specified that it is indeed for the Editor scrip! Thank you for the detailed answer. The 'mode' accessor you spoke of, do I need to reference the "1-6" as you said in order to get the associated method name?

avatar image DiegoSLTS GeoffreyFreudenberg · May 06, 2019 at 02:44 AM 0
Share

For the mode, you'll do something like (I haven't tested this, but I guess this or something similar will work):

 Type[] types = new Type[1];
 int mode = serializedProperty.FindPropertyRelative("$$anonymous$$ode").intValue;
 switch(mode) {
 case 1: types[0] = typeof(void); break;
 case 2: types[0] = typeof(Object); break;
 case 3: types[0] = typeof(int); break;
 case 4: types[0] = typeof(float); break;
 case 5: types[0] = typeof(string); break;
 case 6: types[0] = typeof(bool); break;
 }

Where serializedProperty would be the SerializedProperty of the listener added to the onClick event. It's not hard to get that once you understand how SerializedObjects and SerializedProperties work, but it takes a lot of trial and error to get it right. $$anonymous$$ake sure you set your project to serialize files with "Force Text" so you can see how each property is saven on the .unity file, it'll make things a lot easier.

avatar image GeoffreyFreudenberg DiegoSLTS · May 06, 2019 at 11:32 AM 0
Share

I see. I'll look into this. Thanks for your help!

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

128 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 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 avatar image avatar image avatar image avatar image

Related Questions

Check if a listener has already been added to a button? 2 Answers

How to access the persistent listener gameobjects on a button? 1 Answer

I want the gameobject field of my persistent listeners to reference an object other than the one they are on. Is this possible? 1 Answer

Where is my OnClick in the button script? 1 Answer

OnClick() problem with Button script: "Deleting an array element will copy the complete array to all..." 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