Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Unity custom level editor List<> problem

Discussion in 'Scripting' started by Scydetracked, Aug 5, 2012.

  1. Scydetracked

    Scydetracked

    Joined:
    Jul 13, 2012
    Posts:
    5
    Hi,

    First of all, I'd like to say I have been looking through this forum and have not found that which I am about to ask. Then again I may have missed a key word... Now, I'm trying to make a custom editor window that will save all my variables in a text file, to be loaded by the level later. that part works great. Problem is, I am unable to keep my List<> between editor plays, they always reset themsevles. Since these lists contain the information needed by the level to work, i can't test anything. So my question is; Is there a way to keep my List<> so that unity doesn't reset them each time I test play my level?

    Again, sorry if this has been answered, but I couldn't find it...
    in any case, thanks for helping, if you can!

    _Phil_
     
  2. hausmaus

    hausmaus

    Joined:
    Dec 9, 2011
    Posts:
    105
  3. Scydetracked

    Scydetracked

    Joined:
    Jul 13, 2012
    Posts:
    5
    Doesn't work for me, I'm beginning to suspect I might have started this the wrong way.... Do I need a class other than the EditorWindow derived one I have? or is it supposed to keep my variables intact?
     
  4. hausmaus

    hausmaus

    Joined:
    Dec 9, 2011
    Posts:
    105
    I just looked at my last custom editor and actually it's a little more involved. Here are the files (they are for a simple custom fog effect):

    http://www.adrianswall.com/temp/serializedFields.zip


    - The [SerializeField] attribute is added to private fields in a MonoBehavior script. The idea behind this script is to create getters and setters for the fields so that some logic can be run when they are modified, which in this case requires [ExecuteInEditMode] as well.

    - There is also a Custom Editor script, which extends Editor (not EditorWindow), and only works on objects with the above MonoBehavior script. This script gets the fields from the MonoBehavior script and exposes them as properties, which can then be modified through UI elements.


    Obviously this solves a different problem than what you suggested (I misremembered having a simple EditorWindow doing the same thing), but the fields are properly serialized this way.

    -abm
     
  5. Scydetracked

    Scydetracked

    Joined:
    Jul 13, 2012
    Posts:
    5
    I don't have time to check this right now, but I'll look into this as soon as I can! I'll write back as soon as I get this to work or fail miserably. Thanks for the help!
     
  6. Scydetracked

    Scydetracked

    Joined:
    Jul 13, 2012
    Posts:
    5
    Ok, so I went through your code and I have some questions;
    - Why do you load the serialized values in the AE_Fog_Synch if you aren't using them?
    - You said the setters have some logic in them, does that mean that whichever script they call must have teh [ExecuteInEditMode] tag as well?

    Anywho, I'll start rewritting my editor class, as I was declaring and using every variable in the same script...

    Thanks for lighting the way a bit!

    _Phil_
     
  7. hausmaus

    hausmaus

    Joined:
    Dec 9, 2011
    Posts:
    105
    1) But they are used! They are bound to the UI elements.

    2) The getter/setter and ExecuteInEdit mode were just there because any changes to those properties should be reflected scene-wide immediately (they are global shader properties).

    I feel sort of goofy because the examples that I thought were clean uses of [SerializedField] turned out to be this multi-part custom editor process. So that's overcomplicating things, but it is an example of the attribute working correctly :) Hopefully you can strip it down.

    -abm
     
  8. Scydetracked

    Scydetracked

    Joined:
    Jul 13, 2012
    Posts:
    5
    Oh, haha, I didn't follow that correctly, I guess... So, it's the fifth time I edit this post, but I've managed to understand everything you did, and succeded in making the editor like I needed, biggest difference is that I need to always refer to the same object, so my script still inherits from EditorWindow. As no matter what object I select, this window will always edit the level parameters. Anyway, thanks for everything! If it wasn't for you, I think I still wouldn't understand serialization... haha!
     
    Last edited: Aug 6, 2012
  9. Flamesky

    Flamesky

    Joined:
    Jan 28, 2015
    Posts:
    13
    could you please let me have a look at your self editor window? now i am doing the similar job on unity editorwindow, i want to show the buttons/list/pictures/text like what i want to to ,could you give me a help? Thanks very much..