(Programmatically) creating a scene with the same content, produces an .unity file with different content always. It seems the GameObject’s inside the scene file are stored randomly.
The provided example creates a new scene and places a few prefabs at hard-coded positions. It’s always the same. However, comparing the produced .unity file with a diff tool (such as TortoiseMerge), shows the scene file is different always.
This causes version control issues. Most likely different asset bundles as well, even though the actual scene is identical.
C# Source Code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
class CreateSceneTest
{
[MenuItem("Test/Create Scene A")]
static void DoMenuItem()
{
// create a new scene
var scene = UnityEditor.SceneManagement.EditorSceneManager.NewScene(UnityEditor.SceneManagement.NewSceneSetup.EmptyScene);
// create a floor place
var floor = (GameObject)PrefabUtility.InstantiatePrefab(AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Art/Floor.prefab"));
floor.transform.SetPositionAndRotation(Vector3.zero, Quaternion.identity);
floor.transform.SetAsLastSibling();
// create a few prefabs
foreach (var pos in new Vector3[] {
new Vector3(5, 0, 0),
new Vector3(0, 5, 0),
new Vector3(0, 0, 5),})
{
var cube = (GameObject)PrefabUtility.InstantiatePrefab(AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Art/Cube.prefab"));
cube.transform.SetPositionAndRotation(pos, Quaternion.identity);
cube.transform.SetAsLastSibling();
}
// save scene
UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(scene);
UnityEditor.SceneManagement.EditorSceneManager.SaveScene(scene, "Assets/Scenes/Scene A.unity");
}
}
Reproduce
- Open user attached project
- Press Mainmenu/Test/Create Scene A
- Duplicate generated “Scenes/Scene A.unity” and rename it to “Backup”
- Press Mainmenu/Test/Create Scene A
- Compare “Scenes/Scene A.unity” and rename it to “Scenes/Backup” with a diff tool
Observe the file content is different.
Expected
The produced file should be the same. If the input is identical, the output should be identical as well.