BeaconController.cs 3.0 KB

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