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 iMamo · Feb 06, 2017 at 08:25 AM · custom editorinterfaceselectioncustom inspectorimplementation

Unity Editor interface implementation selector?

Suppose I have some interface IMyInterface with two or more implementations. Every implementation has different fields, properties, etc., but they all aim to do the same task. All implementations are Serializable.

I'd like to use an implementation of IMyInterface in one of my MonoBehaviours, but I want the user to choose which one to use (perhaps using a dropdown Popup?).

Based on the user's choiche, I'd like to display the editor for the chosen implementation, so that the user can change its settings.

How would I go about doing that?

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 Bunny83 · Feb 07, 2017 at 03:42 AM

I'm not sure what you mean by "All implementations are Serializable". Interfaces can't be serialized as they are only a "method-contract". An interface doesn't contain any data. Furthermore Unity's serialization system can't serialize references of an interface type, only types which are derived from UnityEngine.Object. That means user created types are restricted to either MonoBehaviour or ScriptableObject derived types.

So you would need a public variable of type MonoBehaviour or a derived type so it will be serialized and will show up in the inspector. Even though you could write a custom PropertyDrawer and use EditorGUI.ObjectField, it wouldn't help because ObjectField doesn't work with an interface type as type restriction. I'm actually a bit disappointed that it doesn't work. I thought that might be a nice solution, but no. You can use your interface type as restriction, but as result the field won't accept any reference. Also the ObjectPicker (which shows when you click that little icon on the right side) doesn't show any matching assets.

The only two workarounds would be to either use the ordinary ObjectField so the user can select any MonoBehaviour derived instance and then manually check for the interface type and if the selected object doesn't implement it, set the reference to null. The only other solution is to create your own "ObjectField control". It's not that hard but still a lot work depending on what features you want. Presenting a dropdown with all instances in the scene would be a way, but if there are many objects this is not a good solution. Providing simple drag&drop support is probably the best way.

In the end you have to ask yourself if it's worth the all hassle. A solution that should work is to use an abstract base class that is derived from MonoBehaviour. However that of course restricts you to "one" base class. If you use an abstract base class you don't need to do anything. Just declare a public variable of your base class and you can only select instances which are derived from that base class.

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 iMamo · Feb 11, 2017 at 06:04 PM 0
Share

With "All implementations are Serializable", I mean that all classes which implement I$$anonymous$$yInterface are serializable.

I tried replacing the interface with a class extending UnityEngine.Object, and make the other classes extend I$$anonymous$$yInterface ins$$anonymous$$d of implementing it, but still: the inspector won't show the variables for extending classes when I do public I$$anonymous$$yInterface myThing = new $$anonymous$$yImplementingClass();.

In simple terms I'm asking how to make a dropdown, and based on the selected dropdown item, present the user with some fields for ints, floats, checkboxes, etc. to modify. The modifiable fields are given by the public or serializable fields of the class with the same name as the selected option entry.

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

EditorGUILayout.PropertyField cannot draw custom class? 2 Answers

Is there any way to hide the "Object picker" of an EditorGUILayout.ObjectField ? 2 Answers

Custom editor UnityEvents in List 0 Answers

How to limit an ObjectField to assets with specific file extension 2 Answers

Should I use Inspector or custom editor window? 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