DeviceView.cs 8.8 KB

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