EditorController.cs 6.9 KB

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