From 753b7e86ccc4f2fdc8f5bf3fc8aa7d89e18f6a53 Mon Sep 17 00:00:00 2001 From: mhby1g21 <mhby1g21@soton.ac.uk> Date: Tue, 14 Jan 2025 19:03:32 +0000 Subject: [PATCH] working importscenery overload --- AVVR/Assets/Scenes/MenuInterface.unity | 115 ++++++++++++------ .../_Scripts/MenuInterface/BuildRoom.cs | 86 +++++++++++-- 2 files changed, 160 insertions(+), 41 deletions(-) diff --git a/AVVR/Assets/Scenes/MenuInterface.unity b/AVVR/Assets/Scenes/MenuInterface.unity index 8a2d12a..1e61666 100644 --- a/AVVR/Assets/Scenes/MenuInterface.unity +++ b/AVVR/Assets/Scenes/MenuInterface.unity @@ -2507,6 +2507,8 @@ MonoBehaviour: - name: speakers inputValidator: {fileID: 8551979803024852529} buildButton: {fileID: 1673999060} + audioSourcePrefab: {fileID: 0} + panelToDisable: {fileID: 1572829306} --- !u!1 &615529042 GameObject: m_ObjectHideFlags: 0 @@ -4097,15 +4099,15 @@ PrefabInstance: m_Modifications: - target: {fileID: 31863182664826471, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} propertyPath: m_IsActive - value: 0 + value: 1 objectReference: {fileID: 0} - target: {fileID: 148683583016176325, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} propertyPath: m_IsActive - value: 0 + value: 1 objectReference: {fileID: 0} - target: {fileID: 512479885677563606, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} propertyPath: m_IsActive - value: 0 + value: 1 objectReference: {fileID: 0} - target: {fileID: 880674844493258928, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} propertyPath: m_IsActive @@ -4117,27 +4119,27 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 1118667690234916503, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} propertyPath: m_SizeDelta.y - value: 275 + value: 0 objectReference: {fileID: 0} - target: {fileID: 1902673287801689678, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} propertyPath: m_AnchorMax.x - value: 0.495 + value: 0 objectReference: {fileID: 0} - target: {fileID: 1902673287801689678, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} propertyPath: m_AnchorMax.y - value: 1 + value: 0 objectReference: {fileID: 0} - target: {fileID: 1902673287801689678, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} propertyPath: m_AnchorMin.x - value: 0.495 + value: 0 objectReference: {fileID: 0} - target: {fileID: 1902673288439150151, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} propertyPath: m_AnchorMax.x - value: 0.495 + value: 0 objectReference: {fileID: 0} - target: {fileID: 1902673288439150151, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} propertyPath: m_AnchorMax.y - value: 1 + value: 0 objectReference: {fileID: 0} - target: {fileID: 2243020122175513880, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} propertyPath: m_SizeDelta.x @@ -4227,9 +4229,29 @@ PrefabInstance: propertyPath: m_Texture value: objectReference: {fileID: 2800000, guid: 04cb119726b490440a3a1ef7e07e9e52, type: 3} + - target: {fileID: 2907672992081114993, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} + propertyPath: m_AnchorMax.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 2907672992081114993, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} + propertyPath: m_AnchorMin.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 2907672992081114993, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} + propertyPath: m_SizeDelta.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 2907672992081114993, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} + propertyPath: m_AnchoredPosition.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 2907672992081114993, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} + propertyPath: m_AnchoredPosition.y + value: 0 + objectReference: {fileID: 0} - target: {fileID: 4592068467763357121, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} propertyPath: m_IsActive - value: 0 + value: 1 objectReference: {fileID: 0} - target: {fileID: 5104550712541073802, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} propertyPath: m_text @@ -4237,11 +4259,11 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 5322325911060958663, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} propertyPath: m_AnchorMax.x - value: 1 + value: 0 objectReference: {fileID: 0} - target: {fileID: 5322325911060958663, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} propertyPath: m_AnchorMax.y - value: 1 + value: 0 objectReference: {fileID: 0} - target: {fileID: 5745364533364891774, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} propertyPath: m_IsActive @@ -4249,51 +4271,71 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 5855374204657878933, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} propertyPath: m_AnchorMax.y - value: 1 + value: 0 objectReference: {fileID: 0} - target: {fileID: 5855374204657878933, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} propertyPath: m_AnchorMin.y - value: 1 + value: 0 objectReference: {fileID: 0} - target: {fileID: 5855374204657878933, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} propertyPath: m_SizeDelta.x - value: 352 + value: 0 objectReference: {fileID: 0} - target: {fileID: 5855374204657878933, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} propertyPath: m_AnchoredPosition.x - value: 200 + value: 0 objectReference: {fileID: 0} - target: {fileID: 5855374204657878933, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} propertyPath: m_AnchoredPosition.y - value: -111 + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 6061969146017018215, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} + propertyPath: m_AnchorMax.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 6061969146017018215, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} + propertyPath: m_AnchorMax.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 6061969146017018215, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} + propertyPath: m_AnchorMin.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 6061969146654593390, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} + propertyPath: m_AnchorMax.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 6061969146654593390, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} + propertyPath: m_AnchorMax.y + value: 0 objectReference: {fileID: 0} - target: {fileID: 6294802142791462800, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} propertyPath: m_AnchorMax.x - value: 1 + value: 0 objectReference: {fileID: 0} - target: {fileID: 6294802142791462800, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} propertyPath: m_AnchorMax.y - value: 1 + value: 0 objectReference: {fileID: 0} - target: {fileID: 6545128166672127190, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} propertyPath: m_AnchorMax.y - value: 1 + value: 0 objectReference: {fileID: 0} - target: {fileID: 6545128166672127190, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} propertyPath: m_AnchorMin.y - value: 1 + value: 0 objectReference: {fileID: 0} - target: {fileID: 6545128166672127190, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} propertyPath: m_SizeDelta.x - value: 352 + value: 0 objectReference: {fileID: 0} - target: {fileID: 6545128166672127190, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} propertyPath: m_AnchoredPosition.x - value: 200 + value: 0 objectReference: {fileID: 0} - target: {fileID: 6545128166672127190, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} propertyPath: m_AnchoredPosition.y - value: -178 + value: 0 objectReference: {fileID: 0} - target: {fileID: 6608204077774181811, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} propertyPath: m_IsActive @@ -4301,23 +4343,23 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 7358308645308616792, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} propertyPath: m_AnchorMax.y - value: 1 + value: 0 objectReference: {fileID: 0} - target: {fileID: 7358308645308616792, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} propertyPath: m_AnchorMin.y - value: 1 + value: 0 objectReference: {fileID: 0} - target: {fileID: 7358308645308616792, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} propertyPath: m_SizeDelta.x - value: 352 + value: 0 objectReference: {fileID: 0} - target: {fileID: 7358308645308616792, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} propertyPath: m_AnchoredPosition.x - value: 200 + value: 0 objectReference: {fileID: 0} - target: {fileID: 7358308645308616792, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} propertyPath: m_AnchoredPosition.y - value: -44 + value: 0 objectReference: {fileID: 0} - target: {fileID: 7434851320941268098, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} propertyPath: m_AnchorMax.y @@ -4433,23 +4475,23 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 8144156206472818996, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} propertyPath: m_AnchorMax.y - value: 1 + value: 0 objectReference: {fileID: 0} - target: {fileID: 8144156206472818996, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} propertyPath: m_AnchorMin.y - value: 1 + value: 0 objectReference: {fileID: 0} - target: {fileID: 8144156206472818996, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} propertyPath: m_SizeDelta.x - value: 352 + value: 0 objectReference: {fileID: 0} - target: {fileID: 8144156206472818996, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} propertyPath: m_AnchoredPosition.x - value: 200 + value: 0 objectReference: {fileID: 0} - target: {fileID: 8144156206472818996, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} propertyPath: m_AnchoredPosition.y - value: -245 + value: 0 objectReference: {fileID: 0} - target: {fileID: 8298045291338814965, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} propertyPath: m_LocalPosition.x @@ -5700,6 +5742,11 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: d6236b7d0d19f8d49880cd417b94612a, type: 3} m_Name: m_EditorClassIdentifier: +--- !u!1 &1572829306 stripped +GameObject: + m_CorrespondingSourceObject: {fileID: 6608204077774181811, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} + m_PrefabInstance: {fileID: 1225901047} + m_PrefabAsset: {fileID: 0} --- !u!1 &1573797103 GameObject: m_ObjectHideFlags: 0 diff --git a/AVVR/Assets/_Scripts/MenuInterface/BuildRoom.cs b/AVVR/Assets/_Scripts/MenuInterface/BuildRoom.cs index 9022161..161ed9b 100644 --- a/AVVR/Assets/_Scripts/MenuInterface/BuildRoom.cs +++ b/AVVR/Assets/_Scripts/MenuInterface/BuildRoom.cs @@ -1,11 +1,22 @@ using System; using System.Collections.Generic; +using System.IO; using UnityEngine; using UnityEngine.UI; public class BuildRoom : MonoBehaviour { public LabelledInputValidator[] labelledInputValidators; // All the input validators and their respective labels public Button buildButton; // The submit/build room button + public GameObject audioSourcePrefab; // Reference to the audio source prefab + private ImportScenery importScenery; // Reference to ImportScenery component + + void Start() { + // Get or add ImportScenery component + importScenery = gameObject.GetComponent<ImportScenery>(); + if (importScenery == null) { + importScenery = gameObject.AddComponent<ImportScenery>(); + } + } void Update() { // Check if all inputs have been entered and enable/disable the button @@ -61,31 +72,92 @@ public class BuildRoom : MonoBehaviour { } Debug.Log(DictionaryToString(allData)); + + // Extract data from dictionary + Dictionary<string, object> objData = allData["obj"] as Dictionary<string, object>; + Dictionary<string, object> mtlData = allData["mtl"] as Dictionary<string, object>; + Dictionary<string, object> roomDimensions = allData["roomDimensions"] as Dictionary<string, object>; + Dictionary<string, object> startPosition = allData["startPosition"] as Dictionary<string, object>; + Dictionary<string, object> speakers = allData["speakers"] as Dictionary<string, object>; + + if (objData == null || mtlData == null || roomDimensions == null || startPosition == null || speakers == null) { + Debug.LogError("Invalid data structure"); + return; + } + + // Import the scene using ImportScenery + string objPath = objData["path"].ToString(); + string mtlPath = mtlData["path"].ToString(); + ImportScenery.ImportScene(ImportScenery.GetNewMesh(objPath, mtlPath)); + + // Set player start position + GameObject xrOrigin = GameObject.Find("Complete XR Origin Set Up Variant AVVR"); + if (xrOrigin != null) { + Vector3 position = new Vector3( + Convert.ToSingle(startPosition["x"]), + Convert.ToSingle(startPosition["y"]), + Convert.ToSingle(startPosition["z"]) + ); + xrOrigin.transform.position = position; + } + + // Create audio sources + foreach (KeyValuePair<string, object> speaker in speakers) { + Dictionary<string, object> speakerData = speaker.Value as Dictionary<string, object>; + if (speakerData != null) { + CreateAudioSource( + speakerData["path"].ToString(), + Convert.ToSingle(speakerData["x"]), + Convert.ToSingle(speakerData["y"]), + Convert.ToSingle(speakerData["z"]) + ); + } + } + } + + private void CreateAudioSource(string audioPath, float x, float y, float z) { + if (audioSourcePrefab == null) { + Debug.LogError("Audio source prefab not assigned!"); + return; + } + + // Create audio source from prefab + GameObject audioSourceObj = Instantiate(audioSourcePrefab, new Vector3(x, y, z), Quaternion.identity); + audioSourceObj.name = "AudioSource_" + Path.GetFileNameWithoutExtension(audioPath); + + // Load and assign audio clip + AudioClip clip = Resources.Load<AudioClip>(Path.GetFileNameWithoutExtension(audioPath)); + if (clip != null) { + AudioSource audioSource = audioSourceObj.GetComponent<AudioSource>(); + if (audioSource != null) { + audioSource.clip = clip; + } + } else { + Debug.LogError($"Failed to load audio clip: {audioPath}"); + } } string DictionaryToString(Dictionary<string, object> dictionary, int depth = 0) { - string indent = new string(' ', depth * 4); // Indentation for nested levels + string indent = new string(' ', depth * 4); string result = "{\n"; foreach (var kvp in dictionary) { result += indent + $" \"{kvp.Key}\": "; if (kvp.Value is Dictionary<string, object> nestedDict) { - result += DictionaryToString(nestedDict, depth + 1); // Recursive call for nested dictionary + result += DictionaryToString(nestedDict, depth + 1); } else { - result += $"{kvp.Value}"; // Print value directly + result += $"{kvp.Value}"; } - result += ",\n"; // Add a comma and newline after each key-value pair + result += ",\n"; } - result += indent + "}"; // Close the dictionary + result += indent + "}"; return result; } } -// A class to label the data from an input validator -// Also allows for viewing in the inspector [Serializable] public class LabelledInputValidator { public string name; -- GitLab