using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; using System; using Newtonsoft.Json; using Newtonsoft.Json.Linq; /* 主界面 */ public class HomeView : JCUnityLib.ViewBase { public static bool bOpenOtherView = false; public static string openName = ""; [SerializeField] Image myAvatarSprite; [SerializeField] Text nickNameText; [SerializeField] GameObject[] genders; [SerializeField] GameObject btnConnectBow; [SerializeField] Text gameVersion; [SerializeField] GameObject friendBarTipTop; [SerializeField] GameObject friendBarTipBottom; [SerializeField] GameObject bettery; [SerializeField] GameObject betteryBar; [SerializeField] GameObject bettery2; [SerializeField] GameObject betteryBar2; [SerializeField] HomeView_ChallengeOption challengeOption; public HomeView_ChallengeOption MyChallengeOption => challengeOption; [SerializeField] HomeView_TopBarView topBarView; public HomeView_TopBarView MyTopBarView => topBarView; public static HomeView ins; void Awake() { gameVersion.text = "V" + Application.version; gameVersion.gameObject.SetActive(false); //隐藏版本号 if (CommonConfig.needToExamine) { transform.Find("LeftPanel/Item (1)").gameObject.SetActive(false); transform.Find("RightPanel/Item/Text").GetComponent().SetText(1234562); transform.Find("RightPanel/Item (1)/Text").GetComponent().SetText(1234563); transform.Find("RightPanel/Item (3)").gameObject.SetActive(false); } if (topBarView && challengeOption) topBarView.onChangeTypeEvent.AddListener(challengeOption.refreshList); } void Start() { ins = this; transform.SetSiblingIndex(0); //放在ui最底层 BluetoothHolder.Init(); AudioMgr.Init(); TopBarView.NeedShowIt(this); // if (ShootCheck.ins) ShootCheck.ins.AdjustNormalOrHightMode(); if (LoginMgr.myUserInfo.id > 0 || (CommonConfig.StandaloneMode && UserPlayer.ins != null && UserPlayer.ins.hasGetUserInfo)) { RenderNameOrGender(); RenderMyAvatarSprite(); RenderDeviceNames(); } if (CommonConfig.StandaloneMode) { transform.Find("FriendBar").gameObject.SetActive(false); transform.Find("RightPanel/Line (1)").gameObject.SetActive(false); transform.Find("RightPanel/Item (1)").gameObject.SetActive(false); } else StartCoroutine(RefreshFriendBar()); //如果需要初始化跳转页面,这里处理 Debug.Log("bOpenOtherView:"+ bOpenOtherView); if (bOpenOtherView) { bOpenOtherView = false; switch (openName) { case "DeviceView": // ViewMgr.Instance.ShowView(); break; default: break; } } //StartCoroutine(testTip()); } IEnumerator testTip() { yield return new WaitForSeconds(1.0f); GameObject settingsViewObj = ViewManager2.getGameObjectAndShowView(ViewManager2.Path_HomeViewTip); settingsViewObj.GetComponent().setHomeTip(0); } void OnDestroy() { if (ins == this) ins = null; TopBarView.DontNeedShowIt(this); } void Update() { UpdateCheckRenderFriendTip(); UpdateBtnForConnect(); //更新电池显示 updateBatteryStatus(); } // float countingTime1 = 0; BluetoothStatusEnum bowStatus; void UpdateBtnForConnect() { if (BluetoothAim.ins && bowStatus != BluetoothAim.ins.status) { bowStatus = BluetoothAim.ins.status; (int textID, Color color) = BluetoothStatus.GetStatusInfo(BluetoothAim.ins.status); //btnConnectBow.GetComponentInChildren().SetText(textID); //btnConnectBow.GetComponentInChildren().color = color; //btnConnectBow.transform.Find("Check").gameObject.SetActive(bowStatus == BluetoothStatusEnum.ConnectSuccess); } } public static bool ShowProminentBeforeConnectBLE(Action onAgree = null) { if (CommonConfig.StandaloneMode) return false; if (SceneManager.GetActiveScene().name.Equals("HeartRateBand")) return false; if (PlayerPrefs.GetInt("Location-Prominent", 0) == 0) { ModalView m = ModalView.Show(); string pName = Application.productName; m.textKey = "Model_Location-Prominent"; m.onAgreeTextKey = "common_next"; m.onRejectTextKey = "common_cancel"; m.textFormatArgs = new object[] {pName}; m.onAgree = () => { PlayerPrefs.SetInt("Location-Prominent", 1); try { onAgree?.Invoke(); } catch (System.Exception e) { Debug.LogError(e.Message + "\n" + e.StackTrace); } }; return true; } return false; } public static bool ShowProminentBeforeConnectBLE_Event(Action onAgree = null,Action onReject = null) { if (CommonConfig.StandaloneMode) return false; if (SceneManager.GetActiveScene().name.Equals("HeartRateBand")) return false; if (PlayerPrefs.GetInt("Location-Prominent", 0) == 0) { ModalView m = ModalView.Show(); string pName = Application.productName; m.textKey = "Model_Location-Prominent"; m.onAgreeTextKey = "common_next"; m.onRejectTextKey = "common_cancel"; m.textFormatArgs = new object[] { pName }; m.onAgree = () => { PlayerPrefs.SetInt("Location-Prominent", 1); try { onAgree?.Invoke(); } catch (System.Exception e) { Debug.LogError(e.Message + "\n" + e.StackTrace); } }; m.onReject = () => { try { onReject?.Invoke(); } catch (System.Exception e) { Debug.LogError(e.Message + "\n" + e.StackTrace); } }; return true; } return false; } public void OnClick_ConnectBLE() { if (ShowProminentBeforeConnectBLE()) return; BluetoothAim.ins.DoConnect(); } public void OnClick_ShowDeviceView() { AudioMgr.ins.PlayBtn(); ViewMgr.Instance.ShowView(); } public void RenderNameOrGender() { nickNameText.text = LoginMgr.myUserInfo.nickname; genders[LoginMgr.myUserInfo.gender == 2 ? 1 : 0].SetActive(true); genders[LoginMgr.myUserInfo.gender == 2 ? 0 : 1].SetActive(false); LayoutRebuilder.ForceRebuildLayoutImmediate(nickNameText.transform.parent.GetComponent()); } public void RenderMyAvatarSprite() { RoleMgr.SetAvatarToImage(myAvatarSprite, LoginMgr.myUserInfo.avatarID, LoginMgr.myUserInfo.avatarUrl); } public void RenderDeviceNames() { try { (DeviceInfo bowInfo, DeviceInfo arrowInfo) = DeviceMgr.ins.GetCurrentBowArrowInfo(); // this.transform.Find("ShowBow/Text").GetComponent().SetText(bowInfo.config.name); this.transform.Find("ShowBow/Text").GetComponent().SetText(200000); this.transform.Find("ShowArrow/Text").GetComponent().SetText(arrowInfo.config.name); } catch (System.Exception) {} } public Action action_OnClickStartGame; public void GoTo(string target) { AudioMgr.ins.PlayBtn(); switch (target) { case "开始游戏": #if !UNITY_EDITOR if ((CommonConfig.isReleaseVersion || CommonConfig.StandaloneMode) && !BluetoothStatus.IsAllConnected()) { PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("请先连接设备")); return; } #endif ViewMgr.Instance.ShowView(); action_OnClickStartGame?.Invoke(); break; case "联机游戏": if (CommonConfig.isReleaseVersion && !BluetoothStatus.IsAllConnected()) { PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("请先连接设备")); return; } GlobalDataTemp.pkMatchType = PKMatchType.OnlinePK; ViewMgr.Instance.ShowView(); break; case "我的": //ViewMgr.Instance.ShowView(); if (!CommonConfig.StandaloneMode) ViewManager2.ShowView(ViewManager2.Path_PersonalView); break; default: break; } } /* FriendBar */ GameObject friendItemPrefab = null; Transform friendBarContent = null; IEnumerator RefreshFriendBar() { if (friendItemPrefab == null) { friendItemPrefab = transform.Find("FriendBar/Scroll View/Viewport/Content/Item").gameObject; Destroy(friendItemPrefab.GetComponent()); friendItemPrefab.SetActive(false); } if (friendBarContent == null) { friendBarContent = transform.Find("FriendBar/Scroll View/Viewport/Content"); } while (UserPlayer.ins == null || !UserPlayer.ins.loginAuthed || !UserPlayer.ins.hasGetUserInfo) { yield return null; } Action cb = RenderFriendList; UserPlayer.ins.call("friendComp.getMyFriends", null, cb); } public void RenderFriendList(JArray list) { if (flag_SwapFriendAndRank != 0) return; for (int i = 1; i < friendBarContent.childCount; i++) { Destroy(friendBarContent.GetChild(i).gameObject); } if (list.Count > 0) { Color outColor; foreach (var itemInfo in list) { int friendID = itemInfo.Value("friendID"); int avatarID = itemInfo.Value("avatarID"); string avatarUrl = itemInfo.Value("avatarUrl"); string nickname = itemInfo.Value("nickname"); bool online = itemInfo.Value("online"); long offlineTime = itemInfo.Value("offlineTime"); GameObject o = GameObject.Instantiate(friendItemPrefab, friendBarContent); o.SetActive(true); o.name = friendID.ToString(); Image avatarImage = o.transform.Find("Avatar").GetComponent(); RoleMgr.SetAvatarToImage(avatarImage, avatarID, avatarUrl); o.transform.Find("Nickname").GetComponent().text = nickname; TextAutoLanguage2 timeTip = o.transform.Find("Avatar/Line/Text").GetComponent(); TimeUtil.SetOfflineTimeTextKey(offlineTime, online, timeTip); if (online) { ColorUtility.TryParseHtmlString("#12B525", out outColor); } else { ColorUtility.TryParseHtmlString("#A8B2BD", out outColor); } timeTip.GetComponent().color = outColor; } } } IEnumerator RefreshFriendRankBar() { if (friendItemPrefab == null) { friendItemPrefab = transform.Find("FriendBar/Scroll View/Viewport/Content/Item").gameObject; Destroy(friendItemPrefab.GetComponent()); friendItemPrefab.SetActive(false); } if (friendBarContent == null) { friendBarContent = transform.Find("FriendBar/Scroll View/Viewport/Content"); } while (UserPlayer.ins == null || !UserPlayer.ins.loginAuthed || !UserPlayer.ins.hasGetUserInfo) { yield return null; } Action cb = RenderFriendRankList; UserPlayer.ins.call("rankComp.getFriendRankList", null, cb); } void RenderFriendRankList(JArray list) { if (flag_SwapFriendAndRank != 1) return; for (int i = 1; i < friendBarContent.childCount; i++) { Destroy(friendBarContent.GetChild(i).gameObject); } if (list.Count > 0) { foreach (var itemInfo in list) { int userID = itemInfo.Value("id"); int avatarID = itemInfo.Value("avatarID"); string avatarUrl = itemInfo.Value("avatarUrl"); string nickname = itemInfo.Value("nickname"); bool online = itemInfo.Value("online"); long offlineTime = itemInfo.Value("offlineTime"); int rankNum = itemInfo.Value("rankNum"); GameObject o = GameObject.Instantiate(friendItemPrefab, friendBarContent); o.SetActive(true); o.name = userID.ToString(); Image avatarImage = o.transform.Find("Avatar").GetComponent(); RoleMgr.SetAvatarToImage(avatarImage, avatarID, avatarUrl); o.transform.Find("Nickname").GetComponent().text = nickname; TextAutoLanguage2 timeTip = o.transform.Find("Avatar/Line/Text").GetComponent(); Action onApplyToNext = (t) => { if (TextAutoLanguage2.GetLanguage() == 0) { t.text = $"第{rankNum}名"; } else { string str = rankNum.ToString(); if (str.EndsWith("1")) t.text = $"{rankNum}st"; else if (str.EndsWith("2")) t.text = $"{rankNum}nd"; else if (str.EndsWith("3")) t.text = $"{rankNum}rd"; else t.text = $"{rankNum}th"; } Color outColor; if (rankNum == 1) ColorUtility.TryParseHtmlString("#F0E68C", out outColor); else if (rankNum == 2) ColorUtility.TryParseHtmlString("#E0FFFF", out outColor); else if (rankNum == 3) ColorUtility.TryParseHtmlString("#CD8162", out outColor); else ColorUtility.TryParseHtmlString("#EEE5DE", out outColor); t.color = outColor; }; timeTip.onApplyToNext += onApplyToNext; } } } int flag_SwapFriendAndRank = 0; public void OnClick_SwapFriendAndRank() { AudioMgr.ins.PlayBtn(); if (flag_SwapFriendAndRank == 0) { flag_SwapFriendAndRank = 1; transform.Find("FriendBar/FrameBtnTop").GetComponentInChildren().SetTextKey("rank_title"); } else if (flag_SwapFriendAndRank == 1) { flag_SwapFriendAndRank = 0; transform.Find("FriendBar/FrameBtnTop").GetComponentInChildren().SetTextKey("friend_title"); } RefreshFriendBarAccordingCurrentBar(); } //根据栏目类型,调用对应的接口请求 public void RefreshFriendBarAccordingCurrentBar() { if (flag_SwapFriendAndRank == 0) StartCoroutine(RefreshFriendBar()); else if (flag_SwapFriendAndRank == 1) StartCoroutine(RefreshFriendRankBar()); } public void OnClick_ShowDetail() { AudioMgr.ins.PlayBtn(); if (flag_SwapFriendAndRank == 0) { ViewMgr.Instance.ShowView(); } else if (flag_SwapFriendAndRank == 1) { ViewMgr.Instance.ShowView(); } } //检测是否需要提示有人加我好友 bool _old_hasFriendRequest = false; int _old_flag_SwapFriendAndRank = 0; void UpdateCheckRenderFriendTip() { if (UserPlayer.ins == null) return; if ( UserPlayer.ins.tempData.hasFriendRequest == _old_hasFriendRequest && flag_SwapFriendAndRank == _old_flag_SwapFriendAndRank ) return; _old_hasFriendRequest = UserPlayer.ins.tempData.hasFriendRequest; _old_flag_SwapFriendAndRank = flag_SwapFriendAndRank; friendBarTipTop.SetActive( flag_SwapFriendAndRank == 1 && UserPlayer.ins.tempData.hasFriendRequest); friendBarTipBottom.SetActive( flag_SwapFriendAndRank == 0 && UserPlayer.ins.tempData.hasFriendRequest); } public void RenderBattery(int deviceID, int value) { Image img = betteryBar.GetComponent(); img.fillAmount = value / 100f; } public void RenderBattery2() { int value = BluetoothAim.ins.get2PBattery(); Image img = betteryBar2.GetComponent(); img.fillAmount = value / 100f; } public void UpdateParent() { TopBarButtonInfo topBarButtonInfo = topBarView.GetCurrentTopBarButtonInfo(); if (bettery != null) { bettery.transform.SetParent(topBarButtonInfo.container.transform); } if (bettery2 != null) { bettery2.transform.SetParent(topBarButtonInfo.container.transform); } } void updateBatteryStatus() { if (BluetoothAim.ins == null) return; //更新电池图标显示 if (BluetoothAim.ins.status == BluetoothStatusEnum.ConnectSuccess) { if (!bettery.activeSelf) { TopBarButtonInfo topBarButtonInfo = topBarView.GetCurrentTopBarButtonInfo(); bettery.transform.SetParent(topBarButtonInfo.container.transform); bettery.SetActive(true); } } else { if (bettery.activeSelf) { bettery.SetActive(false); } } //更新设备2电池图标显示 if (BluetoothAim.ins.getSmartBowHelper2P() != null && BluetoothAim.ins.getSmartBowHelper2P().GetBluetoothStatus() == SmartBowSDK.BluetoothStatusEnum.Connected) { if (!bettery2.activeSelf) { TopBarButtonInfo topBarButtonInfo = topBarView.GetCurrentTopBarButtonInfo(); bettery2.transform.SetParent(topBarButtonInfo.container.transform); bettery2.SetActive(true); } } else { if (bettery2.activeSelf) { bettery2.SetActive(false); } } } }