HomeView.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.SceneManagement;
  6. using System;
  7. using Newtonsoft.Json;
  8. using Newtonsoft.Json.Linq;
  9. /* 主界面 */
  10. public class HomeView : JCUnityLib.ViewBase
  11. {
  12. public static bool bOpenOtherView = false;
  13. public static string openName = "";
  14. [SerializeField] Image myAvatarSprite;
  15. [SerializeField] Text nickNameText;
  16. [SerializeField] GameObject[] genders;
  17. [SerializeField] GameObject btnConnectBow;
  18. [SerializeField] Text gameVersion;
  19. [SerializeField] GameObject friendBarTipTop;
  20. [SerializeField] GameObject friendBarTipBottom;
  21. [SerializeField] GameObject bettery;
  22. [SerializeField] GameObject betteryBar;
  23. [SerializeField] GameObject bettery2;
  24. [SerializeField] GameObject betteryBar2;
  25. [SerializeField] RawImage QRImage;
  26. [SerializeField] Text CoinCount;
  27. [SerializeField] HomeView_ChallengeOption challengeOption;
  28. public HomeView_ChallengeOption MyChallengeOption => challengeOption;
  29. [SerializeField] HomeView_TopBarView topBarView;
  30. public HomeView_TopBarView MyTopBarView => topBarView;
  31. public static HomeView ins;
  32. void Awake() {
  33. gameVersion.text = "V" + Application.version;
  34. gameVersion.gameObject.SetActive(false); //隐藏版本号
  35. if (CommonConfig.needToExamine) {
  36. transform.Find("LeftPanel/Item (1)").gameObject.SetActive(false);
  37. transform.Find("RightPanel/Item/Text").GetComponent<TextAutoLanguage>().SetText(1234562);
  38. transform.Find("RightPanel/Item (1)/Text").GetComponent<TextAutoLanguage>().SetText(1234563);
  39. transform.Find("RightPanel/Item (3)").gameObject.SetActive(false);
  40. }
  41. if (topBarView && challengeOption) topBarView.onChangeTypeEvent.AddListener(challengeOption.refreshList);
  42. FlushUrlQR();
  43. }
  44. public void FlushUrlQR()
  45. {
  46. QRImage.texture = StandaloneAPI.GetQR();
  47. }
  48. public void selectGame(int _startGameType)
  49. {
  50. // 获取父对象上的 HomeView_ChallengeOption 脚本
  51. HomeView_ChallengeOption challengeOption = GetComponentInParent<HomeView_ChallengeOption>();
  52. if (challengeOption != null)
  53. {
  54. Debug.Log("Found HomeView_ChallengeOption script on object: " + challengeOption.gameObject.name);
  55. }
  56. else
  57. {
  58. Debug.LogError("HomeView_ChallengeOption script not found on the parent object.");
  59. }
  60. ModeSelectView modeSelectView = GetComponentInParent<ModeSelectView>();
  61. if (modeSelectView != null)
  62. {
  63. Debug.Log("Found ModeSelectView script on object: " + modeSelectView.gameObject.name);
  64. }
  65. else
  66. {
  67. Debug.LogError("ModeSelectView script not found on the parent object.");
  68. }
  69. switch (_startGameType)
  70. {
  71. case 1: //奥运射箭
  72. {
  73. GameMgr.judgmentGameType = 1;
  74. modeSelectView.GoToLocalByName("SINGLE_PLYAER");
  75. }
  76. break;
  77. case 2: //打鸭子
  78. {
  79. challengeOption.StartGame(13);
  80. }
  81. break;
  82. case 3: //水果达人
  83. {
  84. challengeOption.StartGame(15);
  85. }
  86. break;
  87. case 4: //荒野射击
  88. {
  89. challengeOption.StartGame(14);
  90. }
  91. break;
  92. case 5: //打野狼
  93. {
  94. GameMgr.judgmentGameType = 5;
  95. modeSelectView.GoToLocalByName("SINGLE_PLYAER");
  96. }
  97. break;
  98. case 6: //打野鸡
  99. {
  100. GameMgr.judgmentGameType = 4;
  101. modeSelectView.GoToLocalByName("SINGLE_PLYAER");
  102. }
  103. break;
  104. case 7: //打野兔
  105. {
  106. GameMgr.judgmentGameType = 3;
  107. modeSelectView.GoToLocalByName("SINGLE_PLYAER");
  108. }
  109. break;
  110. default:
  111. break;
  112. }
  113. }
  114. void Start()
  115. {
  116. ins = this;
  117. transform.SetSiblingIndex(0); //放在ui最底层
  118. BluetoothHolder.Init();
  119. AudioMgr.Init();
  120. TopBarView.NeedShowIt(this);
  121. // if (ShootCheck.ins) ShootCheck.ins.AdjustNormalOrHightMode();
  122. if (LoginMgr.myUserInfo.id > 0 || (CommonConfig.StandaloneMode && UserPlayer.ins != null && UserPlayer.ins.hasGetUserInfo)) {
  123. RenderNameOrGender();
  124. RenderMyAvatarSprite();
  125. RenderDeviceNames();
  126. }
  127. if (CommonConfig.StandaloneMode)
  128. {
  129. transform.Find("FriendBar").gameObject.SetActive(false);
  130. transform.Find("RightPanel/Line (1)").gameObject.SetActive(false);
  131. transform.Find("RightPanel/Item (1)").gameObject.SetActive(false);
  132. }
  133. else StartCoroutine(RefreshFriendBar());
  134. //如果需要初始化跳转页面,这里处理
  135. Debug.Log("bOpenOtherView:"+ bOpenOtherView);
  136. if (bOpenOtherView) {
  137. bOpenOtherView = false;
  138. switch (openName)
  139. {
  140. case "DeviceView":
  141. // ViewMgr.Instance.ShowView<DeviceView>();
  142. break;
  143. default:
  144. break;
  145. }
  146. }
  147. //StartCoroutine(testTip());
  148. }
  149. IEnumerator testTip() {
  150. yield return new WaitForSeconds(1.0f);
  151. GameObject settingsViewObj = ViewManager2.getGameObjectAndShowView(ViewManager2.Path_HomeViewTip);
  152. settingsViewObj.GetComponent<HomeView_Tip>().setHomeTip(0);
  153. }
  154. void OnDestroy()
  155. {
  156. if (ins == this) ins = null;
  157. TopBarView.DontNeedShowIt(this);
  158. }
  159. void Update() {
  160. UpdateCheckRenderFriendTip();
  161. UpdateBtnForConnect();
  162. //更新电池显示
  163. updateBatteryStatus();
  164. }
  165. // float countingTime1 = 0;
  166. BluetoothStatusEnum bowStatus;
  167. void UpdateBtnForConnect() {
  168. if (BluetoothAim.ins && bowStatus != BluetoothAim.ins.status) {
  169. bowStatus = BluetoothAim.ins.status;
  170. (int textID, Color color) = BluetoothStatus.GetStatusInfo(BluetoothAim.ins.status);
  171. //btnConnectBow.GetComponentInChildren<TextAutoLanguage>().SetText(textID);
  172. //btnConnectBow.GetComponentInChildren<Text>().color = color;
  173. //btnConnectBow.transform.Find("Check").gameObject.SetActive(bowStatus == BluetoothStatusEnum.ConnectSuccess);
  174. }
  175. }
  176. public static bool ShowProminentBeforeConnectBLE(Action onAgree = null)
  177. {
  178. if (CommonConfig.StandaloneMode) return false;
  179. if (SceneManager.GetActiveScene().name.Equals("HeartRateBand")) return false;
  180. if (PlayerPrefs.GetInt("Location-Prominent", 0) == 0) {
  181. ModalView m = ModalView.Show();
  182. string pName = Application.productName;
  183. m.textKey = "Model_Location-Prominent";
  184. m.onAgreeTextKey = "common_next";
  185. m.onRejectTextKey = "common_cancel";
  186. m.textFormatArgs = new object[] {pName};
  187. m.onAgree = () => {
  188. PlayerPrefs.SetInt("Location-Prominent", 1);
  189. try {
  190. onAgree?.Invoke();
  191. } catch (System.Exception e) {
  192. Debug.LogError(e.Message + "\n" + e.StackTrace);
  193. }
  194. };
  195. return true;
  196. }
  197. return false;
  198. }
  199. public static bool ShowProminentBeforeConnectBLE_Event(Action onAgree = null,Action onReject = null)
  200. {
  201. if (CommonConfig.StandaloneMode) return false;
  202. if (SceneManager.GetActiveScene().name.Equals("HeartRateBand")) return false;
  203. if (PlayerPrefs.GetInt("Location-Prominent", 0) == 0)
  204. {
  205. ModalView m = ModalView.Show();
  206. string pName = Application.productName;
  207. m.textKey = "Model_Location-Prominent";
  208. m.onAgreeTextKey = "common_next";
  209. m.onRejectTextKey = "common_cancel";
  210. m.textFormatArgs = new object[] { pName };
  211. m.onAgree = () => {
  212. PlayerPrefs.SetInt("Location-Prominent", 1);
  213. try
  214. {
  215. onAgree?.Invoke();
  216. }
  217. catch (System.Exception e)
  218. {
  219. Debug.LogError(e.Message + "\n" + e.StackTrace);
  220. }
  221. };
  222. m.onReject = () =>
  223. {
  224. try
  225. {
  226. onReject?.Invoke();
  227. }
  228. catch (System.Exception e)
  229. {
  230. Debug.LogError(e.Message + "\n" + e.StackTrace);
  231. }
  232. };
  233. return true;
  234. }
  235. return false;
  236. }
  237. public void OnClick_ConnectBLE() {
  238. if (ShowProminentBeforeConnectBLE()) return;
  239. BluetoothAim.ins.DoConnect();
  240. }
  241. public void OnClick_ShowDeviceView() {
  242. AudioMgr.ins.PlayBtn();
  243. ViewMgr.Instance.ShowView<DeviceViewInfrared>();
  244. }
  245. public void RenderNameOrGender() {
  246. nickNameText.text = LoginMgr.myUserInfo.nickname;
  247. genders[LoginMgr.myUserInfo.gender == 2 ? 1 : 0].SetActive(true);
  248. genders[LoginMgr.myUserInfo.gender == 2 ? 0 : 1].SetActive(false);
  249. LayoutRebuilder.ForceRebuildLayoutImmediate(nickNameText.transform.parent.GetComponent<RectTransform>());
  250. }
  251. public void RenderMyAvatarSprite() {
  252. RoleMgr.SetAvatarToImage(myAvatarSprite, LoginMgr.myUserInfo.avatarID, LoginMgr.myUserInfo.avatarUrl);
  253. }
  254. public void RenderDeviceNames()
  255. {
  256. try {
  257. (DeviceInfo bowInfo, DeviceInfo arrowInfo) = DeviceMgr.ins.GetCurrentBowArrowInfo();
  258. // this.transform.Find("ShowBow/Text").GetComponent<TextAutoLanguage>().SetText(bowInfo.config.name);
  259. this.transform.Find("ShowBow/Text").GetComponent<TextAutoLanguage>().SetText(200000);
  260. this.transform.Find("ShowArrow/Text").GetComponent<TextAutoLanguage>().SetText(arrowInfo.config.name);
  261. } catch (System.Exception) {}
  262. }
  263. public Action action_OnClickStartGame;
  264. public void GoTo(string target) {
  265. AudioMgr.ins.PlayBtn();
  266. switch (target)
  267. {
  268. case "开始游戏":
  269. #if !UNITY_EDITOR
  270. if ((CommonConfig.isReleaseVersion || CommonConfig.StandaloneMode) && !BluetoothStatus.IsAllConnected())
  271. {
  272. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("请先连接设备"));
  273. return;
  274. }
  275. #endif
  276. ViewMgr.Instance.ShowView<GameStartView>();
  277. action_OnClickStartGame?.Invoke();
  278. break;
  279. case "联机游戏":
  280. if (CommonConfig.isReleaseVersion && !BluetoothStatus.IsAllConnected()) {
  281. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("请先连接设备"));
  282. return;
  283. }
  284. GlobalDataTemp.pkMatchType = PKMatchType.OnlinePK;
  285. ViewMgr.Instance.ShowView<PKGameOptionView>();
  286. break;
  287. case "我的":
  288. //ViewMgr.Instance.ShowView<MeView>();
  289. ViewManager2.ShowView(ViewManager2.Path_PersonalView);
  290. break;
  291. default:
  292. break;
  293. }
  294. }
  295. /* FriendBar */
  296. GameObject friendItemPrefab = null;
  297. Transform friendBarContent = null;
  298. IEnumerator RefreshFriendBar() {
  299. if (friendItemPrefab == null) {
  300. friendItemPrefab = transform.Find("FriendBar/Scroll View/Viewport/Content/Item").gameObject;
  301. Destroy(friendItemPrefab.GetComponent<Image>());
  302. friendItemPrefab.SetActive(false);
  303. }
  304. if (friendBarContent == null) {
  305. friendBarContent = transform.Find("FriendBar/Scroll View/Viewport/Content");
  306. }
  307. while (UserPlayer.ins == null || !UserPlayer.ins.loginAuthed || !UserPlayer.ins.hasGetUserInfo) {
  308. yield return null;
  309. }
  310. Action<JArray> cb = RenderFriendList;
  311. UserPlayer.ins.call("friendComp.getMyFriends", null, cb);
  312. }
  313. public void RenderFriendList(JArray list) {
  314. if (flag_SwapFriendAndRank != 0) return;
  315. for (int i = 1; i < friendBarContent.childCount; i++) {
  316. Destroy(friendBarContent.GetChild(i).gameObject);
  317. }
  318. if (list.Count > 0) {
  319. Color outColor;
  320. foreach (var itemInfo in list) {
  321. int friendID = itemInfo.Value<int>("friendID");
  322. int avatarID = itemInfo.Value<int>("avatarID");
  323. string avatarUrl = itemInfo.Value<string>("avatarUrl");
  324. string nickname = itemInfo.Value<string>("nickname");
  325. bool online = itemInfo.Value<bool>("online");
  326. long offlineTime = itemInfo.Value<long>("offlineTime");
  327. GameObject o = GameObject.Instantiate(friendItemPrefab, friendBarContent);
  328. o.SetActive(true);
  329. o.name = friendID.ToString();
  330. Image avatarImage = o.transform.Find("Avatar").GetComponent<Image>();
  331. RoleMgr.SetAvatarToImage(avatarImage, avatarID, avatarUrl);
  332. o.transform.Find("Nickname").GetComponent<Text>().text = nickname;
  333. TextAutoLanguage2 timeTip = o.transform.Find("Avatar/Line/Text").GetComponent<TextAutoLanguage2>();
  334. TimeUtil.SetOfflineTimeTextKey(offlineTime, online, timeTip);
  335. if (online) {
  336. ColorUtility.TryParseHtmlString("#12B525", out outColor);
  337. } else {
  338. ColorUtility.TryParseHtmlString("#A8B2BD", out outColor);
  339. }
  340. timeTip.GetComponent<Text>().color = outColor;
  341. }
  342. }
  343. }
  344. IEnumerator RefreshFriendRankBar() {
  345. if (friendItemPrefab == null) {
  346. friendItemPrefab = transform.Find("FriendBar/Scroll View/Viewport/Content/Item").gameObject;
  347. Destroy(friendItemPrefab.GetComponent<Image>());
  348. friendItemPrefab.SetActive(false);
  349. }
  350. if (friendBarContent == null) {
  351. friendBarContent = transform.Find("FriendBar/Scroll View/Viewport/Content");
  352. }
  353. while (UserPlayer.ins == null || !UserPlayer.ins.loginAuthed || !UserPlayer.ins.hasGetUserInfo) {
  354. yield return null;
  355. }
  356. Action<JArray> cb = RenderFriendRankList;
  357. UserPlayer.ins.call("rankComp.getFriendRankList", null, cb);
  358. }
  359. void RenderFriendRankList(JArray list) {
  360. if (flag_SwapFriendAndRank != 1) return;
  361. for (int i = 1; i < friendBarContent.childCount; i++) {
  362. Destroy(friendBarContent.GetChild(i).gameObject);
  363. }
  364. if (list.Count > 0) {
  365. foreach (var itemInfo in list) {
  366. int userID = itemInfo.Value<int>("id");
  367. int avatarID = itemInfo.Value<int>("avatarID");
  368. string avatarUrl = itemInfo.Value<string>("avatarUrl");
  369. string nickname = itemInfo.Value<string>("nickname");
  370. bool online = itemInfo.Value<bool>("online");
  371. long offlineTime = itemInfo.Value<long>("offlineTime");
  372. int rankNum = itemInfo.Value<int>("rankNum");
  373. GameObject o = GameObject.Instantiate(friendItemPrefab, friendBarContent);
  374. o.SetActive(true);
  375. o.name = userID.ToString();
  376. Image avatarImage = o.transform.Find("Avatar").GetComponent<Image>();
  377. RoleMgr.SetAvatarToImage(avatarImage, avatarID, avatarUrl);
  378. o.transform.Find("Nickname").GetComponent<Text>().text = nickname;
  379. TextAutoLanguage2 timeTip = o.transform.Find("Avatar/Line/Text").GetComponent<TextAutoLanguage2>();
  380. Action<Text> onApplyToNext = (t) => {
  381. if (TextAutoLanguage2.GetLanguage() == 0) {
  382. t.text = $"第{rankNum}名";
  383. } else {
  384. string str = rankNum.ToString();
  385. if (str.EndsWith("1")) t.text = $"{rankNum}st";
  386. else if (str.EndsWith("2")) t.text = $"{rankNum}nd";
  387. else if (str.EndsWith("3")) t.text = $"{rankNum}rd";
  388. else t.text = $"{rankNum}th";
  389. }
  390. Color outColor;
  391. if (rankNum == 1) ColorUtility.TryParseHtmlString("#F0E68C", out outColor);
  392. else if (rankNum == 2) ColorUtility.TryParseHtmlString("#E0FFFF", out outColor);
  393. else if (rankNum == 3) ColorUtility.TryParseHtmlString("#CD8162", out outColor);
  394. else ColorUtility.TryParseHtmlString("#EEE5DE", out outColor);
  395. t.color = outColor;
  396. };
  397. timeTip.onApplyToNext += onApplyToNext;
  398. }
  399. }
  400. }
  401. int flag_SwapFriendAndRank = 0;
  402. public void OnClick_SwapFriendAndRank() {
  403. AudioMgr.ins.PlayBtn();
  404. if (flag_SwapFriendAndRank == 0) {
  405. flag_SwapFriendAndRank = 1;
  406. transform.Find("FriendBar/FrameBtnTop").GetComponentInChildren<TextAutoLanguage2>().SetTextKey("rank_title");
  407. } else if (flag_SwapFriendAndRank == 1) {
  408. flag_SwapFriendAndRank = 0;
  409. transform.Find("FriendBar/FrameBtnTop").GetComponentInChildren<TextAutoLanguage2>().SetTextKey("friend_title");
  410. }
  411. RefreshFriendBarAccordingCurrentBar();
  412. }
  413. //根据栏目类型,调用对应的接口请求
  414. public void RefreshFriendBarAccordingCurrentBar()
  415. {
  416. if (flag_SwapFriendAndRank == 0)
  417. StartCoroutine(RefreshFriendBar());
  418. else if (flag_SwapFriendAndRank == 1)
  419. StartCoroutine(RefreshFriendRankBar());
  420. }
  421. public void OnClick_ShowDetail() {
  422. AudioMgr.ins.PlayBtn();
  423. if (flag_SwapFriendAndRank == 0) {
  424. ViewMgr.Instance.ShowView<FriendView>();
  425. } else if (flag_SwapFriendAndRank == 1) {
  426. ViewMgr.Instance.ShowView<RankView>();
  427. }
  428. }
  429. //检测是否需要提示有人加我好友
  430. bool _old_hasFriendRequest = false;
  431. int _old_flag_SwapFriendAndRank = 0;
  432. void UpdateCheckRenderFriendTip()
  433. {
  434. if (UserPlayer.ins == null) return;
  435. if (
  436. UserPlayer.ins.tempData.hasFriendRequest == _old_hasFriendRequest &&
  437. flag_SwapFriendAndRank == _old_flag_SwapFriendAndRank
  438. ) return;
  439. _old_hasFriendRequest = UserPlayer.ins.tempData.hasFriendRequest;
  440. _old_flag_SwapFriendAndRank = flag_SwapFriendAndRank;
  441. friendBarTipTop.SetActive(
  442. flag_SwapFriendAndRank == 1 &&
  443. UserPlayer.ins.tempData.hasFriendRequest);
  444. friendBarTipBottom.SetActive(
  445. flag_SwapFriendAndRank == 0 &&
  446. UserPlayer.ins.tempData.hasFriendRequest);
  447. }
  448. public void RenderBattery(int deviceID, int value)
  449. {
  450. Image img = betteryBar.GetComponent<Image>();
  451. img.fillAmount = value / 100f;
  452. }
  453. public void RenderBattery2() {
  454. int value = BluetoothAim.ins.get2PBattery();
  455. Image img = betteryBar2.GetComponent<Image>();
  456. img.fillAmount = value / 100f;
  457. }
  458. public void UpdateParent() {
  459. TopBarButtonInfo topBarButtonInfo = topBarView.GetCurrentTopBarButtonInfo();
  460. if (bettery != null)
  461. {
  462. bettery.transform.SetParent(topBarButtonInfo.container.transform);
  463. }
  464. if (bettery2 != null)
  465. {
  466. bettery2.transform.SetParent(topBarButtonInfo.container.transform);
  467. }
  468. }
  469. void updateBatteryStatus() {
  470. if (BluetoothAim.ins == null) return;
  471. //更新电池图标显示
  472. if (BluetoothAim.ins.status == BluetoothStatusEnum.ConnectSuccess)
  473. {
  474. if (!bettery.activeSelf)
  475. {
  476. TopBarButtonInfo topBarButtonInfo = topBarView.GetCurrentTopBarButtonInfo();
  477. bettery.transform.SetParent(topBarButtonInfo.container.transform);
  478. bettery.SetActive(true);
  479. }
  480. }
  481. else
  482. {
  483. if (bettery.activeSelf)
  484. {
  485. bettery.SetActive(false);
  486. }
  487. }
  488. //更新设备2电池图标显示
  489. if (BluetoothAim.ins.getSmartBowHelper2P() != null &&
  490. BluetoothAim.ins.getSmartBowHelper2P().GetBluetoothStatus() == SmartBowSDK.BluetoothStatusEnum.Connected)
  491. {
  492. if (!bettery2.activeSelf)
  493. {
  494. TopBarButtonInfo topBarButtonInfo = topBarView.GetCurrentTopBarButtonInfo();
  495. bettery2.transform.SetParent(topBarButtonInfo.container.transform);
  496. bettery2.SetActive(true);
  497. }
  498. }
  499. else
  500. {
  501. if (bettery2.activeSelf)
  502. {
  503. bettery2.SetActive(false);
  504. }
  505. }
  506. }
  507. }