ActivateCameraScript.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* ActivateCameraScript.cs
  2. * author: Lydia Ebbinghaus, Jana-Sophie Schönfeld, Trung-Hoa Ha
  3. */
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using RosSharp.RosBridgeClient;
  7. using UnityEngine.UI;
  8. // Script that activates/deletes and shows/hides a camera when selected with a laser pointer.
  9. // Attached to child of Button (Camera Scroll Menu > Scrollview > Viewport > Content).
  10. public class ActivateCameraScript : MonoBehaviour
  11. {
  12. Dictionary<string,ImageSubscriberMod> imageSubscribers;
  13. GameObject cameraPlane;
  14. Color oldColor;
  15. // Creating dictonary to store and access current image subscribers.
  16. public void Start()
  17. {
  18. imageSubscribers = new Dictionary<string, ImageSubscriberMod>();
  19. }
  20. // Reacts on pushed camera button in menu.
  21. // If button was already selected: deactivate and hide camera.
  22. // If button was not selected: activate and show camera.
  23. public void ActivateDeactivateCamera()
  24. {
  25. // Getting name of selected camera.
  26. string cameraName = transform.parent.GetChild(0).GetComponentInChildren<TMPro.TextMeshProUGUI>().name;
  27. if(cameraName == null){
  28. Debug.Log("ActivateCameraScript.cs: No name was found for the camera. Check if script is attached to the button as a child.");
  29. }
  30. GameObject parentCamera = GameObject.Find("Automatic Cameras");
  31. if(parentCamera == null){
  32. Debug.Log("ActivateCameraScript.cs: Automatic Cameras object was not found. Check if Automatic Cameras has the correct name.");
  33. }
  34. Transform[] trs = parentCamera.GetComponentsInChildren<Transform>(true);
  35. // Searching in automatic camers for selected camera.
  36. foreach(Transform t in trs){
  37. if(t.name == cameraName){
  38. Debug.Log("ActivateCameraScript.cs: found camera" + t.name + cameraName);
  39. cameraPlane = t.gameObject;
  40. }
  41. }
  42. if (cameraPlane != null){
  43. if (cameraPlane.activeSelf == true){
  44. DeactivateCamera();
  45. }
  46. else
  47. {
  48. ActivateCamera();
  49. }
  50. }
  51. else
  52. {
  53. Debug.LogError("ActivateCameraScript.cs: Camera " + cameraName + " not found. Can't activate/deactivate. Check if name of camera object is the same as text of button.");
  54. }
  55. }
  56. // Activates camera and shows at defined position.
  57. private void ActivateCamera()
  58. {
  59. GameObject cam = GameObject.Find("VRCamera");
  60. cameraPlane.SetActive(true);
  61. // Choosing location where camera should appear.
  62. cameraPlane.transform.rotation = cam.transform.rotation;
  63. cameraPlane.transform.Rotate(new Vector3(0,180,0));
  64. // Use this instead to use a plane.
  65. //cameraPlane.transform.Rotate(new Vector3(90,180,0));
  66. cameraPlane.transform.position = cam.transform.position + cam.transform.forward * 0.5f;
  67. // Adds specific image subscriber for camera.
  68. var currentImageSubscriber = GameObject.Find("RosBridge").AddComponent<ImageSubscriberMod>();
  69. currentImageSubscriber.meshRenderer = cameraPlane.GetComponent<MeshRenderer>();
  70. currentImageSubscriber.Topic = cameraPlane.name;
  71. // Higlights selected camera in menu.
  72. this.transform.parent.GetChild(2).GetComponent<RawImage>().color=Color.green;
  73. imageSubscribers.Add(cameraPlane.name,currentImageSubscriber);
  74. }
  75. // Deactivates camera.
  76. private void DeactivateCamera()
  77. {
  78. // Hides camera and deletes higlight in menu.
  79. cameraPlane.SetActive(false);
  80. this.transform.parent.GetChild(2).GetComponent<RawImage>().color=Color.white;
  81. // Destroys image subscriber.
  82. // Otherwise huge lags, which cause small fps. Contributes to VR-sickness.
  83. Destroy(imageSubscribers[cameraPlane.name]);
  84. imageSubscribers.Remove(cameraPlane.name);
  85. }
  86. }