PlayGamesUserProfile.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 || (UNITY_IPHONE && !NO_GPGS))
  17. namespace GooglePlayGames
  18. {
  19. using System;
  20. using System.Collections;
  21. using GooglePlayGames.OurUtils;
  22. using UnityEngine;
  23. using UnityEngine.SocialPlatforms;
  24. /// <summary>
  25. /// Represents a Google Play Games user profile. In the current implementation,
  26. /// this is only used as a base class of <see cref="PlayGamesLocalUser" />
  27. /// and should not be used directly.
  28. /// </summary>
  29. public class PlayGamesUserProfile : IUserProfile
  30. {
  31. private string mDisplayName;
  32. private string mPlayerId;
  33. private string mAvatarUrl;
  34. private volatile bool mImageLoading = false;
  35. private Texture2D mImage;
  36. internal PlayGamesUserProfile(string displayName, string playerId,
  37. string avatarUrl)
  38. {
  39. mDisplayName = displayName;
  40. mPlayerId = playerId;
  41. mAvatarUrl = avatarUrl;
  42. mImageLoading = false;
  43. }
  44. protected void ResetIdentity(string displayName, string playerId,
  45. string avatarUrl)
  46. {
  47. mDisplayName = displayName;
  48. mPlayerId = playerId;
  49. if (mAvatarUrl != avatarUrl)
  50. {
  51. mImage = null;
  52. mAvatarUrl = avatarUrl;
  53. }
  54. mImageLoading = false;
  55. }
  56. #region IUserProfile implementation
  57. public string userName
  58. {
  59. get
  60. {
  61. return mDisplayName;
  62. }
  63. }
  64. public string id
  65. {
  66. get
  67. {
  68. return mPlayerId;
  69. }
  70. }
  71. public bool isFriend
  72. {
  73. get
  74. {
  75. return true;
  76. }
  77. }
  78. public UserState state
  79. {
  80. get
  81. {
  82. return UserState.Online;
  83. }
  84. }
  85. public Texture2D image
  86. {
  87. get
  88. {
  89. if (!mImageLoading && mImage == null && !string.IsNullOrEmpty(AvatarURL))
  90. {
  91. Debug.Log("Starting to load image: " + AvatarURL);
  92. mImageLoading = true;
  93. PlayGamesHelperObject.RunCoroutine(LoadImage());
  94. }
  95. return mImage;
  96. }
  97. }
  98. #endregion
  99. public string AvatarURL
  100. {
  101. get
  102. {
  103. return mAvatarUrl;
  104. }
  105. }
  106. /// <summary>
  107. /// Loads the local user's image from the url. Loading urls
  108. /// is asynchronous so the return from this call is fast,
  109. /// the image is returned once it is loaded. null is returned
  110. /// up to that point.
  111. /// </summary>
  112. internal IEnumerator LoadImage()
  113. {
  114. // the url can be null if the user does not have an
  115. // avatar configured.
  116. if (!string.IsNullOrEmpty(AvatarURL))
  117. {
  118. WWW www = new WWW(AvatarURL);
  119. while (!www.isDone)
  120. {
  121. yield return null;
  122. }
  123. if (www.error == null)
  124. {
  125. this.mImage = www.texture;
  126. }
  127. else
  128. {
  129. mImage = Texture2D.blackTexture;
  130. Debug.Log("Error downloading image: " + www.error);
  131. }
  132. mImageLoading = false;
  133. }
  134. else
  135. {
  136. Debug.Log("No URL found.");
  137. mImage = Texture2D.blackTexture;
  138. mImageLoading = false;
  139. }
  140. }
  141. public override bool Equals(object obj)
  142. {
  143. if (obj == null)
  144. {
  145. return false;
  146. }
  147. if (ReferenceEquals(this, obj))
  148. {
  149. return true;
  150. }
  151. PlayGamesUserProfile other = obj as PlayGamesUserProfile;
  152. if (other == null)
  153. {
  154. return false;
  155. }
  156. return StringComparer.Ordinal.Equals(mPlayerId, other.mPlayerId);
  157. }
  158. public override int GetHashCode()
  159. {
  160. return typeof(PlayGamesUserProfile).GetHashCode() ^ mPlayerId.GetHashCode();
  161. }
  162. public override string ToString()
  163. {
  164. return string.Format("[Player: '{0}' (id {1})]", mDisplayName, mPlayerId);
  165. }
  166. }
  167. }
  168. #endif