Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[SOLVED] Custom Editor - Multi-object editing of classes that don't derive from UnityEngine.Object

Discussion in 'Editor & General Support' started by Talthilas, Sep 12, 2018.

  1. Talthilas

    Talthilas

    Joined:
    Apr 1, 2014
    Posts:
    44
    Hi all,

    I have made a custom editor window that works with a scriptable object for storing and editing data. The scriptable object contains a list with a bunch of custom classes as the items. The editor provides a means to edit the data in these classes. For explanatory purposes, lets call the items in my list dataItems.

    The problem I am trying to find a solution to is multi-dataItem editing. Basically i want to selected multiple items in my list, and change a single property for each of the selected items when I change a field in the editor (for simplicity lets just say that dataItems only contain is a bunch of integers, so all I am doing in the editor window is drawing bunch of integer fields with EditorGUILayout.IntField()).

    Initially I went down the road of using serializedObject and serializedProperty for editing multiple items, but since my dataItems are just regular classes that do not derive from UnityEngine.Object, I couldn't use those (or at least I couldn't work out how I could use those in my situation). Just to reiterate, my dataItems are just custom classes that exist in a list, and the list exists in the scriptable object. I can selected multiple dataItems in the list with no issue, however the problem I have to overcome is to determine which field or property was just changed in the editor, so I can update the same field in each of the selected dataItems in the list.

    Anyone got any ideas?

    Thanks all.
     
    Last edited: Sep 12, 2018
  2. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    Sorry I don't have your answer, but you should be able to get a SerializedObject from a ScriptableObject and get a SerializedProperty from any serialized data fields in the object. Any data fields that are not serialized will not save any edits you make to them no matter what you do!

    A saved prefab, scene, or asset is actually a Serializable object in it's serialized form. It is not possible to save the data from a class using unity editor functions unless the object is Serializable. If the object is Serializable, it can be turned into a SerializedObject. Only serializable data fields in the SerializedObject will have their values saved in Unity.

    ScriptableObjects are serializable, otherwise they would not be able to be saved as an asset file or modified in the Unity inspector without a custom editor. In fact, the API documentation for SerializedObject uses a ScriptableObject as an example. https://docs.unity3d.com/ScriptReference/SerializedObject.html
     
  3. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    I probably should have also mentioned that you can make non-Monobehaviour non-ScriptableObjects classes serializable by adding
    [System.Serializable]
    before your class declaration. Make sure the fields of the class are serializable by either making them public or by adding
    [SerializedField]
    before their declaration.
     
  4. Talthilas

    Talthilas

    Joined:
    Apr 1, 2014
    Posts:
    44
    Hi Gambit,

    Thanks for the reply. I think you may have misunderstood my question. I am familiar with serialized objects/properties, scriptable objects, and serialization in general as I use them extensively in my game. My problem is that as far as I know, multi-object editing only works when you are dealing with multiple objects that derive from UnityEngine.Object. A scriptable object or asset etc all derive from UnityEngine.Object, which is why multi-object editing works with those. In my case I only have one scriptable object. I am try to edit multiple items within a list inside that scriptable object, so although those items have the Serializable attribute, they are not UnityEngine.Objects and as such do not have multi-object support. So just to clarify my question is regarding multi-object editing, not serialization of custom classes.

    Really all I need to do is determine which field was just changed in the editor window so I can update all currently selected items to match that field. If anyone knows how to do this, that would be great.

    Cheers!
     
  5. Talthilas

    Talthilas

    Joined:
    Apr 1, 2014
    Posts:
    44
    To those interested, I found a solution to the above problem. I discovered the following methods EditorGUI.BeginChangeCheck() and EditorGUI.EndChangeCheck() which allowed me to determine which field was changed. Once I knew that I was able to update the same field in all currently selected items using reflection. Works perfectly.