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 /
  • Help Room /
avatar image
0
Question by ballistic_cowboy · Apr 06, 2020 at 06:36 PM · eventsystemevent triggeringevent-listener

How to set a Selectable's interactable property directly using a UnityEvent type through UnityEditor.Events.UnityEventTools.AddPersistentListener?

In the inspector for a UnityEvent field it is possible to assign the dynamic bool result to the interactable property of a button (or any selectable). I would like to automatically assign this call from a script rather than go through the inspector because I have a large number of such links to create.

Using the AddPersistentListener I know it is possible to call a function of a UnityEngine.Object with a bool parameter, however unfortunately interactable is a property of selectable and there is no 'SetInteractable' function I can assign. I also tried adding an extension method for the Selectable class to create such a method, however it turns out static methods are not allowed in the AddPersistentListener either, nor are lambda expressions, and I do not want to subclass button just to add this tiny wrapper function either. My current workaround is to store a reference to the selectable and add a new method to the class that is calling 'AddPersistentListener' such as this:

     public class ButtonLinker: MonoBehaviour
     {
         public Button button;
         public UnityEvent<bool> buttonInteractableCaller;
         public void SetButtonInteractable(bool interactable) {
             button.interactable = interactable;
         }
         public void OnValidate() {
 #if UNITY_EDITOR
             UnityEditor.Events.UnityEventTools.AddPersistentListener(buttonInteractableCaller,SetButtonInteractable);
 #endif
         }
     }

But it would be better if there is some method to assign interactable without having to create this wrapper in the other class. I think unity itself must do something else because the inspector seems to be able to assign a dynamic bool to the interactable property itself without such a wrapper, any ideas how to achieve this?

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

Answer by ballistic_cowboy · Nov 02, 2020 at 05:35 PM

I figured this out for anyone else who is stuck. You need to include System.Reflection and then get the property set method and finally create an action of the correct type before adding it:

      public class ButtonLinker: MonoBehaviour
      {
          public Button button;
          public UnityEvent<bool> buttonInteractableCaller;

          public void OnValidate() {
  #if UNITY_EDITOR
              MethodInfo mInfo = button.GetType().GetProperty("interactable").GetSetMethod();
              var action = System.Delegate.CreateDelegate(typeof(UnityAction<bool>),button,mInfo) as UnityAction<bool>;
              UnityEditor.Events.UnityEventTools.AddPersistentListener(buttonInteractableCaller,action);
  #endif
          }
      }

Comment
Add comment · 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

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

200 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 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

Unity UI Events not working for Canvas objects 1 Answer

OnPointerClick only triggers when clicked on the top part of UI Image 1 Answer

Disable current drag of UI.Slider 2 Answers

IDropHandler only trigger when it's SpriteRenderer sortingOder is higher than draggable sortingOder 0 Answers

How to only click on certain objects/buttons and null the rest please help!! 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