- Home /
How do i go about generating stuff in editor and keeping it in play mode
So, i'm generating the track using segments and attaching them to the end of the track. I created a custom editor script to add "generate" button to the inspector of my track object. In editor it works fine, but as soon as i hit play, my list of segments is empty, while scene still have those segment objects as children of track. I tried to assign some assets to segments in the editor and it keeps them. So i guess i'm doing something wrong with serialization considering unity only serializes values if they are set in inspector.
List is defined like that:
[SerializeField]
public List<GameObject> segments;
And i add segments with that:
void addRandomSegmentsToTrack(int segmentsToAdd)
{
initializeIfNoSegmentsPresent();
foreach (var segment in Range(0, segmentsToAdd))
{
segments.Add(Instantiate(segmentPrefab, trackEndPoint, trackEndRotation));
getSegmentScript(segments.Last()).constructSegment();
segments.Last().transform.parent = this.transform;
updateTrackEndPointAndRotation();
}
}
void initializeIfNoSegmentsPresent()
{
if(!segments.Any())
{
trackEndPoint = transform.position;
trackEndRotation = Quaternion.identity;
}
}
So i really puzzled how can i do something in editor and keep this changes in play mode?
I also have problem with unity events calling object methods in the editor. Maybe i'm making architectural mistakes by decentralizing everything and just passing references to scene components in inspector?
Answer by CodesCove · May 31, 2021 at 03:01 PM
Long story short: just set the data dirty after the changes or record object before making the changes. And to be sure it's saved save the scene automatically (optional)..
Preferred way (to support also undo)
Undo.RecordObject(segments, "My undo add segment"); //this goes before making the change
optional: EditorSceneManager.SaveScene(EditorSceneManager.GetActiveScene());
or like this (this will not record the change)
EditorUtility.SetDirty(segments); //this goes after the changes
optional: EditorSceneManager.SaveScene(EditorSceneManager.GetActiveScene());
or set the whole scene dirty (this will not record the changes)
EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene()); //this goes after the changes
optional: EditorSceneManager.SaveScene(EditorSceneManager.GetActiveScene());
Notice! Save the scene only once when you have iterated to whole list..
hope you get it working!
Hey, thanks a lot! Setting
Undo.RecordObject(this, "Undo on generation");
EditorSceneManager.SaveScene(EditorSceneManager.GetActiveScene());
before proceeding to clear the track and generating new segments works. Now it persists between edit and play mode. Although undo button doesn't change much (only gives me a glitch on regeneration when not all segments are deleted from previous one). Also it seems like a bit of a hack, recording changes for undo only to store them in game object.
Answer by SadeqSoli · May 31, 2021 at 02:36 PM
Hi @mixer468 , I didn't understand why you want to do something in editor first then go to the runtime??? and I also didn't understand your problem with unity events... can you explain more please??? . BTW you can instantiate game objects at AWAKE Or START Method and put them as a child of a parent with a Horizontal, Vertical Or even Grid Layout group as you need them. Also there is the trick of pooling system to make sure it's not an expensive call on startup methods and don't give you any Calculating time, Anyway I hope I got your problem correctly and of course if you'd explain it better, I can help better I believe. good luck, cheers.
Hello, thanks for the answer. Although my question about persistent data already answered, i want to add a little. To elaborate more on the question, game objects generated by my script are left in the scene after play mode started, the problem is, all the data in attached scripts is empty, so i can't get info from segments which is generated on creation.
As for the architectural question, i really think i might be doing something wrong, but in my $$anonymous$$d i really want every game object be on it's own. As for example a camera cold be a child of a track object or a player object, but i have all kinds of play modes in $$anonymous$$d where camera should follow different objects, so i don't want to attach it, just reference scene objects in my camera script to follow accordingly to game state. And as far as implementing events in editor, i really think that it's logical to start work on camera placement in editor mode. For example, i fire an event on track generation, it would be convenient for camera to attach to the correct place of a track on this event, i could've quickly debug it just by visuals if it attaches incorrectly.