LabelObjectScript.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. // Start is called before the first frame update
  16. void Start()
  17. {
  18. //text = Instantiate(Resources.Load("GameObjects/Capsule", typeof(GameObject))) as GameObject;
  19. cameraController = Camera.main.GetComponent<CameraController>();
  20. canvasRect = transform.GetChild(0).GetComponent<RectTransform>();
  21. text = transform.GetChild(0).GetChild(0).GetChild(0).gameObject;
  22. var player = PlayerController.instance;
  23. if (TrackMarker) {
  24. button = transform.GetChild(0).GetChild(0).GetChild(1).GetComponent<Button>();
  25. button.onClick.AddListener(() => player.BroadcastStart(UserId));
  26. }
  27. }
  28. // Update is called once per frame
  29. void Update()
  30. {
  31. // Offset position above object bbox (in world space)
  32. float offsetPosY = transform.position.y + 1.5f;
  33. // Final position of marker above GO in world space
  34. Vector3 offsetPos = new Vector3(transform.position.x, offsetPosY, transform.position.z);
  35. // Calculate *screen* position (note, not a canvas/recttransform position)
  36. Vector2 canvasPos;
  37. Vector2 screenPoint = Camera.main.WorldToScreenPoint(offsetPos);
  38. // Convert screen position to Canvas / RectTransform space <- leave camera null if Screen Space Overlay
  39. RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRect, screenPoint, null, out canvasPos);
  40. // Set
  41. //markerRtra.localPosition = canvasPos;
  42. //if(TrackMarker) text.transform.localPosition = canvasPos;
  43. //else
  44. text.transform.parent.transform.localPosition = canvasPos;
  45. }
  46. CameraController cameraController;
  47. void OnMouseDown()
  48. {
  49. if (TrackMarker)
  50. {
  51. cameraController.target = transform;
  52. cameraController.axes = CameraController.RotationAxes.TrackMarker;
  53. }
  54. }
  55. void BroadcastStart()
  56. {
  57. var player = PlayerController.instance;
  58. if (player.user_broadcast != UserId)
  59. {
  60. if (player.user_broadcast != null)
  61. {
  62. var client = Client.instance;
  63. client.ImageStreamStopSend(player.user_broadcast.Value);
  64. }
  65. player.user_broadcast = UserId;
  66. player.broadcast = true;
  67. }
  68. }
  69. }