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 wladyslaw · Jan 16, 2018 at 07:33 PM · editorinspectorscriptableobjectgenericstructure

On quest for generic editor to allow inspectability and testability of plain data classes

It seemed to be a truly simple problem, but after 3 days of struggling and learning Unity internals I feel a bit hopeless.

The problem

My desire is to be able to see and edit in Inspector the attributed properties of my MonoBehaviour. If a property is a simple type (int, string), I want to see a default edit control for it. If it's a complex object, I want to recursively show its properties in a tree-like structure: asd I want to keep it generic, so that I can use it with multiple classes without modifying my editor code.

What I did

  1. I started with the popular script ExposePropertiesInInspector, which is basically showing in inspector the properties of simple types marked with ExposeProperties attribute. I added the ExposeProperties to some properties in my ActorBehaviour and ActorData classes.

    1. I made empty classes named ExposableMonoBehaviour (for scripts like ActorBehaviour to derive from it) and ExposableObject (for data classes like ActorData to derive from it).

    2. I made editors for them. In OnInspectorGUI, the editors draw the default inspector, use reflection to fetch the properties marked with ExposeProperty attribute, and if the property is deriving from ExposableObject, they add indent and recursively create an editor to show the object's properties beneath.

It does work, but not entirely.

Problems I encountered

  1. Seems like I can't create an editor for a class not deriving from UnityEngine.Object. I chose ScriptableObject because it seemed most lightweight for me and made my ExposableObject derive from it. I thought I can swallow using it, but then I realised ScriptableObjects can't be created in unit test context! but I need to use my ExposableObjects alot in testing.

  2. Although the serialization and deserialization is working well for ExposableObjects (all the properties are nicely saved when needed and retrieved to previous state after quitting play mode), there is one issue with them — after duplicating a GameObject, the new one shares the same instance of my ExposableObject. So when I change Energy in ActorData of one object, it's changed also for the other one.

    So...

How it can be helped? Am I doing something wrong? Am I aiming for too much?

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 Adam-Mechtley · Jan 16, 2018 at 08:44 PM 0
Share

I'm not sure I understand the problem. The text in your problem statement is exactly what Unity does by default:

$$anonymous$$y desire is to be able to see and edit in Inspector the attributed properties of my $$anonymous$$onoBehaviour. If a property is a simple type (int, string), I want to see a default edit control for it. If it's a complex object, I want to recursively show its properties in a tree-like structure:

Is the problem that some things are appearing in the inspector that you don't want, or that some things you want to appear do not?

avatar image Adam-Mechtley Adam-Mechtley · Jan 16, 2018 at 08:45 PM 0
Share

Or I guess is the problem that you want to display properties rather than fields (the latter of which is what Unity supports by default)?

avatar image wladyslaw Adam-Mechtley · Jan 17, 2018 at 08:37 AM 0
Share

Exactly, I'm talking about properties. I don't want to use public fields when I don't need to.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Adam-Mechtley · Jan 17, 2018 at 08:59 AM

Is there a reason you cannot do this the recommended way that still works with Unity's serialization system? E.g., simply add a private backing field for your property with SerializeField attribute

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 wladyslaw · Jan 17, 2018 at 11:29 AM 0
Share

Wow, actually I somehow omitted this approach, rushing to use ExposePropertiesInInspector. However, there's one big issue with doing so — changing the values in the editor affects the fields directly, ignoring the properties encapsulating those fields. In some cases this is not acceptable (e.g. when I need to do some additional stuff in property getter/setter). So my problem is not solved, but honestly the approach you proposed should work for most of my use cases. For the record, by browsing through Unity Editor extensions I stumbled upon Odin Inspector, which I think handles easily the case I'm describing. It's not free though.

avatar image Adam-Mechtley wladyslaw · Jan 17, 2018 at 11:31 AM 0
Share

I have this free package which might help

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

100 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

Related Questions

How should I serialize data that is also editable in the Inspector? 2 Answers

[Solved]Why doesn't my ScriptableObject based asset save using a custom inspector ? 1 Answer

Why doesn't my ScriptableObject save using a custom EditorWindow? 3 Answers

Referencing / linking a .asset / .prefab file in another .asset / .prefab file programmatically. 2 Answers

Unity inspector scripting - scriptable objects - card game 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