public SourceSceneManager(SceneAsset scene) { string name = scene.name; int id = scene.GetInstanceID(); _data = new SceneData(name, id, scene); }
/// <summary> /// Refresh all the dependencies of the template. /// </summary> public void UpdateDependencies() { if (!IsValid) { dependencies = new DependencyInfo[0]; return; } var scenePath = AssetDatabase.GetAssetPath(templateScene.GetInstanceID()); if (string.IsNullOrEmpty(scenePath)) { dependencies = new DependencyInfo[0]; return; } var sceneName = Path.GetFileNameWithoutExtension(scenePath); var sceneFolder = Path.GetDirectoryName(scenePath).Replace("\\", "/"); var sceneCloneableDependenciesFolder = Path.Combine(sceneFolder, sceneName).Replace("\\", "/"); var depList = new List <Object>(); ReferenceUtils.GetSceneDependencies(scenePath, depList); dependencies = depList.Select(d => { var oldDependencyInfo = dependencies.FirstOrDefault(di => di.dependency.GetInstanceID() == d.GetInstanceID()); if (oldDependencyInfo != null) { return(oldDependencyInfo); } var depTypeInfo = SceneTemplateProjectSettings.Get().GetDependencyInfo(d); var dependencyPath = AssetDatabase.GetAssetPath(d); var instantiationMode = depTypeInfo.defaultInstantiationMode; if (depTypeInfo.supportsModification && !string.IsNullOrEmpty(dependencyPath)) { var assetFolder = Path.GetDirectoryName(dependencyPath).Replace("\\", "/"); if (assetFolder == sceneCloneableDependenciesFolder) { instantiationMode = TemplateInstantiationMode.Clone; } } return(new DependencyInfo() { dependency = d, instantiationMode = instantiationMode }); }).ToArray(); }
public int IsSceneInBuildSettings() { if (scene == null) { return(-1); } EditorBuildSettingsScene[] scenes = EditorBuildSettings.scenes; for (int i = 0; i < EditorBuildSettings.scenes.Length; i++) { if (scenes[i].path == AssetDatabase.GetAssetPath(scene.GetInstanceID())) { return(i); } } return(-1); }
void CreateSceneRootPerfab(SceneAsset sceneAsset) { Scene needOpenScene = EditorSceneManager.OpenScene(AssetDatabase.GetAssetPath(sceneAsset.GetInstanceID())); List <GameObject> sceneRootGameObjects = new List <GameObject>(); sceneRootGameObjects.AddRange(needOpenScene.GetRootGameObjects()); foreach (GameObject g in sceneRootGameObjects) { if (CheckNeedRemoveObject(g)) { sceneRootGameObjects.Remove(g); } } GameObject sceneRoot = new GameObject(); sceneRoot.name = name + "SceneRoot"; foreach (GameObject g in sceneRootGameObjects) { g.transform.parent = sceneRoot.transform; } string createPrefabPath = "Assets/intermediate/" + needOpenScene.name + "_SceneRoot.prefab"; GameObject createPrefab = PrefabUtility.CreatePrefab(createPrefabPath, sceneRoot); AssetImporter assetImporter = AssetImporter.GetAtPath(createPrefabPath); //得到Asset assetImporter.assetBundleName = createPrefab.name.ToLower(); //最终设置assetBundleName EditorSceneManager.NewScene(new NewSceneSetup()); }
internal static InstantiationResult Instantiate(SceneTemplateAsset sceneTemplate, bool loadAdditively, string newSceneOutputPath, SceneTemplateAnalytics.SceneInstantiationType instantiationType) { if (!sceneTemplate.isValid) { throw new Exception("templateScene is empty"); } if (EditorApplication.isUpdating) { Debug.LogFormat(LogType.Warning, LogOption.None, null, "Cannot instantiate a new scene while updating the editor is disallowed."); return(null); } // If we are loading additively, we cannot add a new Untitled scene if another unsaved Untitled scene is already opened if (loadAdditively && SceneTemplateUtils.HasSceneUntitled()) { Debug.LogFormat(LogType.Warning, LogOption.None, null, "Cannot instantiate a new scene additively while an unsaved Untitled scene already exists."); return(null); } var sourceScenePath = AssetDatabase.GetAssetPath(sceneTemplate.templateScene); if (String.IsNullOrEmpty(sourceScenePath)) { throw new Exception("Cannot find path for sceneTemplate: " + sceneTemplate.ToString()); } if (!Application.isBatchMode && !loadAdditively && !EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo()) { return(null); } var instantiateEvent = new SceneTemplateAnalytics.SceneInstantiationEvent(sceneTemplate, instantiationType) { additive = loadAdditively }; sceneTemplate.UpdateDependencies(); var hasAnyCloneableDependencies = sceneTemplate.hasCloneableDependencies; SceneAsset newSceneAsset = null; Scene newScene; var templatePipeline = sceneTemplate.CreatePipeline(); if (!InstantiateInMemoryScene(sceneTemplate, sourceScenePath, ref newSceneOutputPath, out var rootFolder, out var isTempMemory)) { instantiateEvent.isCancelled = true; SceneTemplateAnalytics.SendSceneInstantiationEvent(instantiateEvent); return(null); } templatePipeline?.BeforeTemplateInstantiation(sceneTemplate, loadAdditively, isTempMemory ? null : newSceneOutputPath); newSceneTemplateInstantiating?.Invoke(sceneTemplate, isTempMemory ? null : newSceneOutputPath, loadAdditively); newSceneAsset = AssetDatabase.LoadAssetAtPath <SceneAsset>(newSceneOutputPath); var refPathMap = new Dictionary <string, string>(); var idMap = new Dictionary <int, int>(); if (hasAnyCloneableDependencies) { var clonedAssets = CopyCloneableDependencies(sceneTemplate, newSceneOutputPath, ref refPathMap); idMap.Add(sceneTemplate.templateScene.GetInstanceID(), newSceneAsset.GetInstanceID()); ReferenceUtils.RemapAssetReferences(refPathMap, idMap); foreach (var clone in clonedAssets) { if (clone) { EditorUtility.SetDirty(clone); } } AssetDatabase.SaveAssets(); } newScene = EditorSceneManager.OpenScene(newSceneOutputPath, loadAdditively ? OpenSceneMode.Additive : OpenSceneMode.Single); if (hasAnyCloneableDependencies) { EditorSceneManager.RemapAssetReferencesInScene(newScene, refPathMap, idMap); } EditorSceneManager.SaveScene(newScene, newSceneOutputPath); if (isTempMemory) { newSceneAsset = null; newScene.SetPathAndGuid("", newScene.guid); s_CurrentInMemorySceneState.guid = newScene.guid; s_CurrentInMemorySceneState.rootFolder = rootFolder; s_CurrentInMemorySceneState.hasCloneableDependencies = hasAnyCloneableDependencies; s_CurrentInMemorySceneState.dependencyFolderName = Path.GetFileNameWithoutExtension(newSceneOutputPath); } SceneTemplateAnalytics.SendSceneInstantiationEvent(instantiateEvent); templatePipeline?.AfterTemplateInstantiation(sceneTemplate, newScene, loadAdditively, newSceneOutputPath); newSceneTemplateInstantiated?.Invoke(sceneTemplate, newScene, newSceneAsset, loadAdditively); SceneTemplateUtils.SetLastFolder(newSceneOutputPath); return(new InstantiationResult(newScene, newSceneAsset)); }
private void editorGUI(bool removeButton) { EditorGUILayout.BeginVertical(GUI.skin.box); GUILayout.BeginHorizontal(EditorStyles.inspectorDefaultMargins); scriptableScene.isExpanded = EditorGUILayout.Foldout(scriptableScene.isExpanded, new GUIContent(scriptableScene.name), true, EditorStyles.foldout); SceneAsset oldScene = AssetDatabase.LoadAssetAtPath <SceneAsset>(scriptableScene.scenePath); serializedObject.Update(); // Display a button showing a '-' that if clicked removes this Condition from the AllConditions asset. if (removeButton && EditorTools.createListButton("-", true, GUILayout.Width(removeButtonWidth))) { AllGameScriptableScenesEditor.RemoveScriptableScene(scriptableScene); return; } GUILayout.EndHorizontal(); if (scriptableScene.isExpanded) { EditorGUI.BeginChangeCheck(); EditorGUI.indentLevel += 1; EditorGUILayout.LabelField("Unity Scene"); EditorGUI.indentLevel -= 1; GUILayout.BeginVertical(EditorStyles.inspectorDefaultMargins); SceneAsset newScene = EditorGUILayout.ObjectField(oldScene, typeof(SceneAsset), false) as SceneAsset; GUILayout.Space(5); EditorTools.createArrayPropertyButtons(sceneStartingPositionsNamesProperty, sceneStartingPositionsNamesPropName, GUILayout.Width(30f), true); if (sceneStartingPositionsNamesProperty.arraySize > 0) { for (int i = 0; i < sceneStartingPositionsNamesProperty.arraySize; i++) { GUILayout.Space(5); EditorGUILayout.BeginVertical(GUI.skin.box); EditorGUILayout.PropertyField(sceneStartingPositionsNamesProperty.GetArrayElementAtIndex(i), new GUIContent("Position Name " + i), true); EditorGUILayout.EndVertical(); } } GUILayout.EndVertical(); if (newScene) { scriptableScene.name = newScene.name; } if (EditorGUI.EndChangeCheck() && newScene != null) { string newPath = AssetDatabase.GetAssetPath(newScene.GetInstanceID()); scenePathProperty.stringValue = newPath; AssetDatabase.SaveAssets(); } } EditorGUILayout.EndVertical(); // Push the values from the serializedObject back to the target. serializedObject.ApplyModifiedProperties(); }
internal static System.Tuple <Scene, SceneAsset> Instantiate(SceneTemplateAsset sceneTemplate, bool loadAdditively, string newSceneOutputPath, SceneTemplateAnalytics.SceneInstantiationType instantiationType) { if (!sceneTemplate.IsValid) { throw new Exception("templateScene is empty"); } var sourceScenePath = AssetDatabase.GetAssetPath(sceneTemplate.templateScene); if (String.IsNullOrEmpty(sourceScenePath)) { throw new Exception("Cannot find path for sceneTemplate: " + sceneTemplate.ToString()); } if (!Application.isBatchMode && !loadAdditively && !EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo()) { return(null); } var instantiateEvent = new SceneTemplateAnalytics.SceneInstantiationEvent(sceneTemplate, instantiationType) { additive = loadAdditively }; sceneTemplate.UpdateDependencies(); var hasAnyCloneableDependencies = sceneTemplate.dependencies.Any(dep => dep.instantiationMode == TemplateInstantiationMode.Clone); SceneAsset newSceneAsset = null; Scene newScene; var templatePipeline = sceneTemplate.CreatePipeline(); if (hasAnyCloneableDependencies) { if (!InstantiateScene(sceneTemplate, sourceScenePath, ref newSceneOutputPath)) { instantiateEvent.isCancelled = true; SceneTemplateAnalytics.SendSceneInstantiationEvent(instantiateEvent); return(null); } templatePipeline?.BeforeTemplateInstantiation(sceneTemplate, loadAdditively, newSceneOutputPath); newSceneTemplateInstantiating?.Invoke(sceneTemplate, newSceneOutputPath, loadAdditively); var refPathMap = new Dictionary <string, string>(); var refMap = CopyCloneableDependencies(sceneTemplate, newSceneOutputPath, ref refPathMap); newScene = EditorSceneManager.OpenScene(newSceneOutputPath, loadAdditively ? OpenSceneMode.Additive : OpenSceneMode.Single); newSceneAsset = AssetDatabase.LoadAssetAtPath <SceneAsset>(newSceneOutputPath); var idMap = new Dictionary <int, int>(); idMap.Add(sceneTemplate.templateScene.GetInstanceID(), newSceneAsset.GetInstanceID()); EditorSceneManager.RemapAssetReferencesInScene(newScene, refPathMap, idMap); EditorSceneManager.SaveScene(newScene, newSceneOutputPath); foreach (var clone in refMap.Values) { if (clone) { EditorUtility.SetDirty(clone); } } AssetDatabase.SaveAssets(); } else { templatePipeline?.BeforeTemplateInstantiation(sceneTemplate, loadAdditively, newSceneOutputPath); newSceneTemplateInstantiating?.Invoke(sceneTemplate, newSceneOutputPath, loadAdditively); if (loadAdditively) { newScene = EditorSceneManager.OpenScene(sourceScenePath, OpenSceneMode.Additive); } else { newScene = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single); var sourceScene = EditorSceneManager.OpenScene(sourceScenePath, OpenSceneMode.Additive); SceneManager.MergeScenes(sourceScene, newScene); } } SceneTemplateAnalytics.SendSceneInstantiationEvent(instantiateEvent); templatePipeline?.AfterTemplateInstantiation(sceneTemplate, newScene, loadAdditively, newSceneOutputPath); newSceneTemplateInstantiated?.Invoke(sceneTemplate, newScene, newSceneAsset, loadAdditively); SceneTemplateUtils.SetLastFolder(newSceneOutputPath); return(new System.Tuple <Scene, SceneAsset>(newScene, newSceneAsset)); }