BoxFriendRequest.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. public class BoxFriendRequest : MonoBehaviour
  8. {
  9. void Awake()
  10. {
  11. GetFriendRequestItemPrefab().SetActive(false);
  12. }
  13. void OnEnable()
  14. {
  15. Refresh();
  16. }
  17. Transform GetFriendRequestItemParent()
  18. {
  19. return transform.Find("ScrollView/Viewport/Content");
  20. }
  21. GameObject GetFriendRequestItemPrefab()
  22. {
  23. return GetFriendRequestItemParent().Find("Item").gameObject;
  24. }
  25. //是否同意过好友添加请求,该记录可帮助提醒homeView的好友栏刷新
  26. bool _hasAcceptAnyFriends = false;
  27. void Refresh()
  28. {
  29. for (int i = 1; i < GetFriendRequestItemParent().childCount; i++)
  30. {
  31. Destroy(GetFriendRequestItemParent().GetChild(i).gameObject);
  32. }
  33. Action<JArray> cb = delegate (JArray list) {
  34. if (list.Count > 0)
  35. {
  36. foreach (var itemInfo in list)
  37. {
  38. int otherID = itemInfo.Value<int>("otherID");
  39. int avatarID = itemInfo.Value<int>("avatarID");
  40. string avatarUrl = itemInfo.Value<string>("avatarUrl");
  41. string nickname = itemInfo.Value<string>("nickname");
  42. bool online = itemInfo.Value<bool>("online");
  43. long offlineTime = itemInfo.Value<long>("offlineTime");
  44. GameObject o = Instantiate(GetFriendRequestItemPrefab(), GetFriendRequestItemParent());
  45. o.SetActive(true);
  46. o.name = otherID.ToString();
  47. RoleMgr.SetAvatarToImage(
  48. o.transform.Find("Avatar/Sprite").GetComponent<Image>(),
  49. avatarID, avatarUrl);
  50. o.transform.Find("Name").GetComponent<Text>().text = nickname;
  51. Button btnNo = o.transform.Find("BtnIgnore").GetComponent<Button>();
  52. btnNo.onClick.RemoveAllListeners();
  53. btnNo.onClick.AddListener(delegate () {
  54. AudioMgr.ins.PlayBtn();
  55. UserPlayer.ins.call("friendComp.decideAddFriend", new object[] { false, otherID });
  56. Destroy(o);
  57. UserPlayer.ins.tempData.hasFriendRequest = GetFriendRequestItemParent().childCount - 1 > 1;
  58. });
  59. Button btnYes = o.transform.Find("BtnAgree").GetComponent<Button>();
  60. btnYes.onClick.RemoveAllListeners();
  61. btnYes.onClick.AddListener(delegate () {
  62. AudioMgr.ins.PlayBtn();
  63. UserPlayer.ins.call("friendComp.decideAddFriend", new object[] { true, otherID });
  64. Destroy(o);
  65. _hasAcceptAnyFriends = true;
  66. UserPlayer.ins.tempData.hasFriendRequest = GetFriendRequestItemParent().childCount - 1 > 1;
  67. });
  68. }
  69. }
  70. else
  71. {
  72. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("好友请求列表为空"));
  73. }
  74. };
  75. UserPlayer.ins.call("friendComp.getFriendRequestList", null, cb);
  76. PopupMgr.ins.ClearAllTip();
  77. }
  78. }