DebugConsole.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.EventSystems;
  6. namespace SerialPortUtility
  7. {
  8. public class DebugConsole : MonoBehaviour
  9. {
  10. private static int instanceNum = 0;
  11. public SerialPortUtilityPro SpupObject = null;
  12. private Button ViewButton;
  13. private Text ViewButtonText;
  14. private InputField SerialPortInputField;
  15. private GameObject Panel;
  16. private Text InfomationText;
  17. private Text DataView;
  18. private ContentSizeFitter DataViewSizeFitter;
  19. private Dropdown DataSendMode;
  20. private bool PanelViewed;
  21. private const int UPDATECOUNT = 6;
  22. private int UpdateUICount = 0;
  23. // Use this for initialization
  24. void Start()
  25. {
  26. ViewButton = this.transform.Find("ViewButton").GetComponent<Button>();
  27. ViewButtonText = this.transform.Find("ViewButton/Text").GetComponent<Text>();
  28. SerialPortInputField = this.transform.Find("Panel/InputField").GetComponent<InputField>();
  29. Panel = this.transform.Find("Panel").gameObject;
  30. InfomationText = this.transform.Find("Panel/InfomationText").GetComponent<Text>();
  31. DataView = this.transform.Find("Panel/ScrollPanel/ScrollView/Viewport/Content").GetComponent<Text>();
  32. DataViewSizeFitter = this.transform.Find("Panel/ScrollPanel/ScrollView/Viewport/Content").GetComponent<ContentSizeFitter>();
  33. DataSendMode = this.transform.Find("Panel/Dropdown").GetComponent<Dropdown>();
  34. if (SpupObject == null) {
  35. if(this.transform.parent != null)
  36. SpupObject = this.transform.parent.GetComponent<SerialPortUtilityPro>();
  37. }
  38. Panel.SetActive(false);
  39. PanelViewed = false;
  40. if (SpupObject == null)
  41. {
  42. ViewButton.interactable = false;
  43. ViewButtonText.text = "null";
  44. InfomationText.text = "null";
  45. }
  46. else
  47. {
  48. ViewButtonText.text = SpupObject.gameObject.name;
  49. string systemName = SpupObject.OpenMethod.ToString();
  50. InfomationText.text = SpupObject.gameObject.name + " : " + systemName;
  51. }
  52. RectTransform rtm = this.transform.Find("ViewButton").GetComponent<RectTransform>();
  53. rtm.anchoredPosition = new Vector3(20.0f + (170.0f*(float)instanceNum), -20.0f, 0.0f);
  54. ++instanceNum;
  55. //find EventSystem
  56. EventSystem eventSystem = GameObject.FindObjectOfType<EventSystem>();
  57. if (eventSystem == null)
  58. {
  59. GameObject obj = Resources.Load<GameObject>("Prefabs/SPUPEventSystem");
  60. GameObject objeve = Instantiate<GameObject>(obj);
  61. objeve.name = "EventSystem";
  62. }
  63. }
  64. // Update is called once per frame
  65. void Update()
  66. {
  67. if (SpupObject == null)
  68. return;
  69. if (!SpupObject.IsOpened()) {
  70. Panel.SetActive(false);
  71. PanelViewed = false;
  72. ViewButtonText.color = new Color(255, 0, 0);
  73. } else
  74. ViewButtonText.color = new Color(255, 255, 255);
  75. if (PanelViewed)
  76. {
  77. --UpdateUICount;
  78. if (UpdateUICount < 0)
  79. {
  80. DataView.text = SpupObject.GetSerialDebugString;
  81. if(true)
  82. DataViewSizeFitter.SetLayoutVertical();
  83. UpdateUICount = UPDATECOUNT;
  84. }
  85. }
  86. }
  87. public void SendButtonClick()
  88. {
  89. if (SpupObject == null)
  90. return;
  91. if (PanelViewed)
  92. {
  93. if (SerialPortInputField.text != string.Empty)
  94. {
  95. //Write
  96. switch(DataSendMode.value) {
  97. //NONE
  98. default:
  99. case 0: if (!SpupObject.Write(SerialPortInputField.text))
  100. SpupObject.SerialDebugAddString(" [Send Error!]", true);
  101. break;
  102. //LF
  103. case 1: if (!SpupObject.WriteLF(SerialPortInputField.text))
  104. SpupObject.SerialDebugAddString(" [Send Error!]", true);
  105. break;
  106. //CR
  107. case 2: if (!SpupObject.WriteCR(SerialPortInputField.text))
  108. SpupObject.SerialDebugAddString(" [Send Error!]", true);
  109. break;
  110. //CRLF
  111. case 3: if (!SpupObject.WriteCRLF(SerialPortInputField.text))
  112. SpupObject.SerialDebugAddString(" [Send Error!]", true);
  113. break;
  114. //Binary
  115. case 4:
  116. if (!SpupObject.Write(HexStringToData(SerialPortInputField.text)))
  117. SpupObject.SerialDebugAddString(" [Send Error!]", true);
  118. break;
  119. }
  120. }
  121. }
  122. }
  123. private byte[] HexStringToData(string text)
  124. {
  125. string[] hexValuesSplit = text.Split(' ');
  126. byte[] retData = new byte[hexValuesSplit.Length];
  127. int inum = 0;
  128. foreach (string hex in hexValuesSplit)
  129. {
  130. try
  131. {
  132. // Convert the number expressed in base-16 to an integer.
  133. retData[inum] = System.Convert.ToByte(hex, 16);
  134. ++inum;
  135. }
  136. catch(System.FormatException)
  137. {
  138. //ignore
  139. }
  140. }
  141. return retData;
  142. }
  143. public void CloseButtonClick()
  144. {
  145. HideWindow();
  146. if (SpupObject != null)
  147. SpupObject.Close();
  148. }
  149. public void HideWindow()
  150. {
  151. Panel.SetActive(false);
  152. PanelViewed = false;
  153. }
  154. public void ViewButtonClick()
  155. {
  156. bool cur_panelView = PanelViewed;
  157. SerialPortUtility.DebugConsole[] dc_objs = GameObject.FindObjectsOfType(typeof(SerialPortUtility.DebugConsole)) as SerialPortUtility.DebugConsole[];
  158. foreach (SerialPortUtility.DebugConsole dc_obj in dc_objs)
  159. dc_obj.HideWindow();
  160. if(cur_panelView == false)
  161. {
  162. //Open
  163. if (SpupObject != null)
  164. SpupObject.Open();
  165. Panel.SetActive(true);
  166. PanelViewed = true;
  167. }
  168. }
  169. }
  170. }