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 h5nc · Feb 08, 2017 at 08:53 AM · editorbuttononclick

How to get parameters of UnityEvent that I set in inspector from script?

alt text

I want to get the number 5 that I set in inspector but I find nowhere in my button.onClick. What I eventually to do is to copy (or move) this onClick event to another component in script (execute in editor mode).

I googled GetValidMethodInfo but this doesn't contain parameters.

How can I do this?

simeji-2017-2-8-14-29-8.jpg (38.0 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
2
Best Answer

Answer by Adam-Mechtley · Feb 08, 2017 at 10:48 AM

The most flexible way to do this is to use SerializedProperty. You may find my answer here helpful, but in short you will do something like this:

 // assuming you have a reference to your Button component as m_Button
 SerializedObject so = new SerializedObject(m_Button);
 SerializedProperty persistentCalls = so.FindProperty("m_OnClick.m_PersistentCalls.m_Calls");
 for (int i = 0; i < persistentCalls.arraySize; ++i)
     // in this example I'm just logging it
     Debug.Log(persistentCalls.GetArrayElementAtIndex(i).FindPropertyRelative("m_Arguments.m_IntArgument").intValue);

When you retrieve the int value in this way, you can use SerializedProperty again on your destination object, or you can use UnityEventTools.AddIntPersistentListener and supply the int value you retrieved.

Comment
Add comment · Show 2 · 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 h5nc · Feb 08, 2017 at 11:34 AM 0
Share

This is great but a little bit cheaty.. Is it actually "ok" or "legal" to use reflection to get non-visible members? I can find unofficial codes but I'm not sure if I can use it in commercial products. Is there any other way to do this? If not, I'll go with this for sure:)

avatar image Adam-Mechtley h5nc · Feb 08, 2017 at 12:01 PM 0
Share

SerializedProperty is not the same as using reflection. It's actually the means we recommend for people to modify properties in editor scripts, because it automatically handles prefab overrides, dirtying, undo/redo, and so on. It is certainly possible you can get into trouble if e.g., some backing field name changes, but these particular properties have already been through a couple of renames so it is safe and reasonable to assume they will not be changing. The caveat of course is that this is an editor-only class, so you should not use it in runtime code.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Referencing function in editor 0 Answers

Problem when instantiate button with Onclick.Addlistener 0 Answers

Only certain buttons are playing the sound onclick 0 Answers

Calling preset Button OnClick events from another script 3 Answers

OnClick() animation 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