• 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 JUSTCAMH · Apr 12, 2019 at 12:47 AM · shadereditor-scripting

Using an abstract class in the inspector

So, I've got a set of 'attacks', which are scriptable objects. I want to define an attack as a set of actions, where each action derives from the abstract class 'Action'. For example, there would be a 'WaitAction' and a 'DamageInRegionAction', all which would extend Action. What I want is for an easy way to set this all up in the editor. I'm perfectly capable of creating the code for such a system, but I don't know how to edit this data in the editor. I'd rather not use scriptable objects to create the action system, simply to avoid having 10 files per attack. My attack would contain an array of actions that would be performed. How would I make it so that I can specify in the editor which type of Action I want, since I won't be using ScriptableObjects? I've tried using custom PropertyDrawers and enums with little avail, so any help would be appreciated. Am I just condemned to using Scriptable Objects, or is there a better solution?

Comment
Add comment · Show 1
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 surfuay · Apr 12, 2019 at 01:45 AM 0
Share

i think it's as simple as declaring a serializable array in your script that will be calling the actions

[serializefield] private GameObject[] actions;

serializefield makes it acessible in the editor and you'll find the actions section click on the down arrow and it will ask you to specify the size of the field (10) and then you just drag each item into the vacant slots.

1 Reply

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

Answer by Bunny83 · Apr 12, 2019 at 01:58 AM

You simply can't since Unity's serializer does not support inheritance for custom serializable classes. Only MonoBehaviours or ScriptableObjects do support inheritance since they are the only extendable classes which represent a serializable reference type. Since you don't want to use them it's not possible through Unity's serialization system. Anything else would require some nasty custom serialization workarounds.


What's the exact problem you have with ScriptableObjects? Keep in mind that you can actually store multiple instances in a single asset file. Though this can get a bit messy. It's far more convenient to store a single asset as a single file.

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 JUSTCAMH · Apr 12, 2019 at 02:11 AM 0
Share

I feared as much. Thank you for the answer. I've aimed for modularity in the past, and been bogged in endless chains of scriptable objects.

Would it be possible to pair each 'Action' field with an enum, then have the Action field be cast to the appropriate class, and show the options for that? For example, I just select 'Wait' from an enum drop-down, then the action field switches to show the properties associated with the 'WaitAction'?

avatar image Bunny83 JUSTCAMH · Apr 12, 2019 at 10:05 AM 0
Share

No, that's not possible. You have to differentiate between a cast and a conversion. A cast just changes the "reference type" to the actual underlying type. A conversion would create a new instance based on the old. However for example if you have a list of "Animals" and you put some Dogs some Cats and a Camel into that list, Unity will serialize them all as "Animals". So they won't have any Dog, Cat or Camel specific fields. So even when you remembered that item 2 was a Cat, the actual instance in the list after deserialization is just an Animal. (and yes, afaik the deserializer actually can instantiate abstract classes as well since it doesn't go through the contructor. $$anonymous$$aybe they fixed it already).


As i said workarounds would be to serialize your objects instances yourself into a common format (JSON, binary, ...) and store that serialized data inside a common structure which will be serialized by Unity. In this case the Unity serialization system only provides you with the data you have serialized and you are responsible of recreating the actual instances. You can use Unity's JsonUtility for that. Though you have to also rememer the actual type you serialized. This whole approach is quite complicated, results in a larger serialization memory footprint and is less flexible in terms of inspector editing. With this approach we do not serialize references to instances but we're back to "child object owning" like Unity does, with the exception that we could support polymorphism. Though the question is: Is it worth it? ^^


If you want go through a custom serialization approach, be aware that Unity provides the ISerializationCallbackReceiver interface (and people think "internationalization" is a long word :D). The interface can be applied to any class that Unity can serialize. It allows you to hook into the serialization procedure of Unity. Though you can not influence what Unity does. It just provides a way to perform your own serialization / deserialization before / after Unity. Note that those callbacks are issued from a seperate thread. So you can not use any Unity API in those callbacks. Though i think the JsonUtility should work.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

174 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 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

How to change editor highlighting behavior? 0 Answers

How to save emissive Material assets using "_EMISSION" keyword by script? 1 Answer

Can't include shaders in packages exported by ExportPackage. How to work around this? 1 Answer

Allow shaders with 'time' variable to update in editor 1 Answer

How to force the compilation of a shader in Unity? 5 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