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
1
Question by cornel · Oct 28, 2013 at 09:09 AM · inspectorserializedpropertycustom class

Serialized property and custom class

I want to expose a custom class in the inspector. Using the default editor my class is shown as it should be, but I can't expose it in my custom editor.

I have a abstract class:

 [System.Serializable]
 public abstract class BaseAbility : IAbility
 {
   public Team casterTeam;
   public float areaOfEffect;
   public float cooldown;
   public int cost;

   protected BaseAbility(Team team, float aoe, float cd, int cost)
   {
       this.casterTeam = team;
       this.areaOfEffect = aoe;
       this.cooldown = cd;
       this.cost = cost;
   }
   public abstract void Cast(Vector3 position);

   public abstract void Cancel();

 }

And another class that is derived from baseAbility:

 [System.Serializable]
 public class FireAbility : BaseAbility
 {
   public int damage;

   public FireAbility(int damage, Team team, float aoe, float cd, int cost)
       : base(team, aoe, cd, cost)
   {
       this.damage = damage;
   }

   public override void Cast(Vector3 position)
   {
       GameObject go = PoolManager.Spawn("FireBall");
       Fireball fireball = go.GetComponent<Fireball>();
       if (fireball != null)
       {
           fireball.FireAt(this, position);
       }
   }

   public override void Cancel()
   {
   }
 }

I want to expose FireAbility in the inspector, but for some reason I can't do so. I have a class that has a public FireAbility named fireAbility and in the editor class I do this:

 SerializedProperty fireAbility = serializedObject.FindProperty("fireAbility");
 EditorGUILayout.PropertyField(fireAbility);

I don't understand why this doesn't work. I have other classes that use custom classes and they just work, but for some reason this doesn't want to.

I don't understand what I'm doing wrong; If I use the default editor it shows my class, but when I use my editor it just shows the toggle arrow and no members. I tried adding [SerializedField], removing the abstract keyword, removing constructors, but nothing worked.

Comment
Add comment · Show 3
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 vexe · Oct 28, 2013 at 10:38 AM 0
Share

This might help you.

avatar image cornel · Oct 28, 2013 at 02:40 PM 0
Share

It's certainly interesting, but it's very messy. Since the default editor exposes the class, I guess there must be a way to expose it in a custom editor, but I can't find anything about how the default editor works.

avatar image bumbu · Nov 17, 2015 at 12:54 PM 0
Share

I'm having a similar problem. I have a class which i have custom property drawer. In the custom inspector I receive SerializedProperty which i would like to convert to the original class. I have a already working editor where i can select the method using my EditorWindow. However i would like use the same in the default inspector if my $$anonymous$$onoBehavrious has this property but I'm struggling with the SerializedProperty conversion.

 [Serializable]
 public class FunctionSelectorProperties
 {
     public GameObject go;
     public $$anonymous$$onoBehaviour functionTarget;
     public string methodName;
 }
 
 public class OtherClass : ScriptableObject // this is .asset file
 {
     public string foo;
     public int bar;
     public FunctionSelectorProperties functionSelector = new FunctionSelectorProperties();
 }
 
 [CustomPropertyDrawer(typeof(FunctionSelectorProperties))]
 public class FunctionSelectorDrawer : PropertyDrawer
 {
     // Draw the property inside the given rect
     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
     {
     // ...
 
         FunctionSelectorProperties functionSelector = (FunctionSelectorProperties)property.objectReferenceValue; // cant do this 
 
     // ...
     }
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Statement · Oct 28, 2013 at 02:59 PM

Does the object actually have a "fireAbility" field? Is it exposed as type of IAbility, BaseAbility or FireAbility? It will not work properly if the underlying type is any of the former two.

Try passing true to the PropertyField.

 EditorGUILayout.PropertyField(fireAbility, true);

If true the property including children is drawn; otherwise only the control itself (such as only a foldout but nothing below it).

Comment
Add comment · Show 2 · 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 cornel · Oct 28, 2013 at 03:38 PM 0
Share

After a lot of searching I came to think that polymorphism isn't that well supported with the editor classes. Your way seems to be the only one that works, that is using concrete classes only (no abstract classes or interfaces);

avatar image Statement · Oct 28, 2013 at 05:04 PM 0
Share

You could perhaps have a mechanism for backing storage and create the types and behaviour at runtime though. Or use ScriptableObject, which uses a different serialization logic and allows for polymorphism, though, it adds a little overhead to each object and may not be what you want.

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

19 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

Related Questions

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

Change the fields on a object bound to a SerializedProperty via custom inspector? 1 Answer

Draw ScrptableObject inspector in other inspector 4 Answers

Where can I find the default "serialized data" files that Unity save and load during run-time / editor time from the inspector? 0 Answers

[solved] How to edit an array of custom classes in the Inspector 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