BoxFriendList.cs 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using System;
  6. using Newtonsoft.Json.Linq;
  7. namespace SmartBow {
  8. public class BoxFriendList : MonoBehaviour
  9. {
  10. void Awake()
  11. {
  12. GetMyFriendItemPrefab().SetActive(false);
  13. }
  14. void OnEnable()
  15. {
  16. Refresh();
  17. }
  18. Transform GetMyFriendItemParent()
  19. {
  20. return transform.Find("ScrollView/Viewport/Content");
  21. }
  22. GameObject GetMyFriendItemPrefab()
  23. {
  24. return GetMyFriendItemParent().Find("Item").gameObject;
  25. }
  26. //是否删除过好友,该记录可帮助提醒homeView的好友栏刷新
  27. bool _hasDeleteAnyFriends = false;
  28. void Refresh()
  29. {
  30. for (int i = 1; i < GetMyFriendItemParent().childCount; i++)
  31. {
  32. Destroy(GetMyFriendItemParent().GetChild(i).gameObject);
  33. }
  34. Action<JArray> cb = delegate (JArray list) {
  35. if (list.Count > 0)
  36. {
  37. foreach (var itemInfo in list)
  38. {
  39. int friendID = itemInfo.Value<int>("friendID");
  40. int avatarID = itemInfo.Value<int>("avatarID");
  41. string avatarUrl = itemInfo.Value<string>("avatarUrl");
  42. string nickname = itemInfo.Value<string>("nickname");
  43. bool online = itemInfo.Value<bool>("online");
  44. long offlineTime = itemInfo.Value<long>("offlineTime");
  45. GameObject o = Instantiate(GetMyFriendItemPrefab(), GetMyFriendItemParent());
  46. o.SetActive(true);
  47. o.name = friendID.ToString();
  48. RoleMgr.SetAvatarToImage(
  49. o.transform.Find("Avatar/Sprite").GetComponent<Image>(),
  50. avatarID, avatarUrl);
  51. o.transform.Find("Name").GetComponent<Text>().text = nickname;
  52. //在线提示
  53. Text onlineTip = o.transform.Find("OnlineTip").GetComponent<Text>();
  54. GameObject onlinePoint = onlineTip.transform.Find("PointOnline").gameObject;
  55. if (online)
  56. {
  57. onlineTip.enabled = false;
  58. onlinePoint.SetActive(true);
  59. }
  60. else
  61. {
  62. onlineTip.enabled = true;
  63. onlineTip.text = TimeUtil.GetOfflineTimeStr(offlineTime, online);
  64. onlinePoint.SetActive(false);
  65. }
  66. //删除按钮
  67. Button btnDelete = o.transform.Find("BtnDelete").GetComponent<Button>();
  68. btnDelete.onClick.RemoveAllListeners();
  69. btnDelete.onClick.AddListener(delegate () {
  70. AudioMgr.ins.PlayBtn();
  71. ModalConfirmDeleteFriend modalConfirmDeleteFriend = GetComponentInParent<SocialView>().ShowModalConfirmDeleteFriend(true);
  72. modalConfirmDeleteFriend.OnConfirm = () =>
  73. {
  74. Action<bool> cb = delegate (bool res)
  75. {
  76. if (res)
  77. {
  78. _hasDeleteAnyFriends = true;
  79. Destroy(o);
  80. }
  81. };
  82. UserPlayer.ins.call("friendComp.deleteMyFriend", new object[] { friendID }, cb);
  83. };
  84. });
  85. }
  86. }
  87. else
  88. {
  89. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("好友列表为空"));
  90. }
  91. HomeView.ins?.RenderFriendList(list);
  92. };
  93. UserPlayer.ins.call("friendComp.getMyFriends", null, cb);
  94. PopupMgr.ins.ClearAllTip();
  95. }
  96. }
  97. }