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. Dismiss Notice

Custom editor does not register changes to my object

Discussion in 'Immediate Mode GUI (IMGUI)' started by Adagio-81, Mar 11, 2016.

  1. Adagio-81

    Adagio-81

    Joined:
    Mar 28, 2015
    Posts:
    18
    I have a simple object with an array of data:

    Code (CSharp):
    1. [Serializable]
    2. public class MapGenerator : MonoBehaviour
    3. {
    4.     [SerializeField]
    5.     private int mapHeight = 0;
    6.  
    7.     [SerializeField]
    8.     private int mapWidth = 0;
    9.  
    10.     [SerializeField]
    11.     public TileTypes[] Tiles;
    12. }
    I started (like many others) with creating a 2D array, but have realized Unity doesn't work well with 2D arrays.
    Anyway. I then created a simpleeditor:

    Code (CSharp):
    1. [CustomEditor(typeof(MapGenerator))]
    2. public class MapEditor : Editor
    3. {
    4.     public override void OnInspectorGUI()
    5.     {
    6.         MapGenerator map = target as MapGenerator;
    7.  
    8.         map.MapHeight = EditorGUILayout.IntSlider("Map Height", map.MapHeight, 0, 15);
    9.         map.MapWidth = EditorGUILayout.IntSlider("Map Width", map.MapWidth, 0, 15);
    10.  
    11.         for (int row = 0; row < map.MapHeight; row++)
    12.         {
    13.             for (int column = 0; column < map.MapWidth; column++)
    14.             {
    15.                 map.Tiles[(row * map.MapWidth) + column] = (TileTypes)EditorGUILayout.EnumPopup(map.Tiles[(row * map.MapWidth) + column]);
    16.             }
    17.         }
    18.  
    19.         SceneView.RepaintAll();
    20.     }
    21. }
    All this works a little bit. If I change a few tiles it does it correctly in the code and it shows correctly when playing. Unfortunately it doesn't save when closing. No matter what I change on the map, whenever I close Unity, Unity closes down without saving any of the changes. It obviously doesn't register I have made a change. Even if I manually click on the Save button before I close Unity, it will not save any changes made
    On the other hand, if I make a few changes on the map, then in the scene make a change to another object (e.g. move the camera a bit) and closes Unity, it will ask if I want to save the changes. If I press yes to save the changes, it will also save the changes to the map.

    Any idea on why it will only save changes to my map, if I make changes to other objects in the scene?
     
  2. seldom

    seldom

    Joined:
    Dec 4, 2013
    Posts:
    118
    You need to set the object as dirty when it was modified:
    Code (CSharp):
    1. EditorGUI.BeginChangeCheck ();
    2. ...
    3. if( EditorGUI.EndChangeCheck())
    4.     EditorUtility.SetDirty(map);
     
  3. Adagio-81

    Adagio-81

    Joined:
    Mar 28, 2015
    Posts:
    18
    Thanks, but it doesn't seem to make any difference. The SetDirty(map) code is called when making a chance, but Unity still does not register any change on the object and closing Unity will still leave it unsaved

    Edit:

    By searching for your code, I managed to find this code that fixes the problem:

    Code (CSharp):
    1. UnityEditor.SceneManagement.EditorSceneManager.MarkAllScenesDirty();
    Thanks for the help :)
     
    Last edited: Mar 16, 2016