UIManagerSingle.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. using UnityEngine.UI;
  6. namespace InfraredManager
  7. {
  8. public class UIManagerSingle : MonoBehaviour
  9. {
  10. //连接弓箭按钮
  11. public Button mConnectBow;
  12. private Text mConnectBowText;
  13. //滚动的日志
  14. public GameObject logObj;
  15. public Text mScrollViewLogTextBow;
  16. private int bowLogTextCount = 0;
  17. //清空日志按钮
  18. public Button mClearLogBow;
  19. public Button mControlLogBtn;
  20. //进入野鸭模拟场景按钮
  21. public Button EnterDuckHunterBtn;
  22. //进入射箭模拟场景按钮
  23. public Button EnterGameBtn;
  24. //发送数据
  25. public Toggle SyncVector;
  26. #region 需要隐藏的面板节点
  27. public GameObject[] uiArray;
  28. public bool bGetPanelActive
  29. {
  30. get {
  31. return bPanelActive;
  32. }
  33. }
  34. bool bPanelActive = true;
  35. #endregion
  36. #region 需要调整的材质信息
  37. [Range(0.1f, 3.0f)]
  38. public float brightness = 1.0f; // 亮度
  39. public Slider brightnessSlider;
  40. public Text brightnessText;
  41. [Range(0.1f, 3.0f)]
  42. public float saturation = 1.0f; // 饱和度
  43. public Slider saturationSlider;
  44. public Text saturationText;
  45. [Range(0.1f, 3.0f)]
  46. public float contrast = 1.0f; // 对比度
  47. public Slider contrastSlider;
  48. public Text contrastText;
  49. public Material material; // 材质
  50. //亮度
  51. public void SliderBrightness()
  52. {
  53. var _value = brightnessSlider.value;
  54. brightness = ((int)(_value * 10)) / 10.0f;
  55. brightnessText.text = brightness + "";
  56. material.SetFloat("_Brightness", brightness); // 设置亮度
  57. }
  58. //饱和度
  59. public void SliderSaturation()
  60. {
  61. var _value = saturationSlider.value;
  62. saturation = ((int)(_value * 10)) / 10.0f;
  63. saturationText.text = saturation + "";
  64. material.SetFloat("_Saturation", saturation); // 设置饱和度
  65. }
  66. //对比度
  67. public void SliderContrast()
  68. {
  69. var _value = contrastSlider.value;
  70. contrast = ((int)(_value * 10)) / 10.0f;
  71. contrastText.text = contrast + "";
  72. material.SetFloat("_Contrast", contrast); // 设置对比度
  73. }
  74. public void onReset() {
  75. brightnessSlider.value = 1.0f;
  76. saturationSlider.value = 1.0f;
  77. contrastSlider.value = 1.0f;
  78. SliderBrightness();
  79. SliderSaturation();
  80. SliderContrast();
  81. }
  82. #endregion
  83. void Awake()
  84. {
  85. if (ConnetDevicesSingle.ins)
  86. ConnetDevicesSingle.ins.OnLogAction += SetLogText;
  87. Debug.Log("SetLogText!");
  88. }
  89. // Start is called before the first frame update
  90. void Start()
  91. {
  92. mConnectBow.onClick.AddListener(onConnectBowDevice);
  93. mConnectBowText = mConnectBow.GetComponentInChildren<Text>();
  94. mClearLogBow.onClick.AddListener(clearLogText);
  95. mControlLogBtn.onClick.AddListener(onControlLog);
  96. EnterGameBtn.onClick.AddListener(enterGame);
  97. EnterDuckHunterBtn.onClick.AddListener(enterDuckHunter);
  98. EnterDuckHunterBtn.interactable = true;
  99. EnterGameBtn.interactable = true;
  100. mConnectBow.interactable = false;
  101. //默认显示并且在start 读取
  102. SyncVector.isOn = PlayerPrefs.GetInt("CrossHairImageActive", 1) == 1;
  103. }
  104. void OnDestroy()
  105. {
  106. if (ConnetDevicesSingle.ins)
  107. ConnetDevicesSingle.ins.OnLogAction -= SetLogText;
  108. }
  109. // Update is called once per frame
  110. void Update()
  111. {
  112. //更新ui
  113. if (ConnetDevicesSingle.ins)
  114. {
  115. bool mBowConnected = ConnetDevicesSingle.ins.mBluetoothHelperBow != null && ConnetDevicesSingle.ins.mBluetoothHelperBow.isConnected();
  116. bool mBowIsScanning = ConnetDevicesSingle.ins.mIsBowScanning;
  117. bool mBowIsConnecting = ConnetDevicesSingle.ins.mIsBowConnecting;
  118. if (!mBowConnected && !mBowIsScanning && !mBowIsConnecting)
  119. {
  120. //未连接,未扫描,不是连接中
  121. mConnectBowText.text = "点击连接弓箭";
  122. if (!mConnectBow.interactable)
  123. mConnectBow.interactable = true;
  124. }
  125. else if (!mBowConnected && mBowIsScanning)
  126. {
  127. //扫描中
  128. mConnectBowText.text = "扫描中...";
  129. if (mConnectBow.interactable)
  130. mConnectBow.interactable = false;
  131. }
  132. else if (mBowConnected)
  133. {
  134. //已连接
  135. mConnectBowText.text = "已连接";
  136. //if (mConnectBow.interactable)
  137. // mConnectBow.interactable = false;
  138. if (!mConnectBow.interactable)
  139. mConnectBow.interactable = true;
  140. }
  141. //if (mBowConnected)
  142. //{
  143. // if (!EnterDuckHunterBtn.interactable)
  144. // EnterDuckHunterBtn.interactable = true;
  145. // if (!EnterGameBtn.interactable)
  146. // EnterGameBtn.interactable = true;
  147. //}
  148. //else
  149. //{
  150. // if (EnterDuckHunterBtn.interactable)
  151. // EnterDuckHunterBtn.interactable = false;
  152. // if (EnterGameBtn.interactable)
  153. // EnterGameBtn.interactable = false;
  154. //}
  155. }
  156. }
  157. //连接弓箭设备
  158. void onConnectBowDevice()
  159. {
  160. ConnetDevicesSingle.ins?.ConnectBLE();
  161. }
  162. public void SetLogText(string text)
  163. {
  164. //Debug.Log("SetLogText:" + text);
  165. if (bowLogTextCount > 200)
  166. {
  167. clearLogText();
  168. }
  169. mScrollViewLogTextBow.text += text + "\n";
  170. bowLogTextCount++;
  171. }
  172. public void clearLogText()
  173. {
  174. mScrollViewLogTextBow.text = "";
  175. bowLogTextCount = 0;
  176. }
  177. public void onControlLog() {
  178. logObj.SetActive(!logObj.activeSelf);
  179. }
  180. public void enterGame()
  181. {
  182. SceneManager.LoadScene("Game_InfraredCamera", LoadSceneMode.Single);
  183. hideManager();
  184. }
  185. public void enterDuckHunter()
  186. {
  187. SceneManager.LoadScene("DuckHunter_Test2", LoadSceneMode.Single);
  188. hideManager();
  189. }
  190. public void SendGetConfig()
  191. {
  192. }
  193. public void SendGetShoot()
  194. {
  195. ConnetDevicesSingle.ins?.ButtonShoot();
  196. }
  197. public void onHideCrosshair()
  198. {
  199. ConnetDevicesSingle.ins?.onHideCrosshair();
  200. SyncVector.isOn = false;
  201. }
  202. public void onShowCrosshair()
  203. {
  204. ConnetDevicesSingle.ins?.onShowCrosshair();
  205. SyncVector.isOn = true;
  206. }
  207. public void onToggleSyncVector() {
  208. if (SyncVector.isOn)
  209. {
  210. ConnetDevicesSingle.ins?.onShowCrosshair();
  211. }
  212. else {
  213. ConnetDevicesSingle.ins?.onHideCrosshair();
  214. }
  215. }
  216. //隐藏控制面板UI
  217. public void hideManager()
  218. {
  219. // transform.parent.GetComponent<Canvas>().gameObject.SetActive(false);
  220. bPanelActive = false;
  221. for (int i = 0; i < uiArray.Length; i++)
  222. {
  223. uiArray[i].SetActive(false);
  224. }
  225. }
  226. //显示控制面板UI
  227. public void showManager()
  228. {
  229. // transform.parent.GetComponent<Canvas>().gameObject.SetActive(true);
  230. bPanelActive = true;
  231. for (int i = 0; i < uiArray.Length; i++)
  232. {
  233. uiArray[i].SetActive(true);
  234. }
  235. }
  236. }
  237. }