static void CreateSceneCachePlayerMenu(MenuCommand menuCommand) { string sceneCacheFilePath = EditorUtility.OpenFilePanelWithFilters("Select Cache File", "", new string[] { "All supported files", "sc", "All files", "*" }); if (string.IsNullOrEmpty(sceneCacheFilePath)) { return; } //Prefab and assets path MeshSyncRuntimeSettings runtimeSettings = MeshSyncRuntimeSettings.GetOrCreateSettings(); string scOutputPath = runtimeSettings.GetSceneCacheOutputPath(); string prefabFileName = Path.GetFileNameWithoutExtension(sceneCacheFilePath); string prefabPath = $"{scOutputPath}/{prefabFileName}.prefab"; string assetsFolder = Path.Combine(scOutputPath, prefabFileName); bool created = SceneCachePlayerEditorUtility.CreateSceneCachePlayerAndPrefab(sceneCacheFilePath, prefabPath, assetsFolder, out SceneCachePlayer player, out GameObject prefab); if (!created) { EditorUtility.DisplayDialog("MeshSync" , "Failed to open " + sceneCacheFilePath + ". Possible reasons: file format version does not match, or the file is not scene cache." , "Ok" ); } }
//---------------------------------------------------------------------------------------------------------------------- internal static void ChangeSceneCacheFile(SceneCachePlayer cachePlayer, string sceneCacheFilePath) { string prefabPath = null; GameObject go = cachePlayer.gameObject; //Check if it's possible to reuse the old assetsFolder string assetsFolder = cachePlayer.GetAssetsFolder(); if (string.IsNullOrEmpty(assetsFolder)) { MeshSyncRuntimeSettings runtimeSettings = MeshSyncRuntimeSettings.GetOrCreateSettings(); string scOutputPath = runtimeSettings.GetSceneCacheOutputPath(); assetsFolder = Path.Combine(scOutputPath, Path.GetFileNameWithoutExtension(sceneCacheFilePath)); } bool isPrefabInstance = cachePlayer.gameObject.IsPrefabInstance(); //We are directly modifying a prefab if (!isPrefabInstance && go.IsPrefab()) { prefabPath = AssetDatabase.GetAssetPath(go); CreateSceneCachePlayerAndPrefab(sceneCacheFilePath, prefabPath, assetsFolder, out SceneCachePlayer player, out GameObject newPrefab); Object.DestroyImmediate(player.gameObject); return; } if (isPrefabInstance) { GameObject prefab = PrefabUtility.GetCorrespondingObjectFromSource(cachePlayer.gameObject); prefabPath = AssetDatabase.GetAssetPath(prefab); } cachePlayer.CloseCache(); //[TODO-sin: 2020-9-28] Find out if it is possible to do undo properly Undo.RegisterFullObjectHierarchyUndo(cachePlayer.gameObject, "SceneCachePlayer"); cachePlayer.Init(assetsFolder); cachePlayer.OpenCacheInEditor(sceneCacheFilePath); //Save as prefab again if (string.IsNullOrEmpty(prefabPath)) { return; } //Prevent overwriting ".sc" file itself bool isPrefabExt = (Path.GetExtension(prefabPath).ToLower() == ".prefab"); if (isPrefabExt) { cachePlayer.gameObject.SaveAsPrefab(prefabPath); } }
//---------------------------------------------------------------------------------------------------------------------- private static bool ValidateSceneCacheOutputPath() { MeshSyncRuntimeSettings runtimeSettings = MeshSyncRuntimeSettings.GetOrCreateSettings(); string scOutputPath = runtimeSettings.GetSceneCacheOutputPath(); if (!scOutputPath.StartsWith("Assets")) { DisplaySceneCacheOutputPathErrorDialog(scOutputPath); return false; } try { Directory.CreateDirectory(scOutputPath); } catch { DisplaySceneCacheOutputPathErrorDialog(scOutputPath); return false; } return true; }
//---------------------------------------------------------------------------------------------------------------------- void RefreshSettings() { MeshSyncRuntimeSettings settings = MeshSyncRuntimeSettings.GetOrCreateSettings(); m_generatedSCResPathTextField.value = settings.GetSceneCacheOutputPath(); }