LabelObjectScript.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. /// <summary>
  6. /// текст над маркером
  7. /// </summary>
  8. public class LabelObjectScript : MonoBehaviour
  9. {
  10. RectTransform canvasRect;
  11. GameObject text;
  12. Button button;
  13. public bool TrackMarker = true;
  14. public uint UserId;
  15. //public string NameUser;
  16. // Start is called before the first frame update
  17. void Start()
  18. {
  19. //text = Instantiate(Resources.Load("GameObjects/Capsule", typeof(GameObject))) as GameObject;
  20. cameraController = Camera.main.GetComponent<CameraController>();
  21. canvasRect = transform.GetChild(0).GetComponent<RectTransform>();
  22. text = transform.GetChild(0).GetChild(0).GetChild(0).gameObject;
  23. var player = PlayerController.instance;
  24. if (TrackMarker) {
  25. button = transform.GetChild(0).GetChild(0).GetChild(1).GetComponent<Button>();
  26. button.onClick.AddListener(() => player.BroadcastStart(UserId));
  27. }
  28. }
  29. // Update is called once per frame
  30. void Update()
  31. {
  32. // Offset position above object bbox (in world space)
  33. float offsetPosY = transform.position.y + 1.5f;
  34. // Final position of marker above GO in world space
  35. Vector3 offsetPos = new Vector3(transform.position.x, offsetPosY, transform.position.z);
  36. // Calculate *screen* position (note, not a canvas/recttransform position)
  37. Vector2 canvasPos;
  38. Vector2 screenPoint = Camera.main.WorldToScreenPoint(offsetPos);
  39. // Convert screen position to Canvas / RectTransform space <- leave camera null if Screen Space Overlay
  40. RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRect, screenPoint, null, out canvasPos);
  41. // Set
  42. //markerRtra.localPosition = canvasPos;
  43. //if(TrackMarker) text.transform.localPosition = canvasPos;
  44. //else
  45. text.transform.parent.transform.localPosition = canvasPos;
  46. }
  47. CameraController cameraController;
  48. void OnMouseDown()
  49. {
  50. if (TrackMarker)
  51. {
  52. cameraController.target = transform;
  53. cameraController.axes = CameraController.RotationAxes.TrackMarker;
  54. }
  55. }
  56. void BroadcastStart()
  57. {
  58. var player = PlayerController.instance;
  59. if (player.user_broadcast != UserId)
  60. {
  61. if (player.user_broadcast != null)
  62. {
  63. var client = Client.instance;
  64. client.ImageStreamStopSend(player.user_broadcast.Value);
  65. }
  66. player.user_broadcast = UserId;
  67. player.broadcast = true;
  68. }
  69. }
  70. }