Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Lachee1 · Apr 15, 2014 at 10:14 AM · c#classcustomenumpropertydrawer

PropertyDrawer: Enum to select extended class

I am experimenting with unitys editor, to see what I can do and learn it a bit. At the moment I am stuck with one of my experiments.

I have a class called UIElement and have other classes extend of it (eg: UIButton and UILabel). I wish for the different extension to be selectable/changeable in the editor so I have been working on a PropertyDrawer for the class and created enums to help. However im stuck on switching the types on based on the selected enum (and keeping it switched, even on scene save and load).

So, in other words... I wish to be able to switch the classes (that are extended of UIElement) using a enum and PropertyDrawers. Is this even possible and if so, how can I achive it?

Here is a shot of what I currently have to help give more understanding:

alt text

Here is my classes:

 public class UIElement {
     public UIType uiType { get { return getUIType(); } }
     public string name;
     public Rect position;
 
     public virtual void OnGUI () {}
     public virtual void OnInspector (Rect position,  SerializedProperty property) {}
     public virtual UIType getUIType() { return UIType.label; }
 }
 
 [Serializable]
 public class UIButton : UIElement {
     public Texture2D image;
     public override void OnGUI () {
         
     }
     
     public override void OnInspector(Rect position,  SerializedProperty property) {
         EditorGUI.PropertyField(position, property.FindPropertyRelative("image"));
     }
 
     public override UIType getUIType() { return UIType.button; }
 }
 
 
 [Serializable]
 public class UILabel : UIElement {
     public string text;
     public override void OnGUI () {
 
     }
 
     public override void OnInspector(Rect position,  SerializedProperty property) {
         EditorGUI.PropertyField(position, property.FindPropertyRelative("text"));
     }
 
     public override UIType getUIType() { return UIType.label; }
 }



And here is my PropertyDrawer at the moment:

 [CustomPropertyDrawer(typeof(UIElement))]
 public class UIElementDrawer : PropertyDrawer {
     public UIType tpe;
     public override void OnGUI (Rect position, SerializedProperty property, GUIContent label) {    
         //Debug.Log(property.FindPropertyRelative("uiType").intValue);
 
         label = EditorGUI.BeginProperty(position, label, property);
         
         position.height = 16;
 
         tpe = (UIType) EditorGUI.EnumPopup(position,label,tpe);
         position.y += 18;
 
         EditorGUI.PropertyField(position, property.FindPropertyRelative("name"));
         position.y += 18;
 
         EditorGUI.PropertyField(position, property.FindPropertyRelative("position"));
         position.y += 18;
 
         //Im stuck here, how do i change stuff?
         /*switch(label) {
         
         }*/
     
         EditorGUI.EndProperty();
     }
 
     public override float GetPropertyHeight (SerializedProperty property, GUIContent label) {
         return 18F*4F;
     }
 }
 

Thank you in advance, and I'm sorry im not very good at explaining things :\\

Comment
Add comment · Show 2
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 Berenger · Apr 15, 2014 at 10:23 AM 0
Share

I can't try right, but maybe you can use generic ? Like :

 [CustomPropertyDrawer(typeof(T))]
 public class UIElementDrawer <T>: PropertyDrawer where T : UIElement { ...
avatar image Lachee1 · Apr 15, 2014 at 10:29 AM 0
Share

Sadly that does not work, and it would still leave me the issue of swapping types when the enum is changed :(

0 Replies

· Add your reply
  • Sort: 

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

21 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

Related Questions

Restricting enum options in inspector when using a propertydrawer. 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to get PropertyAttribute or SerializedProperty owner class instance if it's nested? 1 Answer

PropertyDrawer, Change property within callback 2 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