HomeView.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 : MonoBehaviour
  11. {
  12. [SerializeField] Image myAvatarSprite;
  13. [SerializeField] Text nickNameText;
  14. [SerializeField] GameObject[] genders;
  15. [SerializeField] GameObject btnConnectBow;
  16. [SerializeField] GameObject btnConnectArrow;
  17. [SerializeField] GameObject friendTip;
  18. public static HomeView ins;
  19. void Awake() {
  20. if (CommonConfig.needToExamine) {
  21. transform.Find("LeftPanel/Item (1)").gameObject.SetActive(false);
  22. transform.Find("RightPanel/Item/Text").GetComponent<TextAutoLanguage>().SetText(1234562);
  23. transform.Find("RightPanel/Item (1)/Text").GetComponent<TextAutoLanguage>().SetText(1234563);
  24. transform.Find("RightPanel/Item (3)").gameObject.SetActive(false);
  25. }
  26. }
  27. void Start()
  28. {
  29. ins = this;
  30. BluetoothHolder.Init();
  31. AudioMgr.Init();
  32. if (ShootCheck.ins) ShootCheck.ins.AdjustNormalOrHightMode();
  33. if (LoginMgr.myUserInfo.id > 0) {
  34. RenderNameOrGender();
  35. RenderMyAvatarSprite();
  36. RenderDeviceNames();
  37. }
  38. StartCoroutine(RefreshFriendBar());
  39. }
  40. void OnDestroy()
  41. {
  42. if (ins == this) ins = null;
  43. }
  44. void Update() {
  45. if (UserPlayer.ins != null) {
  46. if (UserPlayer.ins.tempData.hasFriendRequest && !friendTip.activeSelf) {
  47. friendTip.SetActive(true);
  48. }
  49. else if (!UserPlayer.ins.tempData.hasFriendRequest && friendTip.activeSelf) {
  50. friendTip.SetActive(false);
  51. }
  52. }
  53. }
  54. public void RenderNameOrGender() {
  55. nickNameText.text = LoginMgr.myUserInfo.nickname;
  56. genders[LoginMgr.myUserInfo.gender == 2 ? 1 : 0].SetActive(true);
  57. genders[LoginMgr.myUserInfo.gender == 2 ? 0 : 1].SetActive(false);
  58. LayoutRebuilder.ForceRebuildLayoutImmediate(nickNameText.transform.parent.GetComponent<RectTransform>());
  59. }
  60. public void RenderMyAvatarSprite() {
  61. myAvatarSprite.sprite = RoleMgr.GetAvatar(LoginMgr.myUserInfo.avatarID);
  62. }
  63. public void RenderDeviceNames()
  64. {
  65. try {
  66. (DeviceInfo bowInfo, DeviceInfo arrowInfo) = DeviceMgr.ins.GetCurrentBowArrowInfo();
  67. // this.transform.Find("ShowBow/Text").GetComponent<TextAutoLanguage>().SetText(bowInfo.config.name);
  68. this.transform.Find("ShowBow/Text").GetComponent<TextAutoLanguage>().SetText(200000);
  69. this.transform.Find("ShowArrow/Text").GetComponent<TextAutoLanguage>().SetText(arrowInfo.config.name);
  70. } catch (System.Exception) {}
  71. }
  72. public void GoTo(string target) {
  73. AudioMgr.ins.PlayBtn();
  74. switch (target)
  75. {
  76. case "开始游戏":
  77. if (CommonConfig.isReleaseVersion && !BluetoothStatus.IsAllConnected()) {
  78. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("请先连接设备"));
  79. return;
  80. }
  81. GameObject.Instantiate(SceneResMgr.ins.GetPrefab("GameStartView"));
  82. break;
  83. case "联机游戏":
  84. if (CommonConfig.isReleaseVersion && !BluetoothStatus.IsAllConnected()) {
  85. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("请先连接设备"));
  86. return;
  87. }
  88. GlobalDataTemp.pkMatchType = PKMatchType.OnlinePK;
  89. GameObject.Instantiate(SceneResMgr.ins.GetPrefab("PKGameOptionView"));
  90. break;
  91. case "好友":
  92. GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/FriendView"));
  93. break;
  94. case "排行磅":
  95. GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/RankView"));
  96. break;
  97. case "对战":
  98. GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/RoleSelectView"));
  99. break;
  100. case "教程":
  101. GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/CourseView"), Vector3.zero, new Quaternion());
  102. break;
  103. case "我的":
  104. GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/MeView"), Vector3.zero, new Quaternion());
  105. break;
  106. case "设备":
  107. GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/DeviceView"), Vector3.zero, new Quaternion());
  108. break;
  109. case "商城":
  110. GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/ShopView"), Vector3.zero, new Quaternion());
  111. break;
  112. default:
  113. break;
  114. }
  115. }
  116. /* FriendBar */
  117. GameObject friendItemPrefab = null;
  118. Transform friendBarContent = null;
  119. IEnumerator RefreshFriendBar() {
  120. if (friendItemPrefab == null) {
  121. friendItemPrefab = transform.Find("FriendBar/Scroll View/Viewport/Content/Item").gameObject;
  122. Destroy(friendItemPrefab.GetComponent<Image>());
  123. friendItemPrefab.SetActive(false);
  124. }
  125. if (friendBarContent == null) {
  126. friendBarContent = transform.Find("FriendBar/Scroll View/Viewport/Content");
  127. }
  128. while (LoginMgr.myUserInfo.id < 1) {
  129. yield return null;
  130. }
  131. Action<JArray> cb = RenderFriendList;
  132. UserPlayer.ins.call("friendComp.getMyFriends", null, cb);
  133. }
  134. public void RenderFriendList(JArray list) {
  135. if (flag_SwapFriendAndRank != 0) return;
  136. for (int i = 1; i < friendBarContent.childCount; i++) {
  137. Destroy(friendBarContent.GetChild(i).gameObject);
  138. }
  139. if (list.Count > 0) {
  140. Color outColor;
  141. foreach (var itemInfo in list) {
  142. int friendID = itemInfo.Value<int>("friendID");
  143. int avatarID = itemInfo.Value<int>("avatarID");
  144. string nickname = itemInfo.Value<string>("nickname");
  145. bool online = itemInfo.Value<bool>("online");
  146. long offlineTime = itemInfo.Value<long>("offlineTime");
  147. GameObject o = GameObject.Instantiate(friendItemPrefab, friendBarContent);
  148. o.SetActive(true);
  149. o.name = friendID.ToString();
  150. o.transform.Find("Avatar").GetComponent<Image>().sprite = RoleMgr.GetAvatar(avatarID);
  151. o.transform.Find("Nickname").GetComponent<Text>().text = nickname;
  152. TextAutoLanguage2 timeTip = o.transform.Find("Avatar/Line/Text").GetComponent<TextAutoLanguage2>();
  153. TimeUtil.SetOfflineTimeTextKey(offlineTime, online, timeTip);
  154. if (online) {
  155. ColorUtility.TryParseHtmlString("#12B525", out outColor);
  156. } else {
  157. ColorUtility.TryParseHtmlString("#A8B2BD", out outColor);
  158. }
  159. timeTip.GetComponent<Text>().color = outColor;
  160. }
  161. }
  162. }
  163. IEnumerator RefreshFriendRankBar() {
  164. if (friendItemPrefab == null) {
  165. friendItemPrefab = transform.Find("FriendBar/Scroll View/Viewport/Content/Item").gameObject;
  166. Destroy(friendItemPrefab.GetComponent<Image>());
  167. friendItemPrefab.SetActive(false);
  168. }
  169. if (friendBarContent == null) {
  170. friendBarContent = transform.Find("FriendBar/Scroll View/Viewport/Content");
  171. }
  172. while (LoginMgr.myUserInfo.id < 1) {
  173. yield return null;
  174. }
  175. Action<JArray> cb = RenderFriendRankList;
  176. UserPlayer.ins.call("rankComp.getFriendRankList", null, cb);
  177. }
  178. void RenderFriendRankList(JArray list) {
  179. if (flag_SwapFriendAndRank != 1) return;
  180. for (int i = 1; i < friendBarContent.childCount; i++) {
  181. Destroy(friendBarContent.GetChild(i).gameObject);
  182. }
  183. if (list.Count > 0) {
  184. foreach (var itemInfo in list) {
  185. int userID = itemInfo.Value<int>("id");
  186. int avatarID = itemInfo.Value<int>("avatarID");
  187. string nickname = itemInfo.Value<string>("nickname");
  188. bool online = itemInfo.Value<bool>("online");
  189. long offlineTime = itemInfo.Value<long>("offlineTime");
  190. int rankNum = itemInfo.Value<int>("rankNum");
  191. GameObject o = GameObject.Instantiate(friendItemPrefab, friendBarContent);
  192. o.SetActive(true);
  193. o.name = userID.ToString();
  194. o.transform.Find("Avatar").GetComponent<Image>().sprite = RoleMgr.GetAvatar(avatarID);
  195. o.transform.Find("Nickname").GetComponent<Text>().text = nickname;
  196. TextAutoLanguage2 timeTip = o.transform.Find("Avatar/Line/Text").GetComponent<TextAutoLanguage2>();
  197. Action<Text> onApplyToNext = (t) => {
  198. if (TextAutoLanguage2.GetLanguage() == 0) {
  199. t.text = $"第{rankNum}名";
  200. } else {
  201. string str = rankNum.ToString();
  202. if (str.EndsWith("1")) t.text = $"{rankNum}st";
  203. else if (str.EndsWith("2")) t.text = $"{rankNum}nd";
  204. else if (str.EndsWith("3")) t.text = $"{rankNum}rd";
  205. else t.text = $"{rankNum}th";
  206. }
  207. Color outColor;
  208. if (rankNum == 1) ColorUtility.TryParseHtmlString("#F0E68C", out outColor);
  209. else if (rankNum == 2) ColorUtility.TryParseHtmlString("#E0FFFF", out outColor);
  210. else if (rankNum == 3) ColorUtility.TryParseHtmlString("#CD8162", out outColor);
  211. else ColorUtility.TryParseHtmlString("#EEE5DE", out outColor);
  212. t.color = outColor;
  213. };
  214. timeTip.onApplyToNext += onApplyToNext;
  215. }
  216. }
  217. }
  218. int flag_SwapFriendAndRank = 0;
  219. public void OnClick_SwapFriendAndRank() {
  220. AudioMgr.ins.PlayBtn();
  221. if (flag_SwapFriendAndRank == 0) {
  222. flag_SwapFriendAndRank = 1;
  223. transform.Find("FriendBar/FrameBtnTop").GetComponentInChildren<TextAutoLanguage2>().SetTextKey("rank_title");
  224. StartCoroutine(RefreshFriendRankBar());
  225. } else if (flag_SwapFriendAndRank == 1) {
  226. flag_SwapFriendAndRank = 0;
  227. transform.Find("FriendBar/FrameBtnTop").GetComponentInChildren<TextAutoLanguage2>().SetTextKey("friend_title");
  228. StartCoroutine(RefreshFriendBar());
  229. }
  230. }
  231. public void OnClick_ShowDetail() {
  232. AudioMgr.ins.PlayBtn();
  233. if (flag_SwapFriendAndRank == 0) {
  234. GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/FriendView"));
  235. } else if (flag_SwapFriendAndRank == 1) {
  236. GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/RankView"));
  237. }
  238. }
  239. }