Monetization.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. namespace UnityEngine.Monetization
  3. {
  4. [Obsolete("Deprecated. Please use Advertisements", false)]
  5. public static class Monetization
  6. {
  7. public static event EventHandler<PlacementContentReadyEventArgs> onPlacementContentReady
  8. {
  9. add
  10. {
  11. s_Platform.OnPlacementContentReady += value;
  12. }
  13. remove
  14. {
  15. s_Platform.OnPlacementContentReady -= value;
  16. }
  17. }
  18. public static event EventHandler<PlacementContentStateChangeEventArgs> onPlacementContentStateChange
  19. {
  20. add
  21. {
  22. s_Platform.OnPlacementContentStateChange += value;
  23. }
  24. remove
  25. {
  26. s_Platform.OnPlacementContentStateChange -= value;
  27. }
  28. }
  29. static IMonetizationPlatform s_Platform;
  30. static bool s_Initialized;
  31. static internal IMonetizationPlatform platform
  32. {
  33. get { return s_Platform; }
  34. set { s_Platform = value; }
  35. }
  36. static Monetization()
  37. {
  38. s_Platform = Creator.CreatePlatform();
  39. }
  40. /// <summary>
  41. /// Returns the current Unity Monetization version.
  42. /// </summary>
  43. public static string version
  44. {
  45. get
  46. {
  47. return s_Platform.version;
  48. }
  49. }
  50. /// <summary>
  51. /// Returns whether the monetization system is initialized successfully.
  52. /// </summary>
  53. public static bool isInitialized
  54. {
  55. get
  56. {
  57. return s_Initialized;
  58. }
  59. internal set
  60. {
  61. s_Initialized = value;
  62. }
  63. }
  64. /// <summary>
  65. /// Returns if the current platform is supported by the monetization system.
  66. /// </summary>
  67. public static bool isSupported
  68. {
  69. get
  70. {
  71. bool supported = Application.isEditor ||
  72. (Application.platform == RuntimePlatform.Android && s_Platform.isSupported) ||
  73. (Application.platform == RuntimePlatform.IPhonePlayer && s_Platform.isSupported);
  74. return supported;
  75. }
  76. }
  77. public static bool IsReady(string placementId)
  78. {
  79. return s_Platform.IsReady(string.IsNullOrEmpty(placementId) ? null : placementId);
  80. }
  81. /// <summary>
  82. /// Initialize the monetization system with specified gameId, testMode, IPurchasingAdapter
  83. /// </summary>
  84. /// <param name="gameId">Game identifier.</param>
  85. /// <param name="testMode">Test mode.</param>
  86. public static void Initialize(string gameId, bool testMode)
  87. {
  88. if (!isInitialized)
  89. {
  90. isInitialized = true;
  91. var framework = new MetaData("framework");
  92. framework.Set("name", "Unity");
  93. framework.Set("version", Application.unityVersion);
  94. SetMetaData(framework);
  95. var adapter = new MetaData("adapter");
  96. #if ASSET_STORE
  97. adapter.Set("name", "AssetStore");
  98. #else
  99. adapter.Set("name", "Packman");
  100. #endif
  101. adapter.Set("version", version);
  102. adapter.Set("flavor", "monetization");
  103. SetMetaData(adapter);
  104. s_Platform.onError += OnError;
  105. s_Platform.Initialize(gameId, testMode);
  106. }
  107. }
  108. public static void SetPurchasingAdapter(IPurchasingAdapter adapter)
  109. {
  110. s_Platform.SetPurchasingAdapter(adapter);
  111. }
  112. public static PlacementContent GetPlacementContent(string placementId)
  113. {
  114. return s_Platform.GetPlacementContent(placementId);
  115. }
  116. public static INativePromoAdapter CreateNativePromoAdapter(PromoAdPlacementContent placementContent)
  117. {
  118. return s_Platform.CreateNativePromoAdapter(placementContent);
  119. }
  120. public static void SetMetaData(MetaData metaData)
  121. {
  122. s_Platform.SetMetaData(metaData);
  123. }
  124. private static void OnError(object sender, UnityServicesErrorEventArgs e)
  125. {
  126. Debug.LogErrorFormat("Unity Services recieved error {0} - {1}", e?.error, e?.message);
  127. }
  128. }
  129. }