BeaconController.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. }
  56. // Перемещение
  57. void OnMouseDrag()
  58. {
  59. if (mode == Mode.Editor)
  60. {
  61. curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
  62. curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
  63. var x =(float) Math.Round(curPosition.x, Rounding);
  64. var z = (float)Math.Round(curPosition.z, Rounding);
  65. transform.position = new Vector3(x, Height, z);
  66. text.text = $"id={id} x={x} y={z}";
  67. canvas.transform.GetChild(0).transform.position = new Vector3(Input.mousePosition.x, Input.mousePosition.y+20, screenPoint.z);
  68. }
  69. }
  70. private void OnMouseUp()
  71. {
  72. if (mode == Mode.Editor)
  73. {
  74. info = false;
  75. text.text = text_info;
  76. text.gameObject.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 120);
  77. text.gameObject.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 75);
  78. transform.GetComponent<LabelObjectScript>().enabled = true;
  79. }
  80. }
  81. }