EditorController.cs 7.2 KB

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