EditorController.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using UnityEngine;
  6. using UnityEngine.SceneManagement;
  7. using UnityEngine.UI;
  8. /// <summary>
  9. /// Редактор
  10. /// </summary>
  11. public class EditorController : MonoBehaviour
  12. {
  13. public InputField Name;
  14. public InputField Image;
  15. public InputField SizeX;
  16. public InputField SizeY;
  17. public GameObject plane;
  18. public GameObject BeaconsContent;
  19. public GameObject PanelBeaconEdit;
  20. public Toggle ToggleGrid;
  21. public Toggle ToggleScalePanel;
  22. public Toggle projection;
  23. public GameObject Grid;
  24. public Dropdown DropdownLocation;
  25. public Transform Locations;
  26. public Button ButtonAddBeacon;
  27. public List<Beacon> Beacons = new List<Beacon>();
  28. PlayerController player;
  29. ModeController mode;
  30. ToolsController tools;
  31. Client client;
  32. // Start is called before the first frame update
  33. void Start()
  34. {
  35. client = Client.instance;
  36. player = GameObject.Find("Canvas").transform.GetChild(2).GetComponent<PlayerController>();
  37. mode = GameObject.Find("ButtonMode").GetComponent<ModeController>();
  38. tools = GameObject.Find("EditorTools").GetComponent<ToolsController>();
  39. ButtonAddBeacon.onClick.AddListener(() => {
  40. AddBeacon();
  41. });
  42. }
  43. // Update is called once per frame
  44. void Update()
  45. {
  46. SizeX.text = $"{plane.transform.localScale.x}";
  47. SizeY.text = $"{plane.transform.localScale.z}";
  48. if (ToggleGrid.isOn != Grid.activeSelf) Grid.SetActive(ToggleGrid.isOn);
  49. if (ToggleScalePanel.isOn != plane.GetComponent<TouchScript>().activeScript) plane.GetComponent<TouchScript>().activeScript = ToggleScalePanel.isOn;
  50. if (projection.isOn)
  51. {
  52. Camera.main.orthographic = true;
  53. }
  54. else
  55. {
  56. Camera.main.orthographic = false;
  57. }
  58. PlayerController.LoadMaps(client, DropdownLocation, Locations.gameObject);
  59. }
  60. public void UpdatePanel()
  61. {
  62. if (!Image.text.Equals(""))
  63. StartCoroutine(SendingFormController.LoadImage(Image.text, plane));
  64. var x = 1f;
  65. if (!SizeX.text.Equals(""))
  66. x = float.Parse(SizeX.text, CultureInfo.InvariantCulture);
  67. var y = 1f;
  68. if (!SizeY.text.Equals(""))
  69. y = float.Parse(SizeY.text, CultureInfo.InvariantCulture);
  70. plane.transform.localScale = new Vector3(x, 1, y);
  71. var scale = plane.transform.localScale;
  72. plane.transform.position = new Vector3(scale.x * 5, 0, scale.z * 5);
  73. }
  74. public void Save()
  75. {
  76. //SceneManager.LoadScene("Location");
  77. tools.CursorOff();
  78. mode.Switch();
  79. var l = new Location { id = PlayerController.locations.Count, name = Name.text };
  80. PlayerController.locations.Add(l);
  81. player.DropdownLocation.options.Add(new Dropdown.OptionData(l.name));
  82. var location = new GameObject();
  83. location.name = l.name;
  84. location.transform.SetParent(Locations);
  85. foreach (var w in WallCreate.Walls)
  86. w.transform.SetParent(location.transform);
  87. }
  88. public void Back()
  89. {
  90. //SceneManager.LoadScene("Location");
  91. mode.Switch();
  92. }
  93. public void AddBeacon(Beacon b = null)
  94. {
  95. //PanelBeaconEdit.SetActive(true);
  96. var beacon = Instantiate(Resources.Load("GameObjects/Beacon", typeof(GameObject))) as GameObject;
  97. if(ModeController.editor) beacon.GetComponent<BeaconController>().mode = BeaconController.Mode.Editor;
  98. beacon.name = $"Beacon_{b?.id.ToString() ?? Beacons.Count.ToString()}";
  99. if (b != null)
  100. {
  101. beacon.transform.position = new Vector3(b.x / 100, 0.5f, b.y / 100);
  102. //beacon.transform.GetChild(0).GetChild(0).GetChild(0).GetComponent<Text>().text
  103. beacon.GetComponent<BeaconController>().text_info= $"{b.id}\nuuid={b.uuid}\nmajor={b.major}\nminor={b.minor}";
  104. beacon.GetComponent<BeaconController>().id = $"{b.id}";
  105. }
  106. beacon.transform.SetParent(GameObject.Find("Beacons").transform);
  107. var name_beacon = $"Маяк {b?.id.ToString() ?? Beacons.Count.ToString()}";
  108. PanelBeaconEdit.transform.GetChild(0).GetComponent<Text>().text = name_beacon;
  109. var panel_button = Instantiate(Resources.Load("GameObjects/Panel_Beacon", typeof(GameObject))) as GameObject;
  110. panel_button.transform.SetParent(BeaconsContent.transform);
  111. panel_button.name = $"Beacon_{b?.id.ToString() ?? Beacons.Count.ToString()}";
  112. var button = panel_button.transform.GetChild(0).GetComponent<Button>();
  113. button.name = $"BeaconButton_{Beacons.Count}";
  114. button.transform.GetChild(0).GetComponent<Text>().text = name_beacon;
  115. //var b = new Beacon { };
  116. button.onClick.AddListener(() =>
  117. {
  118. PanelBeaconEdit.SetActive(true);
  119. PanelBeaconEdit.transform.GetChild(0).GetComponent<Text>().text = name_beacon;
  120. if (b != null)
  121. {
  122. PanelBeaconEdit.transform.GetChild(1).GetComponent<InputField>().text = b.uuid;
  123. PanelBeaconEdit.transform.GetChild(2).GetComponent<InputField>().text = $"{b.major}";
  124. PanelBeaconEdit.transform.GetChild(3).GetComponent<InputField>().text = $"{b.major}";
  125. }
  126. });
  127. //var text = PanelBeaconEdit.transform.GetChild(0).GetComponent<Text>().text = "";
  128. //var uuid = PanelBeaconEdit.transform.GetChild(1).GetComponent<InputField>().text = "";
  129. if (b == null) b = new Beacon();
  130. var button_ok = PanelBeaconEdit.transform.GetChild(5).transform.GetChild(0).GetComponent<Button>();
  131. button_ok.onClick.AddListener(() =>
  132. {
  133. b.uuid = PanelBeaconEdit.transform.GetChild(1).GetComponent<InputField>().text;
  134. b.minor = uint.Parse(PanelBeaconEdit.transform.GetChild(2).GetComponent<InputField>().text);
  135. b.major = uint.Parse(PanelBeaconEdit.transform.GetChild(3).GetComponent<InputField>().text);
  136. PanelBeaconEdit.SetActive(false);
  137. });
  138. var button_close = PanelBeaconEdit.transform.GetChild(5).transform.GetChild(1).GetComponent<Button>();
  139. button_close.onClick.AddListener(() =>
  140. {
  141. PanelBeaconEdit.SetActive(false);
  142. });
  143. var button_del = panel_button.transform.GetChild(1).GetComponent<Button>();
  144. button_del.onClick.AddListener(() =>
  145. {
  146. Destroy(beacon);
  147. Destroy(panel_button);
  148. Beacons.Remove(b);
  149. if (PanelBeaconEdit.activeSelf) PanelBeaconEdit.SetActive(false);
  150. });
  151. //b.id = (uint)Beacons.Count;
  152. b.beacon = beacon;
  153. b.panel_button = panel_button;
  154. Beacons.Add(b);
  155. }
  156. }