PlayGamesLocalUser.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 || (UNITY_IPHONE && !NO_GPGS))
  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
  82. {
  83. return mPlatform.GetFriends();
  84. }
  85. }
  86. /// <summary>
  87. /// Gets an id token for the user.
  88. /// </summary>
  89. public string GetIdToken()
  90. {
  91. return mPlatform.GetIdToken();
  92. }
  93. /// <summary>
  94. /// Returns whether or not the local user is authenticated to Google Play Games.
  95. /// </summary>
  96. /// <returns>
  97. /// <c>true</c> if authenticated; otherwise, <c>false</c>.
  98. /// </returns>
  99. public bool authenticated
  100. {
  101. get
  102. {
  103. return mPlatform.IsAuthenticated();
  104. }
  105. }
  106. /// <summary>
  107. /// Not implemented. As safety placeholder, returns true.
  108. /// </summary>
  109. public bool underage
  110. {
  111. get
  112. {
  113. return true;
  114. }
  115. }
  116. /// <summary>
  117. /// Gets the display name of the user.
  118. /// </summary>
  119. /// <returns>
  120. /// The display name of the user.
  121. /// </returns>
  122. public new string userName
  123. {
  124. get
  125. {
  126. string retval = string.Empty;
  127. if (authenticated)
  128. {
  129. retval = mPlatform.GetUserDisplayName();
  130. if (!base.userName.Equals(retval))
  131. {
  132. ResetIdentity(retval, mPlatform.GetUserId(), mPlatform.GetUserImageUrl());
  133. }
  134. }
  135. return retval;
  136. }
  137. }
  138. /// <summary>
  139. /// Gets the user's Google id.
  140. /// </summary>
  141. /// <remarks> This id is persistent and uniquely identifies the user
  142. /// across all games that use Google Play Game Services. It is
  143. /// the preferred method of uniquely identifying a player instead
  144. /// of email address.
  145. /// </remarks>
  146. /// <returns>
  147. /// The user's Google id.
  148. /// </returns>
  149. public new string id
  150. {
  151. get
  152. {
  153. string retval = string.Empty;
  154. if (authenticated)
  155. {
  156. retval = mPlatform.GetUserId();
  157. if (!base.id.Equals(retval))
  158. {
  159. ResetIdentity(mPlatform.GetUserDisplayName(), retval, mPlatform.GetUserImageUrl());
  160. }
  161. }
  162. return retval;
  163. }
  164. }
  165. /// <summary>
  166. /// Returns true (since this is the local user).
  167. /// </summary>
  168. public new bool isFriend
  169. {
  170. get
  171. {
  172. return true;
  173. }
  174. }
  175. /// <summary>
  176. /// Gets the local user's state. This is always <c>UserState.Online</c> for
  177. /// the local user.
  178. /// </summary>
  179. public new UserState state
  180. {
  181. get
  182. {
  183. return UserState.Online;
  184. }
  185. }
  186. public new string AvatarURL
  187. {
  188. get
  189. {
  190. string retval = string.Empty;
  191. if (authenticated)
  192. {
  193. retval = mPlatform.GetUserImageUrl();
  194. if (!base.id.Equals(retval))
  195. {
  196. ResetIdentity(mPlatform.GetUserDisplayName(),
  197. mPlatform.GetUserId(), retval);
  198. }
  199. }
  200. return retval;
  201. }
  202. }
  203. /// <summary>Gets the email of the signed in player.</summary>
  204. /// <remarks>If your game requires a persistent, unique id for the
  205. /// player, the use of PlayerId is recommendend since it does not
  206. /// require extra permission consent from the user.
  207. /// This is only available if the Requires Google Plus option
  208. /// is added to the setup (which enables additional
  209. /// permissions for the application).
  210. /// NOTE: This property can only be accessed using the main Unity thread.
  211. /// </remarks>
  212. /// <value>The email.</value>
  213. public string Email
  214. {
  215. get
  216. {
  217. // treat null as unitialized, empty as no email. This can
  218. // happen when the web client is not initialized.
  219. if (authenticated && string.IsNullOrEmpty(emailAddress))
  220. {
  221. emailAddress = mPlatform.GetUserEmail();
  222. emailAddress = emailAddress ?? string.Empty;
  223. }
  224. return authenticated ? emailAddress : string.Empty;
  225. }
  226. }
  227. /// <summary>
  228. /// Gets the player's stats.
  229. /// </summary>
  230. /// <param name="callback">Callback when they are available.</param>
  231. public void GetStats(Action<CommonStatusCodes, PlayerStats> callback)
  232. {
  233. if (mStats == null || !mStats.Valid)
  234. {
  235. mPlatform.GetPlayerStats((rc, stats) =>
  236. {
  237. mStats = stats;
  238. callback(rc, stats);
  239. });
  240. }
  241. else
  242. {
  243. // 0 = success
  244. callback(CommonStatusCodes.Success, mStats);
  245. }
  246. }
  247. }
  248. }
  249. #endif