| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- /* 设备界面 */
- public class DeviceView : MonoBehaviour, MenuBackInterface
- {
- Transform scrollContent;
- Transform itemPrefab;
- GameObject bowOptions;
- GameObject arrowOptions;
- Transform btnConnect;
- List<DeviceInfo> deviceList;
- void Start()
- {
- PersistenHandler.ins?.menuBackCtr.views.Add(this);
- scrollContent = this.transform.Find("ScrollView/Viewport/Content");
- itemPrefab = scrollContent.GetChild(0);
- itemPrefab.gameObject.SetActive(false);
- bowOptions = this.transform.Find("ItemInfo/BowOptions").gameObject;
- arrowOptions = this.transform.Find("ItemInfo/ArrowOptions").gameObject;
- btnConnect = this.transform.Find("ItemShow/Connect");
- deviceList = DeviceMgr.ins.GetMyDeviceList();
- //初始化设备列表-并默认点开第一个设备
- foreach (DeviceInfo deviceInfo in deviceList)
- {
- DeviceConfig deviceConfig = deviceInfo.config;
- GameObject item = GameObject.Instantiate(itemPrefab.gameObject, scrollContent);
- Image modelImage = item.transform.Find("ModelBG/Model").GetComponent<Image>();
- modelImage.sprite = Resources.Load<Sprite>("Textures/Device/" + deviceConfig.model);
- modelImage.SetNativeSize();
- item.transform.Find("TextBG/Text").GetComponent<TextAutoLanguage>().SetText(deviceConfig.name);
- item.gameObject.SetActive(true);
- item.GetComponent<Button>().onClick.AddListener(delegate() {
- if (currentDeviceInfo == deviceInfo) return;
- AudioMgr.ins.PlayBtn();
- SelectItem(deviceInfo);
- });
- }
- SelectItem(deviceList[0]);
- //添加连接按钮监听
- btnConnect.GetComponent<Button>().onClick.AddListener(delegate() {
- bool changeDevice = !currentDeviceInfo.inuse;
- DeviceMgr.ins.UseDevice(currentDeviceInfo);
- if (changeDevice) {
- HomeView.ins.RenderDeviceNames();
- }
- if (currentDeviceInfo.config.type == 1) {
- BluetoothAim.ins.DoConnect();
- }
- if (currentDeviceInfo.config.type == 2) {
- BluetoothShoot.ins.DoConnect();
- }
- });
- //初始化弓的校准按钮
- Button[] bowOptionBtns = bowOptions.GetComponentsInChildren<Button>();
- for (int i = 0; i < bowOptionBtns.Length; i++)
- {
- int optionID = i;
- bowOptionBtns[i].onClick.AddListener(delegate() {
- AudioMgr.ins.PlayBtn();
- if (optionID == 0) {
- DeviceCalibrateView.Create(DeviceCalibrateItem.Gyr);
- } else if (optionID == 1) {
- DeviceCalibrateView.Create(DeviceCalibrateItem.Mag);
- } else if (optionID == 2) {
- AimHandler.ins.DoIdentity();
- }
- });
- }
- //初始化箭的加速计按钮选项
- // Button[] arrowOptionBtns = arrowOptions.GetComponentsInChildren<Button>();
- // for (int i = 0; i < arrowOptionBtns.Length; i++)
- // {
- // int optionID = i;
- // arrowOptionBtns[i].onClick.AddListener(delegate() {
- // AudioMgr.ins.PlayBtn();
- // //select
- // int acc = 16;
- // if (optionID == 1) {
- // acc = 64;
- // }
- // SelectAccForArrow(acc);
- // //save
- // LoginMgr.myUserInfo.arrowAccValue = acc;
- // LoginMgr.myUserInfo.Save();
- // //关闭设备连接,用于提醒更换设备
- // if (BluetoothShoot.ins.status == BluetoothStatusEnum.ConnectSuccess) {
- // BluetoothShoot.ins.DoConnect();
- // }
- // });
- // }
- // SelectAccForArrow(LoginMgr.myUserInfo.arrowAccValue);
- }
- void OnDestroy()
- {
- PersistenHandler.ins?.menuBackCtr.views.Remove(this);
- }
- void FixedUpdate()
- {
- UpdateBtnForConnect();
- }
- public bool OnMenuBack() {
- Destroy(gameObject);
- return true;
- }
- BluetoothStatusEnum bowStatus;
- // BluetoothStatusEnum arrowStatus;
- void UpdateBtnForConnect() {
- if (currentDeviceInfo == null) return;
- if (currentDeviceInfo.inuse && currentDeviceInfo.config.type == 1) {
- if (BluetoothAim.ins && bowStatus != BluetoothAim.ins.status) {
- bowStatus = BluetoothAim.ins.status;
- (int textID, Color color) = BluetoothStatus.GetStatusInfo(BluetoothAim.ins.status);
- btnConnect.GetComponentInChildren<TextAutoLanguage>().SetText(textID);
- btnConnect.GetComponentInChildren<Text>().color = color;
- if (BluetoothAim.ins.status == BluetoothStatusEnum.Connect) {
- btnConnect.GetComponent<Button>().enabled = true;
- } else {
- btnConnect.GetComponent<Button>().enabled = false;
- }
- }
- }
- // if (currentDeviceInfo.inuse && currentDeviceInfo.config.type == 2) {
- // if (BluetoothShoot.ins && arrowStatus != BluetoothShoot.ins.status) {
- // arrowStatus = BluetoothShoot.ins.status;
- // (int textID, Color color) = BluetoothStatus.GetStatusInfo(BluetoothShoot.ins.status);
- // btnConnect.GetComponentInChildren<TextAutoLanguage>().SetText(textID);
- // btnConnect.GetComponentInChildren<Text>().color = color;
- // if (BluetoothShoot.ins.status == BluetoothStatusEnum.Connect) {
- // btnConnect.GetComponent<Button>().enabled = true;
- // } else {
- // btnConnect.GetComponent<Button>().enabled = false;
- // }
- // }
- // }
- }
- void SelectAccForArrow(float acc)
- {
- for (int i = 0; i < arrowOptions.transform.childCount; i++)
- {
- Transform t = arrowOptions.transform.GetChild(i);
- t.GetComponent<Button>().enabled = true;
- t.GetComponent<Image>().sprite = Resources.Load<Sprite>("Textures/Common/ButtonGray");
- t.GetComponentInChildren<Text>().color = Color.gray;
- }
- Transform optionTransform;
- Color outColor;
- if (acc == 16)
- {
- optionTransform = arrowOptions.transform.GetChild(0);
- optionTransform.GetComponent<Button>().enabled = false;
- ColorUtility.TryParseHtmlString("#3D6B03", out outColor);
- optionTransform.GetComponentInChildren<Text>().color = outColor;
- optionTransform.GetComponent<Image>().sprite = Resources.Load<Sprite>("Textures/Common/ButtonGreen");
- }
- else if (acc == 64)
- {
- optionTransform = arrowOptions.transform.GetChild(1);
- optionTransform.GetComponent<Button>().enabled = false;
- ColorUtility.TryParseHtmlString("#B65A00", out outColor);
- optionTransform.GetComponentInChildren<Text>().color = outColor;
- optionTransform.GetComponent<Image>().sprite = Resources.Load<Sprite>("Textures/Common/ButtonYellow");
- }
- }
- DeviceInfo currentDeviceInfo;
- void SelectItem(DeviceInfo deviceInfo)
- {
- currentDeviceInfo = deviceInfo;
- Image modelImage = this.transform.Find("ItemShow/Model").GetComponent<Image>();
- modelImage.sprite = Resources.Load<Sprite>("Textures/Device/" + deviceInfo.config.model);
- modelImage.SetNativeSize();
- this.transform.Find("ItemInfo/Name").GetComponent<TextAutoLanguage>().SetText(deviceInfo.config.name);
- this.transform.Find("ItemInfo/Detail").GetComponent<TextAutoLanguage>().SetText(deviceInfo.config.detail);
- Transform stars = this.transform.Find("ItemInfo/Stars");
- Image starLight = stars.GetChild(0).GetComponent<Image>();
- Image starDark = stars.GetChild(1).GetComponent<Image>();
- starLight.gameObject.SetActive(false);
- starDark.gameObject.SetActive(false);
- for (int i = 1; i <= 5; i++) {
- stars.GetChild(i + 2).GetComponent<Image>().sprite =
- i <= deviceInfo.config.difficulty ? starLight.sprite : starDark.sprite;
- }
- bowOptions.SetActive(deviceInfo.config.type == 1);
- // arrowOptions.SetActive(deviceInfo.config.type == 2);
- //重置连接按钮
- (int textID, Color color) = BluetoothStatus.GetStatusInfo(BluetoothStatusEnum.Connect);
- btnConnect.GetComponentInChildren<TextAutoLanguage>().SetText(textID);
- btnConnect.GetComponentInChildren<Text>().color = color;
- btnConnect.GetComponent<Button>().enabled = true;
- bowStatus = BluetoothStatusEnum.None;
- // arrowStatus = BluetoothStatusEnum.None;
- //是弓才激活连接按钮
- btnConnect.gameObject.SetActive(currentDeviceInfo.config.type == 1);
- }
- public void LeftPointer()
- {
- AudioMgr.ins.PlayBtn();
- int index = deviceList.IndexOf(currentDeviceInfo) - 1;
- if (index < 0) {
- index = deviceList.Count - 1;
- }
- SelectItem(deviceList[index]);
- }
- public void RightPointer()
- {
- AudioMgr.ins.PlayBtn();
- int index = deviceList.IndexOf(currentDeviceInfo) + 1;
- if (index >= deviceList.Count) {
- index = 0;
- }
- SelectItem(deviceList[index]);
- }
- public void Back() {
- AudioMgr.ins.PlayBtn();
- Destroy(this.gameObject);
- }
- }
|