PlayGamesAchievement.cs 9.2 KB

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