public static void CreateNewGameLevel()
{
// Ask the user if they would like to save the current scene if need be, if they do not press cancel...
if (EditorApplication.SaveCurrentSceneIfUserWantsTo())
{
// ... then load a new scene.
EditorApplication.NewScene();
// We do not want the default `Main Camera` object do destroy that.
Object.DestroyImmediate(GameObject.Find("Main Camera"));
// For each required prefab file name...
foreach (string levelRequirement in LevelRequirementPrefabs)
{
// ... load the prefab from the asset database.
GameObject requiredPrefab = AssetDatabase.LoadAssetAtPath<GameObject>(RootLevelRequirements + levelRequirement);
// Instantiate it within the new scene connected to the prefab.
PrefabUtility.InstantiatePrefab(requiredPrefab);
}
// Reference the objects that need connecting to each other.
EndOfLevel endOfLevel = Object.FindObjectOfType<EndOfLevel>();
LevelController levelController = Object.FindObjectOfType<LevelController>();
CameraController cameraController = Object.FindObjectOfType<CameraController>();
Player player = Object.FindObjectOfType<Player>();
// Disconect required objects from prefabs if appropriate to do so.
PrefabUtility.DisconnectPrefabInstance(endOfLevel);
// Create a new serialized object of the end of level.
SerializedObject serializedEndOfLevel = new SerializedObject(endOfLevel);
// Set the appropriate properties to their correct values.
serializedEndOfLevel.FindProperty("menuSwitcher").objectReferenceValue = Object.FindObjectOfType<MenuSwitcher>();
// Apply the changed properties without an undo.
serializedEndOfLevel.ApplyModifiedPropertiesWithoutUndo();
// Create a new serialized object of the level controller.
SerializedObject serializedLevelController = new SerializedObject(levelController);
// Set the appropriate properties to their correct values.
serializedLevelController.FindProperty("cameraController").objectReferenceValue = cameraController;
serializedLevelController.FindProperty("player").objectReferenceValue = player;
// Apply the changed properties without an undo.
serializedLevelController.ApplyModifiedPropertiesWithoutUndo();
// Create a new serialized object of the camera controller.
SerializedObject serializedCameraController = new SerializedObject(cameraController);
// Set the appropriate properties to their correct values.
serializedCameraController.FindProperty("target").objectReferenceValue = player.GetComponent<CameraTarget>();
// Apply the changed properties without an undo.
serializedCameraController.ApplyModifiedPropertiesWithoutUndo();
}
}