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
3
Question by neonblitzer · Apr 13, 2014 at 11:47 PM · editorserializationcustom-inspectorcustom-editorhideflags

Can I serialize data in an Editor class?

I have a custom inspector for a MonoBehaviour. The target is a singleton, and there is only one in a scene at any time. I'd like to preserve, among other things, the state of the editor foldout booleans when changing inspector target (selecting different objects) and (de)serializing the scene. The way I currently achieve it is by having the foldout booleans stored in the editor target, and while it works, the editor state doesn't really belong there.

I haven't succeeded in storing them in the editor object itself. It seems that it may be recreated each time you start inspecting the target, and it doesn't live in the scene hierarchy so it's not serialized. (I've tried the System.Serializable and SerializeField attributes, different HideFlags and setting the editor object dirty, to no avail.) The Editor class derives from ScriptableObject but where does it live? Can I force it to persist in the scene? Or create and use a scene object (like a MonoBehaviour) with certain HideFlags without leaking stuff? Is there some API like EditorPrefs but per-scene?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by vexe · Apr 21, 2014 at 08:44 AM

the editor state doesn't really belong there.

Why not? - Sure, when you build, you don't need anything editor-related in your behaviours. But at edit-time, you could say that the behaviour needs that information in order for it to be inspected properly.

But since we don't need that state code at build, you could just use conditional compilation:

 public class Target : MonoBehaviour
 {
    // ...
    #if UNITY_EDITOR
    [SerializeField, HideInInspector]
    private bool fold;
    #endif
 }

It's private, no one needs to know about it - from your editor script, you could fetch the value easily if you're using SerializedProperties via var spFold = serializedObject.FindProperty("fold"); and then use spFold.booleanValue...

Comment
Add comment · Show 1 · 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 neonblitzer · Apr 24, 2014 at 06:21 PM 1
Share

Thank you for your insight. Your solution works but I'm looking for a more... ideal one :) I think the behaviour shouldn't even have to know that there is an editor for it, much less contain information that is used only by the editor. (If I go to a doctor I shouldn't have to tell them how to inspect me :)

I guess it's just a matter of how much you want to abide by the decoupling principles, so it's kind of an academic question. Still I'd like to know if there is any way to have that data closer to the editor, either in itself or some intermediate place.

Using preprocessor is pretty neat, though.

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

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

Custom Editor for Monobehavior with custom property not retaining Gameobject references 0 Answers

Editable non-monobehaviour objects 1 Answer

Custom editor tags not saving 1 Answer

Custom Inspector Horiztonal Vector2 and Button Spacing 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