Select Git revision
BuildRoom.cs 4.35 KiB
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
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 interactableAudioSourcePrefab;
void Update() {
// Check if all inputs have been entered and enable/disable the button
foreach (LabelledInputValidator labelledInputValidator in labelledInputValidators) {
InputValidator inputValidator = labelledInputValidator.inputValidator;
if (!inputValidator.IsValid()) {
buildButton.interactable = false;
return;
}
}
buildButton.interactable = true;
}
// allData is in the form:
// {
// "obj": {
// "path": "C:\filepath\file.obj",
// },
// "mtl": {
// "path": "C:\filepath\file.mtl",
// },
// "roomDimensions": {
// "x": 1,
// "y": 2,
// "z": 3,
// },
// "startPosition": {
// "x": 4,
// "y": 5,
// "z": 6,
// },
// "speakers": {
// "speaker1": {
// "path": C:\filepath\audio.mp3,
// "x": 7,
// "y": 8,
// "z": 9,
// },
// "speaker2": {
// "path": C:\filepath\audio.mp3,
// "x": 10,
// "y": 11,
// "z": 12,
// }
// }
// }
public void OnButtonPress() {
// Compile all the inputs into one dictionary
Dictionary<string, object> allData = new Dictionary<string, object>();
foreach (LabelledInputValidator labelledInputValidator in labelledInputValidators) {
allData[labelledInputValidator.name] = labelledInputValidator.inputValidator.GetData();
}
Debug.Log(DictionaryToString(allData));
// Load the VR prefab scene
SceneManager.LoadScene("VRPrefabScene");
// Extract speaker data from the dictionary
if (allData.ContainsKey("speakers"))
{
Dictionary<string, object> speakers = allData["speakers"] as Dictionary<string, object>;
foreach (var speakerEntry in speakers)
{
Dictionary<string, object> speakerData = speakerEntry.Value as Dictionary<string, object>;
float x = float.Parse(speakerData["x"].ToString());
float y = float.Parse(speakerData["y"].ToString());
float z = float.Parse(speakerData["z"].ToString());
string audioPath = speakerData["path"].ToString();
// Instantiate the speaker prefab
GameObject speaker = Instantiate(interactableAudioSourcePrefab, new Vector3(x, y, z), Quaternion.identity);
// Ensure the speaker stays in place
Rigidbody rb = speaker.GetComponent<Rigidbody>();
if (rb != null)
{
rb.isKinematic = true;
}
// Set the audio clip for the speaker
AudioSource audioSource = speaker.GetComponent<AudioSource>();
audioSource.clip = Resources.Load<AudioClip>(audioPath);
}
}
// TODO: Handle .obj and .mtl files
}
string DictionaryToString(Dictionary<string, object> dictionary, int depth = 0) {
string indent = new string(' ', depth * 4); // Indentation for nested levels
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
} else {
result += $"{kvp.Value}"; // Print value directly
}
result += ",\n"; // Add a comma and newline after each key-value pair
}
result += indent + "}"; // Close the dictionary
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;
public InputValidator inputValidator;
}