Custom Inspector for ScriptableObject (611962)

Hello guys! I have some problem. Please help me.

This my ScriptableObject

    [System.Serializable]
    public class dm_MapTiles : ScriptableObject
    {

        public dm_MapTiles() { }

        public string mapName;
        public Sprite[,] mapSprites;

    }

And this is my Custom Editor Script

using UnityEditor;
using UnityEngine;
using System.Collections.Generic;

[CustomEditor(typeof(dm_MapTiles))]
public class dm_MapTilesEditor : Editor {

    dm_MapTiles comp;
    static bool showTileEditor = false;

    public void OnEnable()
    {
        comp = (dm_MapTiles)target;
        if (comp.mapSprites == null)
        {
            comp.mapSprites = new Sprite[1,1];
        }
    }

    public override void OnInspectorGUI()
    {
        //MAP DEFAULT INFORMATION
        comp.mapName = EditorGUILayout.TextField("Name", comp.mapName);

        //WIDTH - HEIGHT
        int width = EditorGUILayout.IntField("Map Sprite Width", comp.mapSprites.GetLength(0));
        int height = EditorGUILayout.IntField("Map Sprite Height", comp.mapSprites.GetLength(1));

        if (width != comp.mapSprites.GetLength(0) || height != comp.mapSprites.GetLength(1))
        {
            comp.mapSprites = new Sprite[width, height];
        }

        showTileEditor = EditorGUILayout.Foldout(showTileEditor, "Tile Editor");

        if (showTileEditor)
        {
            for (int h = 0; h < height; h++)
            {
                EditorGUILayout.BeginHorizontal();
                for (int w = 0; w < width; w++)
                {
                    comp.mapSprites[w, h] = (Sprite)EditorGUILayout.ObjectField(comp.mapSprites[w, h], typeof(Sprite), false, GUILayout.Width(65f), GUILayout.Height(65f));
                }
                EditorGUILayout.EndHorizontal();
            }
        }
    }

}

And this my screenshot

If I Play game, my scriptableobject lost all data. Please help me.

At the end of your OnGUI method try to call EditorUtility.SetDirty (Unity - Scripting API: EditorUtility.SetDirty):

// mark "comp" as dirty so the editor knows it should be saved
EditorUtility.SetDirty(comp);

Hey there,

You are also storing your sprites as a multidimensional array which Unity will not serialize. So no matter what you do you will not be able to write your sprite references to disk.

Cheers,

3 Likes