PlayGamesUserProfile.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // <copyright file="PlayGamesUserProfile.cs" company="Google Inc.">
  2. // Copyright (C) 2014 Google Inc. All Rights Reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. // </copyright>
  16. #if UNITY_ANDROID
  17. namespace GooglePlayGames
  18. {
  19. using System;
  20. using System.Collections;
  21. using GooglePlayGames.OurUtils;
  22. using UnityEngine;
  23. #if UNITY_2017_2_OR_NEWER
  24. using UnityEngine.Networking;
  25. #endif
  26. using UnityEngine.SocialPlatforms;
  27. /// <summary>
  28. /// Represents a Google Play Games user profile. In the current implementation,
  29. /// this is only used as a base class of <see cref="PlayGamesLocalUser" />
  30. /// and should not be used directly.
  31. /// </summary>
  32. public class PlayGamesUserProfile : IUserProfile
  33. {
  34. private string mDisplayName;
  35. private string mPlayerId;
  36. private string mAvatarUrl;
  37. private bool mIsFriend;
  38. private volatile bool mImageLoading = false;
  39. private Texture2D mImage;
  40. internal PlayGamesUserProfile(string displayName, string playerId,
  41. string avatarUrl)
  42. {
  43. mDisplayName = displayName;
  44. mPlayerId = playerId;
  45. setAvatarUrl(avatarUrl);
  46. mImageLoading = false;
  47. mIsFriend = false;
  48. }
  49. internal PlayGamesUserProfile(string displayName, string playerId, string avatarUrl,
  50. bool isFriend)
  51. {
  52. mDisplayName = displayName;
  53. mPlayerId = playerId;
  54. mAvatarUrl = avatarUrl;
  55. mImageLoading = false;
  56. mIsFriend = isFriend;
  57. }
  58. protected void ResetIdentity(string displayName, string playerId,
  59. string avatarUrl)
  60. {
  61. mDisplayName = displayName;
  62. mPlayerId = playerId;
  63. mIsFriend = false;
  64. if (mAvatarUrl != avatarUrl)
  65. {
  66. mImage = null;
  67. setAvatarUrl(avatarUrl);
  68. }
  69. mImageLoading = false;
  70. }
  71. #region IUserProfile implementation
  72. public string userName
  73. {
  74. get { return mDisplayName; }
  75. }
  76. public string id
  77. {
  78. get { return mPlayerId; }
  79. }
  80. public string gameId
  81. {
  82. get { return mPlayerId; }
  83. }
  84. public bool isFriend
  85. {
  86. get { return mIsFriend; }
  87. }
  88. public UserState state
  89. {
  90. get { return UserState.Online; }
  91. }
  92. public Texture2D image
  93. {
  94. get
  95. {
  96. if (!mImageLoading && mImage == null && !string.IsNullOrEmpty(AvatarURL))
  97. {
  98. OurUtils.Logger.d("Starting to load image: " + AvatarURL);
  99. mImageLoading = true;
  100. PlayGamesHelperObject.RunCoroutine(LoadImage());
  101. }
  102. return mImage;
  103. }
  104. }
  105. #endregion
  106. public string AvatarURL
  107. {
  108. get { return mAvatarUrl; }
  109. }
  110. /// <summary>
  111. /// Loads the local user's image from the url. Loading urls
  112. /// is asynchronous so the return from this call is fast,
  113. /// the image is returned once it is loaded. null is returned
  114. /// up to that point.
  115. /// </summary>
  116. internal IEnumerator LoadImage()
  117. {
  118. // the url can be null if the user does not have an
  119. // avatar configured.
  120. if (!string.IsNullOrEmpty(AvatarURL))
  121. {
  122. #if UNITY_2017_2_OR_NEWER
  123. UnityWebRequest www = UnityWebRequestTexture.GetTexture(AvatarURL);
  124. www.SendWebRequest();
  125. #else
  126. WWW www = new WWW(AvatarURL);
  127. #endif
  128. while (!www.isDone)
  129. {
  130. yield return null;
  131. }
  132. if (www.error == null)
  133. {
  134. #if UNITY_2017_2_OR_NEWER
  135. this.mImage = DownloadHandlerTexture.GetContent(www);
  136. #else
  137. this.mImage = www.texture;
  138. #endif
  139. }
  140. else
  141. {
  142. mImage = Texture2D.blackTexture;
  143. OurUtils.Logger.e("Error downloading image: " + www.error);
  144. }
  145. mImageLoading = false;
  146. }
  147. else
  148. {
  149. OurUtils.Logger.e("No URL found.");
  150. mImage = Texture2D.blackTexture;
  151. mImageLoading = false;
  152. }
  153. }
  154. public override bool Equals(object obj)
  155. {
  156. if (obj == null)
  157. {
  158. return false;
  159. }
  160. if (ReferenceEquals(this, obj))
  161. {
  162. return true;
  163. }
  164. PlayGamesUserProfile other = obj as PlayGamesUserProfile;
  165. if (other == null)
  166. {
  167. return false;
  168. }
  169. return StringComparer.Ordinal.Equals(mPlayerId, other.mPlayerId);
  170. }
  171. public override int GetHashCode()
  172. {
  173. return typeof(PlayGamesUserProfile).GetHashCode() ^ mPlayerId.GetHashCode();
  174. }
  175. public override string ToString()
  176. {
  177. return string.Format("[Player: '{0}' (id {1})]", mDisplayName, mPlayerId);
  178. }
  179. private void setAvatarUrl(string avatarUrl)
  180. {
  181. mAvatarUrl = avatarUrl;
  182. if (!avatarUrl.StartsWith("https") && avatarUrl.StartsWith("http"))
  183. {
  184. mAvatarUrl = avatarUrl.Insert(4, "s");
  185. }
  186. }
  187. }
  188. }
  189. #endif