Hello everyone,
I’m having some trouble with my Scriptable Object in Unity. I’m trying to use it to store data for my game, whenever I enter play mode or restart Unity, the data is lost.
I am trying to create a Scriptable Object that allows me to easily specify the boundary rules for a procedural generation project. Here are my two scripts: the main script and the editor.
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
[CreateAssetMenu(fileName = "New Prototype", menuName = "Prototype")]
public class Prototype : ScriptableObject
{
public Tile tile;
private Dictionary<int, bool[]> connections = new Dictionary<int, bool[]>();
public bool[] GetConnections(int id)
{
if (!connections.ContainsKey(id))
{
connections[id] = new bool[4];
}
return connections[id];
}
}
using System.Linq;
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(Prototype))]
public class PrototypeEditor : Editor
{
private Prototype prototype;
private void OnEnable()
{
prototype = (Prototype) target;
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
Prototype[] prototypes = AssetDatabase.FindAssets("t:Prototype")
.Select(guid => AssetDatabase.LoadAssetAtPath<Prototype>(AssetDatabase.GUIDToAssetPath(guid)))
.ToArray();
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField("", GUILayout.Width(EditorGUIUtility.labelWidth - 4f));
EditorGUILayout.LabelField("Right", GUILayout.Width(40f));
EditorGUILayout.LabelField("Left", GUILayout.Width(40f));
EditorGUILayout.LabelField("Up", GUILayout.Width(40f));
EditorGUILayout.LabelField("Down", GUILayout.Width(40f));
EditorGUILayout.EndHorizontal();
foreach (var p in prototypes)
{
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField(p.name, GUILayout.Width(EditorGUIUtility.labelWidth - 4f));
bool[] connections = prototype.GetConnections(p.GetInstanceID());
connections[0] = EditorGUILayout.Toggle(connections[0], GUILayout.Width(40f));
connections[1] = EditorGUILayout.Toggle(connections[1], GUILayout.Width(40f));
connections[2] = EditorGUILayout.Toggle(connections[2], GUILayout.Width(40f));
connections[3] = EditorGUILayout.Toggle(connections[3], GUILayout.Width(40f));
EditorGUILayout.EndHorizontal();
}
}
public override Texture2D RenderStaticPreview(string assetPath, Object[] subAssets, int width, int height)
{
Prototype scriptableObject = target as Prototype;
if (scriptableObject == null) return null;
Texture2D icon = null;
if (scriptableObject.tile != null)
{
icon = AssetPreview.GetAssetPreview(scriptableObject.tile);
}
if (icon == null)
{
icon = base.RenderStaticPreview(assetPath, subAssets, width, height);
}
return icon;
}
}
This results in the desired setup seen below.
I am able to check the boxes and see the updates, but the data is lost if I enter play mode or restart unity.
I’ve tried calling AssetDatabase.SaveAssets(), SetDirty(), and trying various methods to serialize the data, but nothing is working.
Does anyone know how I can fix this issue? Any help would be greatly appreciated.
Thank you in advance!