using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using Newtonsoft.Json; using Newtonsoft.Json.Linq; /* 界面-好友 */ public class FriendView : MonoBehaviour { [SerializeField] GameObject myFriendBox; [SerializeField] GameObject friendRequestBox; [SerializeField] GameObject friendRecommendBox; [SerializeField] GameObject searchPlayerBox; void Awake() { InitBtnTabs(); InitMyFriendBox(); InitFriendRequestBox(); InitFriendRecommendBox(); InitSearchPlayerBox(); } void Start() { if (UserPlayer.ins != null) { UserPlayer.ins.tempData.onUpdate += onUserPlayerTempDataUpdate; onUserPlayerTempDataUpdate(); } SetBtnTabSelected(btnTabs[0]); } void OnDestroy() { if (UserPlayer.ins != null) { UserPlayer.ins.tempData.onUpdate -= onUserPlayerTempDataUpdate; } } void onUserPlayerTempDataUpdate() { try { var data = UserPlayer.ins.tempData; btnTabs[1].transform.Find("FriendTip").gameObject.SetActive(data.hasFriendRequest); } catch (System.Exception e) { Debug.LogError(e.Message); Debug.LogError(e.StackTrace); } } #region myFriend void InitMyFriendBox() { myFriendBox.SetActive(false); GetMyFriendItemPrefab().SetActive(false); } Transform GetMyFriendItemParent() { return myFriendBox.transform.Find("ScrollView/Viewport/Content"); } GameObject GetMyFriendItemPrefab() { return GetMyFriendItemParent().Find("Item").gameObject; } void EnterMyFriendBox() { myFriendBox.SetActive(true); for (int i = 1; i < GetMyFriendItemParent().childCount; i++) { Destroy(GetMyFriendItemParent().GetChild(i).gameObject); } Action cb = delegate(JArray list) { if (list.Count > 0) { foreach (var itemInfo in list) { int friendID = itemInfo.Value("friendID"); int avatarID = itemInfo.Value("avatarID"); string nickname = itemInfo.Value("nickname"); bool online = itemInfo.Value("online"); long offlineTime = itemInfo.Value("offlineTime"); GameObject o = GameObject.Instantiate(GetMyFriendItemPrefab(), GetMyFriendItemParent()); o.SetActive(true); o.name = friendID.ToString(); o.transform.Find("Avatar/Sprite").GetComponent().sprite = RoleMgr.GetAvatar(avatarID); o.transform.Find("Name").GetComponent().text = nickname; o.transform.Find("BG0").gameObject.SetActive(!online); o.transform.Find("BG1").gameObject.SetActive(online); Text onlineTip = o.transform.Find("OnlineTip").GetComponent(); onlineTip.text = TimeUtil.GetOfflineTimeStr(offlineTime, online); onlineTip.color = online ? Color.green : Color.gray; Button btnDelete = o.transform.Find("BtnDelete").GetComponent