MenuMaker.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* MenuMaker.cs
  2. * author: Trung-Hoa Ha & Lydia Ebbinghaus.
  3. */
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. // Class that creates a menu, containing buttons with camera images and corresponding name of camera.
  8. // Attached to Content (LeftHand > Camera Scroll Menu > Scroll View > Viewport > Content.
  9. public class MenuMaker : MonoBehaviour
  10. {
  11. public GameObject buttonPrefab;
  12. public GameObject ParentOfCamera;
  13. public Transform buttonContainer;
  14. public Transform firstButtonPosition;
  15. public MeshRenderer meshRenderer;
  16. private CameraCreator cameraCreator ;
  17. private Texture2D texture2D;
  18. private bool retrievedCameras;
  19. private GameObject button;
  20. private Dictionary<string,GameObject> buttons;
  21. void Start()
  22. {
  23. if(buttonPrefab == null || ParentOfCamera == null || buttonContainer == null || firstButtonPosition == null || meshRenderer == null){
  24. Debug.Log("MenuMaker: Public properties of MenuMaker script are not set. Please set them or menu won't work properly");
  25. }
  26. buttons = new Dictionary<string, GameObject>();
  27. cameraCreator = GameObject.Find("VRCamera").GetComponent<CameraCreator>();
  28. }
  29. // If camera images are received. Loads example images (current image) on a corresponding button. Renames button and creates a description of camera.
  30. void Update()
  31. {
  32. if ((retrievedCameras != true) && (cameraCreator.topicsSorted))
  33. {
  34. int cameranumber = ParentOfCamera.transform.childCount;
  35. for (int i = 0; i < cameranumber; i++)
  36. {
  37. // Creates button with camera image and name for a specific camera.
  38. button = Instantiate(buttonPrefab, firstButtonPosition.position + new Vector3(0,-i,0), Quaternion.identity, buttonContainer);
  39. string cameraName = ParentOfCamera.transform.GetChild(i).name;
  40. button.name = cameraName +"Button";
  41. button.transform.GetChild(0).GetComponentInChildren<TMPro.TextMeshProUGUI>().text = cameraName.Split('/')[1] + " " + cameraName.Split('/')[2];
  42. button.transform.GetChild(0).GetComponentInChildren<TMPro.TextMeshProUGUI>().name = cameraName;
  43. loadTexture(cameraName);
  44. // Image does not fit properly in canvas. Wrong orientation.
  45. // Place and orientate button correctly in menu.
  46. button.transform.rotation = button.transform.parent.rotation;
  47. button.transform.position = new Vector3(this.transform.position.x,this.transform.position.y,0);
  48. button.transform.localPosition = new Vector3(this.transform.localPosition.x,this.transform.localPosition.y,0);
  49. buttons.Add(button.name,button);
  50. Debug.Log(cameraName + " added to menu.");
  51. }
  52. Debug.Log("Menu: Images loaded to menu.");
  53. if (cameranumber > 0 ){
  54. Debug.Log("cameras should be set");
  55. retrievedCameras = true;
  56. }
  57. }
  58. }
  59. // Loads imaga data that is associated on a button for a camera with name :cameraName.
  60. public void loadTexture(string cameraName)
  61. {
  62. if(retrievedCameras){
  63. string name = cameraName+"Button";
  64. button = buttons[name];
  65. }
  66. texture2D = new Texture2D(1, 1);
  67. bool isLoaded = texture2D.LoadImage(cameraCreator.getImageData(cameraName));
  68. if (isLoaded)
  69. {
  70. button.transform.GetChild(2).GetComponent<RawImage>().texture = texture2D;
  71. Debug.Log("Texture set" + button);
  72. }
  73. }
  74. }