| 1 | /************************************************************************************ |
| 2 | Filename : ONSPPropagationSerializationManager.cs |
| 3 | Content : Functionality for serializing Oculus Audio geometry |
| 4 | Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved. |
| 5 | |
| 6 | Licensed under the Oculus SDK Version 3.5 (the "License"); |
| 7 | you may not use the Oculus SDK except in compliance with the License, |
| 8 | which is provided at the time of installation or download, or which |
| 9 | otherwise accompanies this software in either electronic or hard copy form. |
| 10 | |
| 11 | You may obtain a copy of the License at |
| 12 | |
| 13 | https://developer.oculus.com/licenses/sdk-3.5/ |
| 14 | |
| 15 | Unless required by applicable law or agreed to in writing, the Oculus SDK |
| 16 | distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | See the License for the specific language governing permissions and |
| 19 | limitations under the License. |
| 20 | ************************************************************************************/ |
| 21 | using UnityEditor; |
| 22 | using UnityEditor.Build; |
| 23 | using UnityEngine; |
| 24 | using UnityEditor.SceneManagement; |
| 25 | using UnityEngine.SceneManagement; |
| 26 | using System.Collections.Generic; |
| 27 | |
| 28 | public enum PlayModeState |
| 29 | { |
| 30 | Stopped, |
| 31 | Playing, |
| 32 | Paused |
| 33 | } |
| 34 | |
| 35 | class ONSPPropagationSerializationManager |
| 36 | { |
| 37 | private static PlayModeState _currentState = PlayModeState.Stopped; |
| 38 | |
| 39 | static ONSPPropagationSerializationManager() |
| 40 | { |
| 41 | EditorSceneManager.sceneSaving += OnSceneSaving; |
| 42 | } |
| 43 | public int callbackOrder { get { return 0; } } |
| 44 | public void OnPreprocessBuild(BuildTarget target, string path) |
| 45 | { |
| 46 | Debug.Log("ONSPPropagationSerializationManager.OnPreprocessBuild for target " + target + " at path " + path); |
| 47 | } |
| 48 | |
| 49 | [MenuItem("Oculus/Spatializer/Build audio geometry for current scene")] |
| 50 | public static void BuildAudioGeometryForCurrentScene() |
| 51 | { |
| 52 | BuildAudioGeometryForScene(EditorSceneManager.GetActiveScene()); |
| 53 | } |
| 54 | |
| 55 | [MenuItem("Oculus/Spatializer/Rebuild audio geometry all scenes")] |
| 56 | public static void RebuildAudioGeometryForAllScenes() |
| 57 | { |
| 58 | Debug.Log("Rebuilding geometry for all scenes"); |
| 59 | |
| 60 | System.IO.Directory.Delete(ONSPPropagationGeometry.GeometryAssetPath, true); |
| 61 | |
| 62 | for (int i = 0; i < EditorSceneManager.sceneCount; ++i) |
| 63 | { |
| 64 | BuildAudioGeometryForScene(EditorSceneManager.GetSceneAt(i)); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | public static void OnSceneSaving(Scene scene, string path) |
| 69 | { |
| 70 | BuildAudioGeometryForScene(scene); |
| 71 | } |
| 72 | |
| 73 | private static void BuildAudioGeometryForScene(Scene scene) |
| 74 | { |
| 75 | Debug.Log("Building audio geometry for scene " + scene.name); |
| 76 | |
| 77 | List<GameObject> rootObjects = new List<GameObject>(); |
| 78 | scene.GetRootGameObjects(rootObjects); |
| 79 | |
| 80 | HashSet<string> fileNames = new HashSet<string>(); |
| 81 | foreach (GameObject go in rootObjects) |
| 82 | { |
| 83 | var geometryComponents = go.GetComponentsInChildren<ONSPPropagationGeometry>(); |
| 84 | foreach (ONSPPropagationGeometry geo in geometryComponents) |
| 85 | { |
| 86 | if (geo.fileEnabled) |
| 87 | { |
| 88 | if (!geo.WriteFile()) |
| 89 | { |
| 90 | Debug.LogError("Failed writing geometry for " + geo.gameObject.name); |
| 91 | } |
| 92 | else |
| 93 | { |
| 94 | if (!fileNames.Add(geo.filePathRelative)) |
| 95 | { |
| 96 | Debug.LogWarning("Duplicate file name detected: " + geo.filePathRelative); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | Debug.Log("Successfully built " + fileNames.Count + " geometry objects"); |
| 104 | } |
| 105 | } |