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 unity_U_9udEh1kEiLrA · Dec 05, 2019 at 09:57 AM · inspectoreditor-scriptingclassesdropdown

Show a dropdown for C# classes in inspector.

I have an object which should have a plugged in behaviors. Currently each behavior is a plain C# class which uses polymorphism.

My question is: can I make an inspector dropdown button which given a set of behaviors would allow me to choose one? So I would like something like this:

     [SerializeField]
     List<Behavior> behaviors;



Where Behavior is an abstract class. The inspector should show a dropdown for each list element allowing me to choose a class which inherits from the abstract class. This list of inheriting classes can be obtained through reflection so all I really need is a dropdown to represent a Behavior in the inspector using a list of strings.

If this cannot be done are there any other ways of making it work. I know I can use scriptable objects and create a file for each behavior and plug those is but that introduces a lot of unnecessary files which I would like to avoid.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by SolidAlloy · May 17, 2020 at 12:10 PM

It can be done in a few lines using ClassTypeReference:

 [ClassExtends(typeof(Behaviour))]
 [SerializeField] private ClassTypeReference someBehaviourType;


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 PlayCreatively · Oct 06, 2020 at 02:23 AM 0
Share

Works great. Good suggestion ☕

avatar image
0

Answer by Xamtox · Dec 05, 2019 at 01:57 PM

You could create a enum:

 private enum MyClasses { MyClassA, MyClassB, MyClassC }
 [SerializeField] private MyClasses class;

And then create an ugly switch:

 public MyBaseClass ReturnMyClass {
     switch (class) {
         case MyClasses.MyClassA: return new MyClassA();
         case MyClasses.MyClassB: return new MyClassB();
         ...
     }
 }

For each new behaviour you'll only need to add new enum value and new switch case. With [Flags] attribute you could select multiple behaviours if you wish (obviously you'll need to rewrite switch for that case).

This is probably the simplest way to achieve required task, but hardly the best. I'm sure, there is much cleaner solution using editor scripts.

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 unity_U_9udEh1kEiLrA · Dec 05, 2019 at 02:57 PM 0
Share

Yeah, that is why I wanted to use Reflection. The concept is simple of what I want, just a drop down with string values which internally creates an instance (easy using reflection). I just do not know how to express it in script.

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

133 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

Related Questions

TexturePropertySingleLine in Editor class 0 Answers

Draw specific Object Inspector into Rect 1 Answer

Unable to solve: ArgumentException: Getting control 3's position in a group with only 3 controls when doing Repaint Aborting 1 Answer

How to change Inspector variable from code, so that when i exit the play mode the new values persist ? 1 Answer

Arrays/Lists in the inspector of Editor Extension scripts 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