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 Yodzilla · Nov 15, 2015 at 10:16 AM · invokeserializedpropertyserializable

Invoking UnityEvents in code and passing parameters

I've created custom buttons in Unity's UI and have added my own UnityEvents to these buttons so that I can assign functions to call on click events and such. I'm doing this with:

 [System.Serializable]
 public class ToggleEvent : UnityEvent<bool> { }

 [SerializeField]
 public ToggleEvent clickEvent;

With the event calls looking simply like:

 public void ButtonEvent(bool value) { }

This works great in the editor but when I call

 clickEvent.Invoke(true);

...it'll only pass whatever the bool value is marked off in the editor, not what's actually being passed. How do I override what's being set in the editor and pass my own variables in code?

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
1
Best Answer

Answer by Yodzilla · Nov 16, 2015 at 12:22 AM

Welp I seem to have found an ugly workaround. Instead of calling clickEvent.Invoke I can disable persistent methods and invoke the one that I want to call like so:

 // this is to turn off the value and call set in the editor
 clickEvent.SetPersistentListenerState(0, UnityEventCallState.Off);
 clickEvent.RemoveAllListeners();

 // get the method assigned in the editor and call it
 MethodInfo methodInfo = UnityEventBase.GetValidMethodInfo(clickEvent.GetPersistentTarget(0), clickEvent.GetPersistentMethodName(0), new System.Type[] { typeof(bool) } );
 methodInfo.Invoke(GameObject.FindObjectOfType<MenuModsScript>(), new object[] { _enabled });

I'll clean it up for sure but if anyone knows anything else I'm all ears.

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 IgorAherne · Sep 27, 2016 at 02:03 PM 0
Share

additionally, from unity's manual

By default a UnityEvent in a $$anonymous$$onobehaviour binds dynamically to a void function. This does not have to be the case as dynamic invocation of UnityEvents supports binding to functions with up to 4 arguments. To do this you need to define a custom UnityEvent class that supports multiple arguments. This is quite easy to do:

 [Serializable]
 public class StringEvent : UnityEvent  {}

By adding an instance of this to your class ins$$anonymous$$d of the base UnityEvent it will allow the callback to bind dynamically to string functions.

This can then be invoked by calling the Invoke() function with a string as argument.

UnityEvents can be defined with up to 4 arguments in their generic definition.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Instantiated objects into serialized property? 2 Answers

How to find property of a serializeObject that has the same name with a field of another property? 1 Answer

Error when trying to Serialize a field that is in a class 0 Answers

Should EditorGUILayout.PropertyField work with serializable classes? 1 Answer

Custom Inspector for an array of a serialized class 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