PlayGamesAchievement.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // <copyright file="PlayGamesAchievement.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 GooglePlayGames.BasicApi;
  21. using UnityEngine;
  22. #if UNITY_2017_1_OR_NEWER
  23. using UnityEngine.Networking;
  24. #endif
  25. using UnityEngine.SocialPlatforms;
  26. internal delegate void ReportProgress(string id, double progress, Action<bool> callback);
  27. /// <summary>
  28. /// Represents a Google Play Games achievement. It can be used to report an achievement
  29. /// to the API, offering identical functionality as <see cref="PlayGamesPlatform.ReportProgress" />.
  30. /// </summary>
  31. internal class PlayGamesAchievement : IAchievement, IAchievementDescription
  32. {
  33. private readonly ReportProgress mProgressCallback;
  34. private string mId = string.Empty;
  35. private bool mIsIncremental = false;
  36. private int mCurrentSteps = 0;
  37. private int mTotalSteps = 0;
  38. private double mPercentComplete = 0.0;
  39. private bool mCompleted = false;
  40. private bool mHidden = false;
  41. private DateTime mLastModifiedTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
  42. private string mTitle = string.Empty;
  43. private string mRevealedImageUrl = string.Empty;
  44. private string mUnlockedImageUrl = string.Empty;
  45. #if UNITY_2017_1_OR_NEWER
  46. private UnityWebRequest mImageFetcher = null;
  47. #else
  48. private WWW mImageFetcher = null;
  49. #endif
  50. private Texture2D mImage = null;
  51. private string mDescription = string.Empty;
  52. private ulong mPoints = 0;
  53. internal PlayGamesAchievement()
  54. : this(PlayGamesPlatform.Instance.ReportProgress)
  55. {
  56. }
  57. internal PlayGamesAchievement(ReportProgress progressCallback)
  58. {
  59. mProgressCallback = progressCallback;
  60. }
  61. internal PlayGamesAchievement(Achievement ach) : this()
  62. {
  63. this.mId = ach.Id;
  64. this.mIsIncremental = ach.IsIncremental;
  65. this.mCurrentSteps = ach.CurrentSteps;
  66. this.mTotalSteps = ach.TotalSteps;
  67. if (ach.IsIncremental)
  68. {
  69. if (ach.TotalSteps > 0)
  70. {
  71. this.mPercentComplete =
  72. ((double) ach.CurrentSteps / (double) ach.TotalSteps) * 100.0;
  73. }
  74. else
  75. {
  76. this.mPercentComplete = 0.0;
  77. }
  78. }
  79. else
  80. {
  81. this.mPercentComplete = ach.IsUnlocked ? 100.0 : 0.0;
  82. }
  83. this.mCompleted = ach.IsUnlocked;
  84. this.mHidden = !ach.IsRevealed;
  85. this.mLastModifiedTime = ach.LastModifiedTime;
  86. this.mTitle = ach.Name;
  87. this.mDescription = ach.Description;
  88. this.mPoints = ach.Points;
  89. this.mRevealedImageUrl = ach.RevealedImageUrl;
  90. this.mUnlockedImageUrl = ach.UnlockedImageUrl;
  91. }
  92. /// <summary>
  93. /// Reveals, unlocks or increment achievement.
  94. /// </summary>
  95. /// <remarks>
  96. /// Call after setting <see cref="id" />, <see cref="completed" />,
  97. /// as well as <see cref="currentSteps" /> and <see cref="totalSteps" />
  98. /// for incremental achievements. Equivalent to calling
  99. /// <see cref="PlayGamesPlatform.ReportProgress" />.
  100. /// </remarks>
  101. public void ReportProgress(Action<bool> callback)
  102. {
  103. mProgressCallback.Invoke(mId, mPercentComplete, callback);
  104. }
  105. /// <summary>
  106. /// Loads the local user's image from the url. Loading urls
  107. /// is asynchronous so the return from this call is fast,
  108. /// the image is returned once it is loaded. null is returned
  109. /// up to that point.
  110. /// </summary>
  111. private Texture2D LoadImage()
  112. {
  113. if (hidden)
  114. {
  115. // return null, we dont have images for hidden achievements.
  116. return null;
  117. }
  118. string url = completed ? mUnlockedImageUrl : mRevealedImageUrl;
  119. // the url can be null if the image is not configured.
  120. if (!string.IsNullOrEmpty(url))
  121. {
  122. if (mImageFetcher == null || mImageFetcher.url != url)
  123. {
  124. #if UNITY_2017_1_OR_NEWER
  125. mImageFetcher = UnityWebRequestTexture.GetTexture(url);
  126. #else
  127. mImageFetcher = new WWW(url);
  128. #endif
  129. mImage = null;
  130. }
  131. // if we have the texture, just return, this avoids excessive
  132. // memory usage calling www.texture repeatedly.
  133. if (mImage != null)
  134. {
  135. return mImage;
  136. }
  137. if (mImageFetcher.isDone)
  138. {
  139. #if UNITY_2017_1_OR_NEWER
  140. mImage = DownloadHandlerTexture.GetContent(mImageFetcher);
  141. #else
  142. mImage = mImageFetcher.texture;
  143. #endif
  144. return mImage;
  145. }
  146. }
  147. // if there is no url, always return null.
  148. return null;
  149. }
  150. /// <summary>
  151. /// Gets or sets the id of this achievement.
  152. /// </summary>
  153. /// <returns>
  154. /// The identifier.
  155. /// </returns>
  156. public string id
  157. {
  158. get { return mId; }
  159. set { mId = value; }
  160. }
  161. /// <summary>
  162. /// Gets a value indicating whether this achievement is incremental.
  163. /// </summary>
  164. /// <remarks>
  165. /// This value is only set by PlayGamesPlatform.LoadAchievements
  166. /// </remarks>
  167. /// <returns><c>true</c> if incremental; otherwise, <c>false</c>.</returns>
  168. public bool isIncremental
  169. {
  170. get { return mIsIncremental; }
  171. }
  172. /// <summary>
  173. /// Gets the current steps completed of this achievement.
  174. /// </summary>
  175. /// <remarks>
  176. /// Undefined for standard (i.e. non-incremental) achievements.
  177. /// This value is only set by PlayGamesPlatform.LoadAchievements, changing the
  178. /// percentComplete will not affect this.
  179. /// </remarks>
  180. /// <returns>The current steps.</returns>
  181. public int currentSteps
  182. {
  183. get { return mCurrentSteps; }
  184. }
  185. /// <summary>
  186. /// Gets the total steps of this achievement.
  187. /// </summary>
  188. /// <remarks>
  189. /// Undefined for standard (i.e. non-incremental) achievements.
  190. /// This value is only set by PlayGamesPlatform.LoadAchievements, changing the
  191. /// percentComplete will not affect this.
  192. /// </remarks>
  193. /// <returns>The total steps.</returns>
  194. public int totalSteps
  195. {
  196. get { return mTotalSteps; }
  197. }
  198. /// <summary>
  199. /// Gets or sets the percent completed.
  200. /// </summary>
  201. /// <returns>
  202. /// The percent completed.
  203. /// </returns>
  204. public double percentCompleted
  205. {
  206. get { return mPercentComplete; }
  207. set { mPercentComplete = value; }
  208. }
  209. /// <summary>
  210. /// Gets a value indicating whether this achievement is completed.
  211. /// </summary>
  212. /// <remarks>
  213. /// This value is only set by PlayGamesPlatform.LoadAchievements, changing the
  214. /// percentComplete will not affect this.
  215. /// </remarks>
  216. /// <returns><c>true</c> if completed; otherwise, <c>false</c>.</returns>
  217. public bool completed
  218. {
  219. get { return this.mCompleted; }
  220. }
  221. /// <summary>
  222. /// Gets a value indicating whether this achievement is hidden.
  223. /// </summary>
  224. /// <value><c>true</c> if hidden; otherwise, <c>false</c>.</value>
  225. public bool hidden
  226. {
  227. get { return this.mHidden; }
  228. }
  229. public DateTime lastReportedDate
  230. {
  231. get { return mLastModifiedTime; }
  232. }
  233. public String title
  234. {
  235. get { return mTitle; }
  236. }
  237. public Texture2D image
  238. {
  239. get { return LoadImage(); }
  240. }
  241. public string achievedDescription
  242. {
  243. get { return mDescription; }
  244. }
  245. public string unachievedDescription
  246. {
  247. get { return mDescription; }
  248. }
  249. public int points
  250. {
  251. get { return (int) mPoints; }
  252. }
  253. }
  254. }
  255. #endif