Advertisement
Guest User

Untitled

a guest
Jan 16th, 2021
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. using System;
  2. using TMPro;
  3. using Photon.Realtime;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using Photon.Chat;
  7.  
  8. namespace KnoxGameStudios
  9. {
  10. public class UIFriend : MonoBehaviour
  11. {
  12. [SerializeField] private TMP_Text friendNameText;
  13. [SerializeField] private string friendName;
  14. [SerializeField] private bool isOnline;
  15. [SerializeField] private Image onlineImage;
  16. [SerializeField] private GameObject inviteButton;
  17. [SerializeField] private Color onlineColor;
  18. [SerializeField] private Color offlineColor;
  19. [SerializeField] public RawImage friendAvatar;
  20. [SerializeField] public string friendAvatarUrl;
  21.  
  22. public static Action<string> OnRemoveFriend = delegate { };
  23. public static Action<string> OnInviteFriend = delegate { };
  24. public static Action<string> OnGetCurrentStatus = delegate { };
  25. public static Action OnGetRoomStatus = delegate { };
  26.  
  27. private void Awake()
  28. {
  29. PhotonChatController.OnStatusUpdated += HandleStatusUpdated;
  30. PhotonChatFriendController.OnStatusUpdated += HandleStatusUpdated;
  31. PhotonRoomController.OnRoomStatusChange += HandleInRoom;
  32. }
  33. private void OnDestroy()
  34. {
  35. PhotonChatController.OnStatusUpdated -= HandleStatusUpdated;
  36. PhotonChatFriendController.OnStatusUpdated -= HandleStatusUpdated;
  37. PhotonRoomController.OnRoomStatusChange -= HandleInRoom;
  38. }
  39.  
  40. private void OnEnable()
  41. {
  42. if (string.IsNullOrEmpty(friendName)) return;
  43. OnGetCurrentStatus?.Invoke(friendName);
  44. OnGetRoomStatus?.Invoke();
  45. }
  46.  
  47. public void Initialize(FriendInfo friend)
  48. {
  49. Debug.Log($"{friend.UserId} is online: {friend.IsOnline} ; in room: {friend.IsInRoom} ; room name: {friend.Room}");
  50.  
  51. SetupUI();
  52. }
  53. public void Initialize(string friendName)
  54. {
  55. Debug.Log($"{friendName} is added");
  56. this.friendName = friendName;
  57.  
  58. SetupUI();
  59. OnGetCurrentStatus?.Invoke(friendName);
  60. OnGetRoomStatus?.Invoke();
  61. }
  62.  
  63.  
  64. private void HandleStatusUpdated(PhotonStatus status)
  65. {
  66. if (string.Compare(friendName, status.PlayerName) == 0)
  67. {
  68. Debug.Log($"Updating status in UI for {status.PlayerName} to status {status.Status}");
  69. SetStatus(status.Status);
  70. }
  71. }
  72.  
  73. private void HandleInRoom(bool inRoom)
  74. {
  75. Debug.Log($"Updating invite ui to {inRoom}");
  76. inviteButton.SetActive(inRoom && isOnline);
  77. }
  78.  
  79. private void SetupUI()
  80. {
  81. friendNameText.SetText(friendName);
  82. inviteButton.SetActive(false);
  83. }
  84.  
  85. private void SetStatus(int status)
  86. {
  87. if (status == ChatUserStatus.Online)
  88. {
  89. onlineImage.color = onlineColor;
  90. isOnline = true;
  91. OnGetRoomStatus?.Invoke();
  92.  
  93. }
  94. else
  95. {
  96. onlineImage.color = offlineColor;
  97. isOnline = false;
  98. inviteButton.SetActive(false);
  99. }
  100. }
  101.  
  102. public void RemoveFriend()
  103. {
  104. Debug.Log($"Clicked to remove friend {friendName}");
  105. OnRemoveFriend?.Invoke(friendName);
  106. }
  107.  
  108. public void InviteFriend()
  109. {
  110. Debug.Log($"Clicked to invite friend {friendName}");
  111. OnInviteFriend?.Invoke(friendName);
  112. }
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement