Skip to content

Instantly share code, notes, and snippets.

@mandarinx
Created September 20, 2016 12:05
Show Gist options
  • Save mandarinx/289c01257e3d463a2f6b595b83859421 to your computer and use it in GitHub Desktop.
Save mandarinx/289c01257e3d463a2f6b595b83859421 to your computer and use it in GitHub Desktop.
Create a custom scene in Unity3D

Create a custom scene in Unity3D

When setting up scenes for test cases, you often need to make preparations like adding a few prefabs, changing light settings and so on. With a script like this, you can map a recipe for a scene setup to a keyboard shortcut.

using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;
static public class NewScene {
[MenuItem("File/New Custom Scene %&n")]
static public void CreateNewScene() {
if (!EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo()) {
return;
}
Scene scene = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene,
NewSceneMode.Single);
Instantiate("Prefabs/Main", scene);
Instantiate("Prefabs/CameraRig", scene);
RenderSettings.ambientIntensity = 1f;
RenderSettings.ambientLight = Color.white;
EditorSceneManager.MarkSceneDirty(scene);
}
static private void Instantiate(string asset, Scene scene) {
GameObject prefab = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/"+asset+".prefab");
PrefabUtility.InstantiatePrefab(prefab, scene);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment