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 demonpants · Oct 13, 2015 at 11:37 PM · editorprefabserialization

Unity custom editor, values will not serialize

I'm aware this problem has been asked like 1000 times. As far as I can tell, though, my setup is fine, and I've done this before. I have the usual issue: I edit my object, but it will not keep those values when I reload the scene. Interestingly enough, it appears to sometimes keep the values when I go to play mode, which as I understood before should have meant it was working. Guess not.

Another super weird thing: when I am editing an object that's in the scene view (selected from the hierarchy window), the editor shows up properly in the inspector and I can do whatever. But when I try to edit the prefab (selected from the project window), the inspector shows absolutely no properties, just the script name that I can tab open or closed (to no effect).

So here's my (partial) code:

 public class CritterView : MonoBehaviour
 {
     [SerializeField]
     protected List<CritterVisualPart> visualParts;
     
     protected Animator animator;
     
     public void Start()
     {
         //WARNING - before this happens, you should have set the Random.seed to be what you wanted!
         
         if ( visualParts == null )
         {
             visualParts = new List<CritterVisualPart>();
         }
         
         PopulatePartsRandomly();
     }
     
     public List<CritterVisualPart> VisualParts
     {
         get
         {
             return visualParts;
         }
         set
         {
             visualParts = value;
         }
     }

 [System.Serializable]
 public class CritterVisualPart
 {
     [SerializeField]
     protected string name;
     
     [SerializeField]
     protected string copiedPartName;
     
     [SerializeField]
     protected List<CritterVisualOption> options;

 [System.Serializable]
 public class CritterVisualOption
 {
     public enum Type
     {
         Sprite = 0,
         Prefab,
         Count
     };
     
     [SerializeField]
     protected GameObject prefab;
     
     [SerializeField]
     protected Sprite sprite;
     
     [SerializeField]
     protected Type type;

And the editor:

 [CustomEditor(typeof(CritterView))]
 public class CritterViewEditor : Editor
 {
     public override void OnInspectorGUI()
     {
         CritterView critterView = target as CritterView;
         
         List<CritterVisualPart> parts = GetNewParts( critterView );
         
         for ( int partIndex = 0; partIndex < parts.Count; partIndex++ )
         {
             ShowVisualPartGUI( parts, parts[ partIndex ] );
             
             GUILayout.Space( 16 );
         }
         
         critterView.VisualParts = parts;
         
         EditorUtility.SetDirty( critterView );
     }

Is it because I'm nesting serialized objects or something? As far as I know that should work.

Also, does anyone know where I can actually go look at the YAML that gets generated for the prefabs? Doesn't appear to be in the meta file. Seems like that would be a good way to diagnose issues.

Comment
Add comment · Show 2
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 Bunny83 · Oct 13, 2015 at 11:44 PM 0
Share

Well, does it work without your custom editor? btw System.Type can't be serialized. Setting the selected object dirty all time doesn't make much sense and can cause problems. You're doing quite wierd things in your custom inspector and we only see a small part of it. What's "GetNewParts" doing?

avatar image demonpants Bunny83 · Oct 14, 2015 at 07:18 PM 0
Share

It's not System.Type, if you look above, it's an enum called Type. I am also selecting it dirty all the time because it's not working. I'll worry about performance when the damn thing starts saving. GetNewParts calls a bunch of GUI draw functions that total 300 lines of code, I avoided pasting it all here.

1 Reply

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

Answer by demonpants · Oct 14, 2015 at 07:30 PM

I fixed it! But I don't even know how, exactly. I think my problems were stemming from two things:

First, I was using GetComponentsInChildren() to show parts of my UI. This would cause the behavior where in the prefab view you would see nothing. I fixed that part simply by calling GetComponentsInChildren( true ) instead, so that inactive (everything in the prefab view) could be found.

Second, I wasn't using mark dirty at all, since it seemed to be working without it. In the code I posted above, I have it, since I had just tried to add it to get things going. Because of the first problem, above, though, it didn't appear to be helping. When I cleared up the first one, the rest started to work. I think!

So, uh, for whoever reads this in the future: I think my advice would be to take things slow and test iteratively that stuff is actually saving (by closing and re-opening the scene) and that prefabs are working as well. I made the mistake of writing probably around 1000 lines across 5 classes for all this without testing that the saving was working. Make sure that's part of your iterative testing so you can isolate and find the weak link early.

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

32 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

Related Questions

What is runtime analogy for Editor's PrefabUtility.SetPropertyModifications? 0 Answers

How to save variables in editor, to use them during play mode 0 Answers

Editing prefab fields from custom editor that are not automatically serialized 0 Answers

Private list is serialized inside a prefab 0 Answers

SerializedObject data wipe when applying to prefab 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