DeviceView.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. /* 设备界面 */
  6. public class DeviceView : MonoBehaviour
  7. {
  8. Transform scrollContent;
  9. Transform itemPrefab;
  10. GameObject bowOptions;
  11. GameObject arrowOptions;
  12. Transform btnConnect;
  13. List<DeviceInfo> deviceList;
  14. void Start()
  15. {
  16. scrollContent = this.transform.Find("ScrollView/Viewport/Content");
  17. itemPrefab = scrollContent.GetChild(0);
  18. itemPrefab.gameObject.SetActive(false);
  19. bowOptions = this.transform.Find("ItemInfo/BowOptions").gameObject;
  20. arrowOptions = this.transform.Find("ItemInfo/ArrowOptions").gameObject;
  21. btnConnect = this.transform.Find("ItemShow/Connect");
  22. deviceList = DeviceMgr.ins.GetMyDeviceList();
  23. //初始化设备列表-并默认点开第一个设备
  24. foreach (DeviceInfo deviceInfo in deviceList)
  25. {
  26. DeviceConfig deviceConfig = deviceInfo.config;
  27. GameObject item = GameObject.Instantiate(itemPrefab.gameObject, scrollContent);
  28. Image modelImage = item.transform.Find("ModelBG/Model").GetComponent<Image>();
  29. modelImage.sprite = Resources.Load<Sprite>("Textures/Device/" + deviceConfig.model);
  30. modelImage.SetNativeSize();
  31. item.transform.Find("TextBG/Text").GetComponent<TextAutoLanguage>().SetText(deviceConfig.name);
  32. item.gameObject.SetActive(true);
  33. item.GetComponent<Button>().onClick.AddListener(delegate() {
  34. if (currentDeviceInfo == deviceInfo) return;
  35. AudioMgr.ins.PlayBtn();
  36. SelectItem(deviceInfo);
  37. });
  38. }
  39. SelectItem(deviceList[0]);
  40. //添加连接按钮监听
  41. btnConnect.GetComponent<Button>().onClick.AddListener(delegate() {
  42. bool changeDevice = !currentDeviceInfo.inuse;
  43. DeviceMgr.ins.UseDevice(currentDeviceInfo);
  44. if (changeDevice) {
  45. HomeView.ins.RenderDeviceNames();
  46. }
  47. if (currentDeviceInfo.config.type == 1) {
  48. BluetoothAim.ins.DoConnect();
  49. }
  50. if (currentDeviceInfo.config.type == 2) {
  51. BluetoothShoot.ins.DoConnect();
  52. }
  53. });
  54. //初始化弓的校准按钮
  55. Button[] bowOptionBtns = bowOptions.GetComponentsInChildren<Button>();
  56. for (int i = 0; i < bowOptionBtns.Length; i++)
  57. {
  58. int optionID = i;
  59. bowOptionBtns[i].onClick.AddListener(delegate() {
  60. AudioMgr.ins.PlayBtn();
  61. if (optionID == 0) {
  62. DeviceCalibrateView.Create(DeviceCalibrateItem.Gyr);
  63. } else if (optionID == 1) {
  64. DeviceCalibrateView.Create(DeviceCalibrateItem.Mag);
  65. } else if (optionID == 2) {
  66. AimHandler.ins.DoIdentity();
  67. }
  68. });
  69. }
  70. //初始化箭的加速计按钮选项
  71. // Button[] arrowOptionBtns = arrowOptions.GetComponentsInChildren<Button>();
  72. // for (int i = 0; i < arrowOptionBtns.Length; i++)
  73. // {
  74. // int optionID = i;
  75. // arrowOptionBtns[i].onClick.AddListener(delegate() {
  76. // AudioMgr.ins.PlayBtn();
  77. // //select
  78. // int acc = 16;
  79. // if (optionID == 1) {
  80. // acc = 64;
  81. // }
  82. // SelectAccForArrow(acc);
  83. // //save
  84. // LoginMgr.myUserInfo.arrowAccValue = acc;
  85. // LoginMgr.myUserInfo.Save();
  86. // //关闭设备连接,用于提醒更换设备
  87. // if (BluetoothShoot.ins.status == BluetoothStatusEnum.ConnectSuccess) {
  88. // BluetoothShoot.ins.DoConnect();
  89. // }
  90. // });
  91. // }
  92. // SelectAccForArrow(LoginMgr.myUserInfo.arrowAccValue);
  93. }
  94. void FixedUpdate()
  95. {
  96. UpdateBtnForConnect();
  97. }
  98. BluetoothStatusEnum bowStatus;
  99. // BluetoothStatusEnum arrowStatus;
  100. void UpdateBtnForConnect() {
  101. if (currentDeviceInfo == null) return;
  102. if (currentDeviceInfo.inuse && currentDeviceInfo.config.type == 1) {
  103. if (BluetoothAim.ins && bowStatus != BluetoothAim.ins.status) {
  104. bowStatus = BluetoothAim.ins.status;
  105. (int textID, Color color) = BluetoothStatus.GetStatusInfo(BluetoothAim.ins.status);
  106. btnConnect.GetComponentInChildren<TextAutoLanguage>().SetText(textID);
  107. btnConnect.GetComponentInChildren<Text>().color = color;
  108. if (BluetoothAim.ins.status == BluetoothStatusEnum.Connect) {
  109. btnConnect.GetComponent<Button>().enabled = true;
  110. } else {
  111. btnConnect.GetComponent<Button>().enabled = false;
  112. }
  113. }
  114. }
  115. // if (currentDeviceInfo.inuse && currentDeviceInfo.config.type == 2) {
  116. // if (BluetoothShoot.ins && arrowStatus != BluetoothShoot.ins.status) {
  117. // arrowStatus = BluetoothShoot.ins.status;
  118. // (int textID, Color color) = BluetoothStatus.GetStatusInfo(BluetoothShoot.ins.status);
  119. // btnConnect.GetComponentInChildren<TextAutoLanguage>().SetText(textID);
  120. // btnConnect.GetComponentInChildren<Text>().color = color;
  121. // if (BluetoothShoot.ins.status == BluetoothStatusEnum.Connect) {
  122. // btnConnect.GetComponent<Button>().enabled = true;
  123. // } else {
  124. // btnConnect.GetComponent<Button>().enabled = false;
  125. // }
  126. // }
  127. // }
  128. }
  129. void SelectAccForArrow(float acc)
  130. {
  131. for (int i = 0; i < arrowOptions.transform.childCount; i++)
  132. {
  133. Transform t = arrowOptions.transform.GetChild(i);
  134. t.GetComponent<Button>().enabled = true;
  135. t.GetComponent<Image>().sprite = Resources.Load<Sprite>("Textures/Common/ButtonGray");
  136. t.GetComponentInChildren<Text>().color = Color.gray;
  137. }
  138. Transform optionTransform;
  139. Color outColor;
  140. if (acc == 16)
  141. {
  142. optionTransform = arrowOptions.transform.GetChild(0);
  143. optionTransform.GetComponent<Button>().enabled = false;
  144. ColorUtility.TryParseHtmlString("#3D6B03", out outColor);
  145. optionTransform.GetComponentInChildren<Text>().color = outColor;
  146. optionTransform.GetComponent<Image>().sprite = Resources.Load<Sprite>("Textures/Common/ButtonGreen");
  147. }
  148. else if (acc == 64)
  149. {
  150. optionTransform = arrowOptions.transform.GetChild(1);
  151. optionTransform.GetComponent<Button>().enabled = false;
  152. ColorUtility.TryParseHtmlString("#B65A00", out outColor);
  153. optionTransform.GetComponentInChildren<Text>().color = outColor;
  154. optionTransform.GetComponent<Image>().sprite = Resources.Load<Sprite>("Textures/Common/ButtonYellow");
  155. }
  156. }
  157. DeviceInfo currentDeviceInfo;
  158. void SelectItem(DeviceInfo deviceInfo)
  159. {
  160. currentDeviceInfo = deviceInfo;
  161. Image modelImage = this.transform.Find("ItemShow/Model").GetComponent<Image>();
  162. modelImage.sprite = Resources.Load<Sprite>("Textures/Device/" + deviceInfo.config.model);
  163. modelImage.SetNativeSize();
  164. this.transform.Find("ItemInfo/Name").GetComponent<TextAutoLanguage>().SetText(deviceInfo.config.name);
  165. this.transform.Find("ItemInfo/Detail").GetComponent<TextAutoLanguage>().SetText(deviceInfo.config.detail);
  166. Transform stars = this.transform.Find("ItemInfo/Stars");
  167. Image starLight = stars.GetChild(0).GetComponent<Image>();
  168. Image starDark = stars.GetChild(1).GetComponent<Image>();
  169. starLight.gameObject.SetActive(false);
  170. starDark.gameObject.SetActive(false);
  171. for (int i = 1; i <= 5; i++) {
  172. stars.GetChild(i + 2).GetComponent<Image>().sprite =
  173. i <= deviceInfo.config.difficulty ? starLight.sprite : starDark.sprite;
  174. }
  175. bowOptions.SetActive(deviceInfo.config.type == 1);
  176. // arrowOptions.SetActive(deviceInfo.config.type == 2);
  177. //重置连接按钮
  178. (int textID, Color color) = BluetoothStatus.GetStatusInfo(BluetoothStatusEnum.Connect);
  179. btnConnect.GetComponentInChildren<TextAutoLanguage>().SetText(textID);
  180. btnConnect.GetComponentInChildren<Text>().color = color;
  181. btnConnect.GetComponent<Button>().enabled = true;
  182. bowStatus = BluetoothStatusEnum.None;
  183. // arrowStatus = BluetoothStatusEnum.None;
  184. //是弓才激活连接按钮
  185. btnConnect.gameObject.SetActive(currentDeviceInfo.config.type == 1);
  186. }
  187. public void LeftPointer()
  188. {
  189. AudioMgr.ins.PlayBtn();
  190. int index = deviceList.IndexOf(currentDeviceInfo) - 1;
  191. if (index < 0) {
  192. index = deviceList.Count - 1;
  193. }
  194. SelectItem(deviceList[index]);
  195. }
  196. public void RightPointer()
  197. {
  198. AudioMgr.ins.PlayBtn();
  199. int index = deviceList.IndexOf(currentDeviceInfo) + 1;
  200. if (index >= deviceList.Count) {
  201. index = 0;
  202. }
  203. SelectItem(deviceList[index]);
  204. }
  205. public void Back() {
  206. AudioMgr.ins.PlayBtn();
  207. Destroy(this.gameObject);
  208. }
  209. }