| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using SmartBowLib;
- using SmartBowLib.lebo;
- using Newtonsoft.Json.Linq;
- /*
- * 投屏功能的用户操作界面
- */
- public class ScreenProjectionView : MonoBehaviour, MenuBackInterface
- {
- //stage1
- public Transform uiDeviceListContent;
- public GameObject uiDeviceListItem;
- public Button btnResolution;
- //stage2
- public Text textDeviceLabel;
- public Button btnStop;
- private int _stageNum = 1;
- public static ScreenProjectionView ins;
- void Awake() {
- ins = this;
- DontDestroyOnLoad(this);
- uiDeviceListItem.SetActive(false);
- }
- void Start() {
- InitBtn_SetResolution();
- btnStop.onClick.AddListener(OnClick_BtnStop);
- MsgReceiver.ins.onMessage += onMessage;
- #if UNITY_ANDROID && !UNITY_EDITOR
- ScreenProjection.Init();
- if (!ScreenProjection.ins) _needReinitSDK = true;
- #endif
- }
- void OnDestroy()
- {
- if (ins == this) ins = null;
- MsgReceiver.ins.onMessage -= onMessage;
- }
- void OnEnable() {
- PersistenHandler.ins?.menuBackCtr.views.Add(this);
- }
- void OnDisable() {
- PersistenHandler.ins?.menuBackCtr.views.Remove(this);
- }
- public bool OnMenuBack() {
- LogicBtnBack();
- return true;
- }
- void onMessage(string tag, string msg) {
- if (tag == "lebo.onBrowse") OnBrowse(JArray.Parse(msg));
- }
- class LeboDeviceServiceInfo {
- public string uid;
- public string name;
- public GameObject o;
- public bool valid;
- }
- Dictionary<string, LeboDeviceServiceInfo> deviceMap = new Dictionary<string, LeboDeviceServiceInfo>();
- void OnBrowse(JArray list) {
- foreach (var item in deviceMap) { item.Value.valid = false; }
- foreach (var jo in list) {
- string uid = jo.Value<string>("uid");
- string name = jo.Value<string>("name");
- if (deviceMap.ContainsKey(uid)) {
- deviceMap[uid].valid = true;
- continue;
- }
- var info = new LeboDeviceServiceInfo();
- info.uid = uid;
- info.name = name;
- GameObject o = Instantiate(uiDeviceListItem, uiDeviceListContent);
- o.GetComponentInChildren<Text>().text = name;
- o.GetComponent<Button>().onClick.AddListener(() => {
- AudioMgr.ins.PlayBtn();
- ChangeStage(2);
- textDeviceLabel.text = name + $" <color=#00FF00>{btnResolution.GetComponentInChildren<Text>().text}</color> ";
- ScreenProjection.ins.Connect(uid, name);
- ScreenProjection.ins.StopBrowse();
- });
- o.SetActive(true);
- info.o = o;
- info.valid = true;
- deviceMap[uid] = info;
- }
- List<string> willDels = null;
- foreach (var item in deviceMap) {
- if (!item.Value.valid) {
- if (willDels == null) willDels = new List<string>();
- willDels.Add(item.Value.uid);
- };
- }
- if (willDels != null) {
- foreach (var mapkey in willDels) {
- Destroy(deviceMap[mapkey].o);
- deviceMap.Remove(mapkey);
- }
- }
- }
- void InitBtn_SetResolution() {
- btnResolution.onClick.AddListener(() => {
- AudioMgr.ins.PlayBtn();
- ScreenProjection.mirrorResolutionID = ScreenProjection.mirrorResolutionID % 2 + 1;
- RefreshBtn_SetResolution();
- });
- RefreshBtn_SetResolution();
- }
- void RefreshBtn_SetResolution() {
- if (ScreenProjection.mirrorResolutionID == 1) {
- btnResolution.GetComponentInChildren<Text>().text = "720P";
- } else if (ScreenProjection.mirrorResolutionID == 2) {
- btnResolution.GetComponentInChildren<Text>().text = "1080P";
- }
- }
- public void OnClick_BtnBack() {
- AudioMgr.ins.PlayBtn();
- LogicBtnBack();
- }
- void LogicBtnBack() {
- if (ScreenProjection.ins && ScreenProjection.ins.HasDeviceInConnect()) {
- gameObject.SetActive(false);
- } else {
- ScreenProjection.Release();
- Destroy(gameObject);
- }
- }
- void ChangeStage(int stage) {
- _stageNum = stage;
- if (_stageNum == 1) {
- transform.Find("Stage1").gameObject.SetActive(true);
- transform.Find("Stage2").gameObject.SetActive(false);
- OnBrowse(new JArray());
- } else if (_stageNum == 2) {
- transform.Find("Stage1").gameObject.SetActive(false);
- transform.Find("Stage2").gameObject.SetActive(true);
- }
- }
- void OnClick_BtnStop() {
- AudioMgr.ins.PlayBtn();
- ScreenProjection.Release();
- ChangeStage(1);
- _needReinitSDK = true;
- }
- bool _needReinitSDK = false;
- void Update() {
- if (_needReinitSDK) {
- ScreenProjection.Init();
- if (ScreenProjection.ins) _needReinitSDK = false;
- }
- }
- bool _pwdInited = false;
- InputField _pwdInputField;
- public void ShowInputPWD() {
- if (!_pwdInited) {
- _pwdInited = true;
- _pwdInputField = transform.Find("PWD/InputField").GetComponent<InputField>();
- transform.Find("PWD/BtnEnter").GetComponent<Button>().onClick.AddListener(OnClick_InputPWDEnter);
- }
- _pwdInputField.text = "";
- transform.Find("PWD").gameObject.SetActive(true);
- }
- void OnClick_InputPWDEnter() {
- AudioMgr.ins.PlayBtn();
- transform.Find("PWD").gameObject.SetActive(false);
- ScreenProjection.ins.setPassword(_pwdInputField.text);
- ScreenProjection.ins.StartMirror();
- }
- }
|