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

How to set SerializedObject

Discussion in 'Editor & General Support' started by sum1nil, Feb 13, 2020.

  1. sum1nil

    sum1nil

    Joined:
    Oct 28, 2017
    Posts:
    4
    I have unity examples that uses 'serializedobject' to maintain an array courtesy of a Unity script called SerializedPropertyExtensions. Unfortunately, I cannot find where to set/get serializedobject. My Code is:
    Code (CSharp):
    1.    
    2.     // cards is the name of array in CardCollection that holds all the cards.
    3.     const string cardsPropName = "cards";
    4.     private SerializedProperty cardsArrayProp;
    5.     private CardSO[] cards; // CardSO are  ScriptableObjects
    6. ...
    7. private void OnEnable()
    8.  
    9.     {
    10.  
    11.         // Set the SerializedProperty variable to the collections array using the array's name
    12.  
    13.         cardsArrayProp = serializedObject.FindProperty(cardsPropName);    }
    I understand using serialized object helps keep things in order but in the above OnEnable, "serializedobject" is not defined.I do not know how to assign in to CardSO scriptable object array.

    How do I set up the CardSO[] to be used by the serializable object.
    I have much more code, both mine and Unity's, but in the unity code I cannot find the declaration for "serializedobject"; it just is there.
     
  2. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,135
    You can create a new SerializedObject like this:
    Code (CSharp):
    1. var serializedObject = new SerializedObject(cards);
    Remember to also call serializedObject.ApplyModifiedProperties after you've modified any properties in order to actually apply them to the cards.

    Examples can be found in the script reference for SerializedObject.
     
  3. sum1nil

    sum1nil

    Joined:
    Oct 28, 2017
    Posts:
    4
    Thank you for your reply and I am sure I need to add more info.
    The item I want serialized is an array holding custom 'playing' cards.

    So from the SineAnimation example, any variable you want to use serializedobject on must be tagged with [SerializeField]?
    In the continuation of the SineAnimation example I see no direct statement assigning serializedObject like above:

    Code (CSharp):
    1. var serializedObject = new SerializedObject(cards);
    Is that because it is part of the UnityEditor namespace?

    On a aside, I am having a time laying out the widgets for my window's UI and was wondering if you could recommend any modern books that deal with Unity's UI. There are a number of them I know, but do you know of a straightforward and accurate one for Unity 2019.x.x.x?

    Thank you.
     
  4. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,135
    Yes you do need to add the SerializeField attribute to the cards field to make it work with SerializedProperty since it's a private field, which Unity won't serialize by default.

    If you're referring to the SineAnimationEditor example that is because the class extends Editor which has the serializedObject property, so there's no need to create a new instance. When you're not making a custom editor you'll usually need to create the instance yourself.

    I don't have any book recommendations to give you on this front, sorry. If you're planning on using the new UIElements there might not be any books written about it yet since the feature only just came out. The new UI Builder is probably your best option for creating your window.