BeaconController.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. /// <summary>
  7. /// Размещение маяков
  8. /// Используется в редакторе и в плеере
  9. /// </summary>
  10. public class BeaconController : MonoBehaviour
  11. {
  12. public float Height = 0.5f;
  13. public int Rounding = 1;
  14. //public bool move = false;
  15. public bool info = false; // вывод таблицы с информацией
  16. public enum Mode { Player, Editor};
  17. public Mode mode = Mode.Player;
  18. public string text_info;
  19. public string id;
  20. Text text;
  21. GameObject canvas;
  22. private Vector3 screenPoint;
  23. private Vector3 offset;
  24. private Vector3 curScreenPoint;
  25. private Vector3 curPosition;
  26. // Start is called before the first frame update
  27. void Start()
  28. {
  29. text = transform.GetChild(0).GetChild(0).GetChild(0).GetComponent<Text>();
  30. text.text = text_info;
  31. canvas = transform.GetChild(0).gameObject;
  32. canvas.SetActive(info);
  33. }
  34. // Update is called once per frame
  35. void Update()
  36. {
  37. if (info != transform.GetChild(0).gameObject.activeSelf)
  38. {
  39. canvas.SetActive(info);
  40. }
  41. }
  42. void OnMouseDown()
  43. {
  44. if(mode == Mode.Player) info = !info;
  45. //Debug.Log(StagesEditorController.indexCursor);
  46. if (mode == Mode.Editor)
  47. {
  48. screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
  49. offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
  50. info = true;
  51. text.gameObject.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 100);
  52. text.gameObject.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 15);
  53. transform.GetComponent<LabelObjectScript>().enabled = false;
  54. }
  55. GetComponent<Outline>().enabled = info;
  56. }
  57. // Перемещение
  58. void OnMouseDrag()
  59. {
  60. if (mode == Mode.Editor)
  61. {
  62. curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
  63. curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
  64. var x =(float) Math.Round(curPosition.x, Rounding);
  65. var z = (float)Math.Round(curPosition.z, Rounding);
  66. transform.position = new Vector3(x, Height, z);
  67. text.text = $"id={id} x={x} y={z}";
  68. canvas.transform.GetChild(0).transform.position = new Vector3(Input.mousePosition.x, Input.mousePosition.y+20, screenPoint.z);
  69. }
  70. }
  71. private void OnMouseUp()
  72. {
  73. if (mode == Mode.Editor)
  74. {
  75. info = false;
  76. text.text = text_info;
  77. text.gameObject.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 120);
  78. text.gameObject.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 75);
  79. transform.GetComponent<LabelObjectScript>().enabled = true;
  80. }
  81. }
  82. /// <summary>
  83. /// Информационная панель над маяком
  84. /// </summary>
  85. /// <param name="b"></param>
  86. public void InformationPanel(Beacon b)
  87. {
  88. text_info = $"{b.id}\n{b.vagrant_label}\nuuid={b.uuid}\nmajor={b.major}\nminor={b.minor}\nmac={b.mac}";
  89. }
  90. public void BeaconEnabled(Beacon b, bool enabled)
  91. {
  92. b.enabled = enabled;
  93. if (b.enabled == false) GetComponent<Renderer>().material.color = Color.grey;
  94. else GetComponent<Renderer>().material.color = Color.red;
  95. }
  96. }