public override void OnImportAsset(AssetImportContext ctx) { try { ctx.DependsOnCustomDependency("EntityBinaryFileFormatVersion"); var sceneWithBuildConfiguration = SceneWithBuildConfigurationGUIDs.ReadFromFile(ctx.assetPath); // Ensure we have as many dependencies as possible registered early in case an exception is thrown var scenePath = AssetDatabase.GUIDToAssetPath(sceneWithBuildConfiguration.SceneGUID.ToString()); ctx.DependsOnSourceAsset(scenePath); if (sceneWithBuildConfiguration.BuildConfiguration.IsValid) { var buildConfigurationPath = AssetDatabase.GUIDToAssetPath(sceneWithBuildConfiguration.BuildConfiguration.ToString()); ctx.DependsOnSourceAsset(buildConfigurationPath); var buildConfigurationDependencies = AssetDatabase.GetDependencies(buildConfigurationPath); foreach (var dependency in buildConfigurationDependencies) { ctx.DependsOnSourceAsset(dependency); } } var dependencies = AssetDatabase.GetDependencies(scenePath); foreach (var dependency in dependencies) { if (dependency.ToLower().EndsWith(".prefab")) { ctx.DependsOnSourceAsset(dependency); } } var config = BuildConfiguration.LoadAsset(sceneWithBuildConfiguration.BuildConfiguration); var scene = EditorSceneManager.OpenScene(scenePath, OpenSceneMode.Additive); try { var settings = new GameObjectConversionSettings(); settings.SceneGUID = sceneWithBuildConfiguration.SceneGUID; settings.BuildConfiguration = config; settings.AssetImportContext = ctx; var sectionRefObjs = new List <ReferencedUnityObjects>(); var sectionData = EditorEntityScenes.WriteEntityScene(scene, settings, sectionRefObjs); WriteRefGuids(sectionRefObjs, sectionData, ctx); } finally { EditorSceneManager.CloseScene(scene, true); } } // Currently it's not acceptable to let the asset database catch the exception since it will create a default asset without any dependencies // This means a reimport will not be triggered if the scene is subsequently modified catch (Exception e) { Debug.Log($"Exception thrown during SubScene import: {e}"); } }
public static void SceneSavingCallback(Scene scene, string scenePath) { var sceneGUID = new GUID(AssetDatabase.AssetPathToGUID(scenePath)); if (EditorEntityScenes.HasEntitySceneCache(sceneGUID)) { EditorEntityScenes.WriteEntityScene(scene, sceneGUID, 0); } }
public override void OnImportAsset(AssetImportContext ctx) { try { var sceneWithBuildSettings = ReadSceneWithBuildSettings(ctx.assetPath); // Ensure we have as many dependencies as possible registered early in case an exception is thrown var scenePath = AssetDatabase.GUIDToAssetPath(sceneWithBuildSettings.SceneGUID.ToString()); ctx.DependsOnSourceAsset(scenePath); if (sceneWithBuildSettings.BuildSettings.IsValid) { var buildSettingPath = AssetDatabase.GUIDToAssetPath(sceneWithBuildSettings.BuildSettings.ToString()); ctx.DependsOnSourceAsset(buildSettingPath); var buildSettingDependencies = AssetDatabase.GetDependencies(buildSettingPath); foreach (var dependency in buildSettingDependencies) { ctx.DependsOnSourceAsset(dependency); } } var dependencies = AssetDatabase.GetDependencies(scenePath); foreach (var dependency in dependencies) { if (dependency.ToLower().EndsWith(".prefab")) { ctx.DependsOnSourceAsset(dependency); } } var buildSettings = BuildSettings.LoadBuildSettings(sceneWithBuildSettings.BuildSettings); var scene = EditorSceneManager.OpenScene(scenePath, OpenSceneMode.Additive); try { var settings = new GameObjectConversionSettings(); settings.SceneGUID = sceneWithBuildSettings.SceneGUID; settings.BuildSettings = buildSettings; settings.AssetImportContext = ctx; EditorEntityScenes.WriteEntityScene(scene, settings); } finally { EditorSceneManager.CloseScene(scene, true); } } // Currently it's not acceptable to let the asset database catch the exception since it will create a default asset without any dependencies // This means a reimport will not be triggered if the scene is subsequently modified catch (Exception e) { Debug.Log($"Exception thrown during SubScene import: {e.Message}"); } }
static void CreateSubSceneAndMoveObjectInside(Scene parentScene, Transform parent, GameObject[] objects, string name) { EditorSceneManager.MarkSceneDirty(parentScene); var srcPath = parentScene.path; var dstDirectory = Path.Combine(Path.GetDirectoryName(srcPath), Path.GetFileNameWithoutExtension(parentScene.path)); var dstPath = Path.Combine(dstDirectory, name + ".unity"); Directory.CreateDirectory(dstDirectory); var scene = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Additive); scene.isSubScene = true; foreach (var go in objects) { go.transform.SetParent(null, true); SceneManager.MoveGameObjectToScene(go, scene); } EditorSceneManager.SaveScene(scene, dstPath); var sceneAsset = AssetDatabase.LoadAssetAtPath <SceneAsset>(dstPath); var gameObject = new GameObject(name, typeof(SubScene)); gameObject.SetActive(false); var subScene = gameObject.GetComponent <SubScene>(); subScene.SceneAsset = sceneAsset; if (parent) { gameObject.transform.parent = parent; } else { SceneManager.MoveGameObjectToScene(gameObject, parentScene); } EditorEntityScenes.WriteEntityScene(subScene); gameObject.SetActive(true); Selection.activeObject = gameObject; }
public static void RebuildEntityCache(params SubScene[] scenes) { try { Profiler.BeginSample("AssetDatabase.StartAssetEditing"); AssetDatabase.StartAssetEditing(); Profiler.EndSample(); for (int i = 0; i != scenes.Length; i++) { var scene = scenes[i]; EditorUtility.DisplayProgressBar("Rebuilding Entity Cache", scene.SceneName, (float)i / scenes.Length); var isLoaded = scene.IsLoaded; if (!isLoaded) { EditScene(scene); } try { EditorEntityScenes.WriteEntityScene(scene); scene.UpdateSceneEntities(); } catch (Exception exception) { Debug.LogException(exception); } if (!isLoaded) { CloseSceneWithoutSaving(scene); } } } finally { Profiler.BeginSample("AssetDatabase.StopAssetEditing"); AssetDatabase.StopAssetEditing(); Profiler.EndSample(); EditorUtility.ClearProgressBar(); } }