Saving ScriptableObjects in EditorWindow
Hi all,
I'm having an issue with saving scriptable objects in either editor window or a scriptable object. Currently, I have scriptable object nodes/windows that are being inherited from a base node:
[System.Serializable]
public class SC_BehaviourTree_BaseBehaviourNode : ScriptableObject
{
[HideInInspector]
public Rect windowRect;
[HideInInspector]
public string windowTitle;
[HideInInspector]
public List<SC_BehaviourTree_BaseBehaviourNode> Nodes = new List<SC_BehaviourTree_BaseBehaviourNode>();
[HideInInspector]
public bool drawConnectingLine = false;
public float windowRectHeight = 30;
public float collapsedRectHeight = 60;
[HideInInspector]
public SC_BehaviourTree_BaseBehaviourNode StartingNode;
[HideInInspector]
public SC_BehaviourTree_BaseBehaviourNode EndingNode;
public bool collapse = false;
public bool originNode = false;
[HideInInspector]
public Vector2 startPosOffset;
Which is then being saved by the windows editor to another scriptable object.
void SetNodesDirty()
{
EditorUtility.SetDirty(saveObject);
foreach(SC_BehaviourTree_BaseBehaviourNode Node in saveObject.savedWindowList)
{
EditorUtility.SetDirty(Node);
if (Node.StartingNode != null)
{
EditorUtility.SetDirty(Node.StartingNode);
}
if (Node.EndingNode != null)
{
EditorUtility.SetDirty(Node.EndingNode);
}
if (Node.Nodes.Count > 0)
{
foreach (SC_BehaviourTree_BaseBehaviourNode ChildNodes in Node.Nodes)
{
EditorUtility.SetDirty(ChildNodes);
}
}
}
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
void OnDisable()
{
SetStartOffset();
saveObject.savedWindowList = windows;
}
void OnEnable()
{
saveObject = Resources.Load<SC_BehaviourTree_LoadSave>("Behaviour Tree/Behaviour Tree Save and Load");
Debug.Log(saveObject.savedWindowList);
for (int i = 0; i < saveObject.savedWindowList.Count; i++)
{
if (saveObject.savedWindowList[i] != null)
{
windows[i] = saveObject.savedWindowList[i];
}
}
Debug.Log(windows.Count + " Behaviour Tree Windows");
FindOriginNode();
SetStartPosition();
}
[CreateAssetMenu(menuName = "Behaviour Tree / Save File")]
[System.Serializable]
public class SC_BehaviourTree_LoadSave: ScriptableObject
{
[HideInInspector]
public List<SC_BehaviourTree_BaseBehaviourNode> savedWindowList = new List<SC_BehaviourTree_BaseBehaviourNode>();
}
So effectively, what I need to be able to do is save the list of behavior nodes from the behavior tree editor and then reload them and their current variables on enable.
Answer by Bunny83 · Jun 20, 2020 at 12:12 PM
Uhm, have you actually stored those scriptable objects in any asset file? If not It could only be saved inside the currently loaded scene. But things saved inside a scene can not be referenced by an asset. Only assets can be referenced by other assets. So when you create your scriptable objects you either have to use AssetDatabase.CreateAsset to create a standalone asset or use AssetDatabase.AddObjectToAsset to add the asset to an existing asset file.
Your answer
Follow this Question
Related Questions
How to Serialize or Save a List of GameObjects 1 Answer
.data serialize and deserialize work in editor but not in build 1 Answer
ScriptableObject not Serializing? 0 Answers
EditorGUILayout.ObjectField on a ScriptableObject's inspector doesn't update properly 0 Answers
Loading and Saving High Score 1 Answer