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
1
Question by purplekjw · Sep 03, 2018 at 11:58 AM · classinheritancecustom inspector

Accessing subclass properties in inspector based on enum

First of all, apologies for the poor title. I was having trouble describing the issue in short.

So here's the issue: I have a custom class named TargetArea which has a single Transform variable intended to hold a prefab to instantiate later. I then have a number of classes which inherit TargetArea (CircleTargetArea, LineTargetArea etc.) and each of these classes have their own variables for modifying their particular type of target area (circle has radius, line has width/length etc.).

On top of that I also have abilities which are all individual ScriptableObjects. Currently my abilities have a "Public TargetArea targetArea" variable which allows me to set the prefab, but what I want to do is to be able to select, in the inspector, which child of TargetArea is relevant for a particular ability, and then be able to input the corresponding values for that particular Type of TargetArea.

I've been looking at property drawers but it's the bit about selecting the child class that's throwing me off. I can't currently think of a way to approach this issue.

Any help is very much appreciated.

Comment
Add comment · Show 6
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 purplekjw · Sep 03, 2018 at 12:00 PM 0
Share

Also apologies for the lack of actual paragraphs. I didn't realize it would appear like that and I have no idea how to correct it.

avatar image Casiell · Sep 03, 2018 at 12:17 PM 0
Share

Are you talking about type casting? With this you can hold a reference to parent type and then convert it to another type that derives from it.

So for example you can do something like this:

 class TargetArea
 { }
 
 class CircleTargetArea : TargetArea
 { 
     public float Radius = 5f;
 }
 
 class Ability
 {
     public TargetArea targetArea;
     
     void DoSomething()
     {
         CircleTargetArea circleTargetArea;
         if(targetArea is CircleTargetArea) //this checks if targetArea can be cast to CircleTargetArea
         {
             circleTargetArea = targetArea as CircleTargetArea; //this is an actual cast that gives you an object of type CircleTargetArea
             Debug.Log(circleTargetArea.Radius);
         }
     }
 }


avatar image purplekjw Casiell · Sep 03, 2018 at 12:33 PM 0
Share

Thank you for your reply. I guess I should try to be clearer.

I'm primarily concerned with the inspector and modifying it to achieve the desired result. For each of my ability assets I would like to be able to select the relevant TargetArea type (circle, line, etc.) in the inspector. I envision a dropdown menu with all of the available types. After selecting the relevant type, I would like for the inspector to then display the properties for that type. (If I select circle, I would like to be able to see and edit its radius property. If I change my selection to line, radius should be replaced by line/width.)

This would allow me to edit target areas for each of my individual abilities in the inspector.

avatar image Casiell purplekjw · Sep 03, 2018 at 12:47 PM 1
Share

Ah, so you want to create a custom editor! Sorry, I'm dying of boredom in my work, so I may bi a bit sleepy.

What you want to do is create an enum with values coresponding to your types.

Then in your custom inspector you just have to check what values this enum has and display correct fields.

As for creating custom inspector you can check some Unity tutorials, they are very nice usually

https://unity3d.com/learn/tutorials/topics/interface-essentials/building-custom-inspector

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by purplekjw · Sep 03, 2018 at 04:02 PM

This is my solution this far. It almost meets my original criteria, but introduces a new and rather large problem which I'll explain at the bottom.

alt text

alt text

As can be seen in the images, the displayed properties change when the "Target Area Type" is changed. This was achieved with the follow custom editor:

 [CustomEditor(typeof(Ability))]
 public class AbilityEditor : Editor {
     public override void OnInspectorGUI() {
         DrawDefaultInspector();
 
         Ability ability = (Ability)target;
         ability.targetAreaType = (Ability.TargetAreaType)EditorGUILayout.EnumPopup("Target Area Type", ability.targetAreaType);
 
         switch (ability.targetAreaType){
             case Ability.TargetAreaType.circle:
                 CircleTargetArea circleTargetArea = ability.targetArea as CircleTargetArea;
                 if (circleTargetArea == null){
                     circleTargetArea = new CircleTargetArea();
                     Debug.Log("Create new circle target area");
                 }
                 //Create inspector fields for the properties unique to circle target area.
                 circleTargetArea.radius = EditorGUILayout.FloatField("Radius", circleTargetArea.radius);
                 //Stores changes to the ability property. Said property can then be accessed for all the information required to instantiate a target area.
                 ability.targetArea = circleTargetArea;
                 break;
             case Ability.TargetAreaType.line:
                 LineTargetArea lineTargetArea = ability.targetArea as LineTargetArea;
                 if (lineTargetArea == null){
                     lineTargetArea = new LineTargetArea();
                 }
                 lineTargetArea.width = EditorGUILayout.FloatField("Width", lineTargetArea.width);
                 lineTargetArea.length = EditorGUILayout.FloatField("Length", lineTargetArea.length);
 
                 ability.targetArea = lineTargetArea;
                 break;
         }
 
         //Allows the prefab to be set. This property is in the parent class "TargetArea", so all children share it. 
         ability.targetArea.targetAreaPrefab = EditorGUILayout.ObjectField("Target Area Prefab", ability.targetArea.targetAreaPrefab, typeof(Transform), true) as Transform;
     }
 }
 

So I achieved what I originally set out to do, to an extent. However, if I'm understanding things correctly then subclasses cannot be serialized. Since CircleTargetArea and LineTargetArea are being stored on my Ability assets as a public TargetArea, the values that are unique to the subclasses are being lost as soon as I hit play. If anyone knows of a way to address this then any comments would be immensely appreciated. I'm wary of going off topic, but opening a new question seems to be overkill especially with the abundance of similar questions. (Despite said abundance, I have yet to find anything that has helped me in terms of finding a solution).


example-circle.png (12.5 kB)
example-line.png (13.7 kB)
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

93 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

Related Questions

Change a Class in the inspector 1 Answer

List of Different Classes That All Inherit From One Class 1 Answer

How to dereference multi-tired class? 0 Answers

C# Question - Do I Have to inherit from MonoBehaviour? What happens if I don't? 2 Answers

Custom Inspector not working with inheritance. 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