- Home /
Objects created in EditorWindow do not persist after reloading project
Hello, I've looked through a few similar questions and didn't find quite what I was looking for. There were a few questions whose solutions involved serializable fields and scriptable objects, but this felt a bit different.
Suppose I have a simple editor window that creates an object. The object will appear in the scene hierarchy correctly, but will not persist through closing / reloading the project scene.
If I manually change the object (rename, change parent, etc) using the Scene Editor itself, then the object will persist. I'm assuming there is a trivial solution for this, but have not had much time to investigate.
Thanks:
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
public class CreateObjectWindow : EditorWindow
{
// Keep track of what we've made
private static List<GameObject> createdObjects;
[MenuItem ("Window/CreateObject")]
public static void OpenCreateObjectWindow ()
{
// Make the window
var window = EditorWindow.GetWindow (typeof(CreateObjectWindow));
// Initialize the list
createdObjects = new List<GameObject> ();
}
void OnGUI ()
{
// Simple single button
if ( GUILayout.Button ("Create Object!") )
{
// Make some empty object
GameObject go = new GameObject ("made in editor!");
createdObjects.Add (go);
// Check if this is persistent (which it will not be)
bool persists = EditorUtility.IsPersistent (go);
Debug.LogFormat ("New object will persist? {0}", persists);
}
}
}
Have u tried creating the game object using Instantiate(...);
?
Answer by ShadyProductions · Mar 22, 2017 at 03:05 PM
obsolete reference but it did point me in the right direction for a solution:
// Get the current scene we're using
var currentScene = UnityEditor.Scene$$anonymous$$anagement.EditorScene$$anonymous$$anager.GetActiveScene ();
// $$anonymous$$ake it dirty (or save will ignore it) and then force a save call
UnityEditor.Scene$$anonymous$$anagement.EditorScene$$anonymous$$anager.$$anonymous$$arkSceneDirty (currentScene);
UnityEditor.Scene$$anonymous$$anagement.EditorScene$$anonymous$$anager.SaveScene (currentScene);