PlayGamesLocalUser.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // <copyright file="PlayGamesLocalUser.cs" company="Google Inc.">
  2. // Copyright (C) 2014 Google Inc.
  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 GooglePlayGames.BasicApi;
  21. using UnityEngine.SocialPlatforms;
  22. /// <summary>
  23. /// Represents the Google Play Games local user.
  24. /// </summary>
  25. public class PlayGamesLocalUser : PlayGamesUserProfile, ILocalUser
  26. {
  27. internal PlayGamesPlatform mPlatform;
  28. private string emailAddress;
  29. private PlayerStats mStats;
  30. internal PlayGamesLocalUser(PlayGamesPlatform plaf)
  31. : base("localUser", string.Empty, string.Empty)
  32. {
  33. mPlatform = plaf;
  34. emailAddress = null;
  35. mStats = null;
  36. }
  37. /// <summary>
  38. /// Authenticates the local user. Equivalent to calling
  39. /// <see cref="PlayGamesPlatform.Authenticate" />.
  40. /// </summary>
  41. public void Authenticate(Action<bool> callback)
  42. {
  43. mPlatform.Authenticate(callback);
  44. }
  45. /// <summary>
  46. /// Authenticates the local user. Equivalent to calling
  47. /// <see cref="PlayGamesPlatform.Authenticate" />.
  48. /// </summary>
  49. public void Authenticate(Action<bool, string> callback)
  50. {
  51. mPlatform.Authenticate(callback);
  52. }
  53. /// <summary>
  54. /// Authenticates the local user. Equivalent to calling
  55. /// <see cref="PlayGamesPlatform.Authenticate" />.
  56. /// </summary>
  57. public void Authenticate(Action<bool> callback, bool silent)
  58. {
  59. mPlatform.Authenticate(callback, silent);
  60. }
  61. /// <summary>
  62. /// Authenticates the local user. Equivalent to calling
  63. /// <see cref="PlayGamesPlatform.Authenticate" />.
  64. /// </summary>
  65. public void Authenticate(Action<bool, string> callback, bool silent)
  66. {
  67. mPlatform.Authenticate(callback, silent);
  68. }
  69. /// <summary>
  70. /// Loads all friends of the authenticated user.
  71. /// </summary>
  72. public void LoadFriends(Action<bool> callback)
  73. {
  74. mPlatform.LoadFriends(this, callback);
  75. }
  76. /// <summary>
  77. /// Synchronous version of friends, returns null until loaded.
  78. /// </summary>
  79. public IUserProfile[] friends
  80. {
  81. get { return mPlatform.GetFriends(); }
  82. }
  83. /// <summary>
  84. /// Gets an id token for the user.
  85. /// </summary>
  86. public string GetIdToken()
  87. {
  88. return mPlatform.GetIdToken();
  89. }
  90. /// <summary>
  91. /// Returns whether or not the local user is authenticated to Google Play Games.
  92. /// </summary>
  93. /// <returns>
  94. /// <c>true</c> if authenticated; otherwise, <c>false</c>.
  95. /// </returns>
  96. public bool authenticated
  97. {
  98. get { return mPlatform.IsAuthenticated(); }
  99. }
  100. /// <summary>
  101. /// Not implemented. As safety placeholder, returns true.
  102. /// </summary>
  103. public bool underage
  104. {
  105. get { return true; }
  106. }
  107. /// <summary>
  108. /// Gets the display name of the user.
  109. /// </summary>
  110. /// <returns>
  111. /// The display name of the user.
  112. /// </returns>
  113. public new string userName
  114. {
  115. get
  116. {
  117. string retval = string.Empty;
  118. if (authenticated)
  119. {
  120. retval = mPlatform.GetUserDisplayName();
  121. if (!base.userName.Equals(retval))
  122. {
  123. ResetIdentity(retval, mPlatform.GetUserId(), mPlatform.GetUserImageUrl());
  124. }
  125. }
  126. return retval;
  127. }
  128. }
  129. /// <summary>
  130. /// Gets the user's Google id.
  131. /// </summary>
  132. /// <remarks> This id is persistent and uniquely identifies the user
  133. /// across all games that use Google Play Game Services. It is
  134. /// the preferred method of uniquely identifying a player instead
  135. /// of email address.
  136. /// </remarks>
  137. /// <returns>
  138. /// The user's Google id.
  139. /// </returns>
  140. public new string id
  141. {
  142. get
  143. {
  144. string retval = string.Empty;
  145. if (authenticated)
  146. {
  147. retval = mPlatform.GetUserId();
  148. if (!base.id.Equals(retval))
  149. {
  150. ResetIdentity(mPlatform.GetUserDisplayName(), retval, mPlatform.GetUserImageUrl());
  151. }
  152. }
  153. return retval;
  154. }
  155. }
  156. /// <summary>
  157. /// Returns true (since this is the local user).
  158. /// </summary>
  159. public new bool isFriend
  160. {
  161. get { return true; }
  162. }
  163. /// <summary>
  164. /// Gets the local user's state. This is always <c>UserState.Online</c> for
  165. /// the local user.
  166. /// </summary>
  167. public new UserState state
  168. {
  169. get { return UserState.Online; }
  170. }
  171. public new string AvatarURL
  172. {
  173. get
  174. {
  175. string retval = string.Empty;
  176. if (authenticated)
  177. {
  178. retval = mPlatform.GetUserImageUrl();
  179. if (!base.id.Equals(retval))
  180. {
  181. ResetIdentity(mPlatform.GetUserDisplayName(),
  182. mPlatform.GetUserId(), retval);
  183. }
  184. }
  185. return retval;
  186. }
  187. }
  188. /// <summary>Gets the email of the signed in player.</summary>
  189. /// <remarks>If your game requires a persistent, unique id for the
  190. /// player, the use of PlayerId is recommendend since it does not
  191. /// require extra permission consent from the user.
  192. /// This is only available if the Requires Google Plus option
  193. /// is added to the setup (which enables additional
  194. /// permissions for the application).
  195. /// NOTE: This property can only be accessed using the main Unity thread.
  196. /// </remarks>
  197. /// <value>The email.</value>
  198. public string Email
  199. {
  200. get
  201. {
  202. // treat null as unitialized, empty as no email. This can
  203. // happen when the web client is not initialized.
  204. if (authenticated && string.IsNullOrEmpty(emailAddress))
  205. {
  206. emailAddress = mPlatform.GetUserEmail();
  207. emailAddress = emailAddress ?? string.Empty;
  208. }
  209. return authenticated ? emailAddress : string.Empty;
  210. }
  211. }
  212. /// <summary>
  213. /// Gets the player's stats.
  214. /// </summary>
  215. /// <param name="callback">Callback when they are available.</param>
  216. public void GetStats(Action<CommonStatusCodes, PlayerStats> callback)
  217. {
  218. if (mStats == null || !mStats.Valid)
  219. {
  220. mPlatform.GetPlayerStats((rc, stats) =>
  221. {
  222. mStats = stats;
  223. callback(rc, stats);
  224. });
  225. }
  226. else
  227. {
  228. // 0 = success
  229. callback(CommonStatusCodes.Success, mStats);
  230. }
  231. }
  232. }
  233. }
  234. #endif