Ingen beskrivning
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UnityPurchasing.cs 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System;
  2. using System.Collections.Generic;
  3. #if IAP_ANALYTICS_SERVICE_ENABLED || IAP_ANALYTICS_SERVICE_ENABLED_WITH_SERVICE_COMPONENT
  4. using Unity.Services.Analytics;
  5. using Unity.Services.Core;
  6. #endif
  7. using UnityEngine.Purchasing.Extension;
  8. namespace UnityEngine.Purchasing
  9. {
  10. /// <summary>
  11. /// The core abstract implementation for Unity Purchasing.
  12. /// </summary>
  13. public abstract class UnityPurchasing
  14. {
  15. /// <summary>
  16. /// The main initialization call for Unity Purchasing.
  17. /// </summary>
  18. /// <param name="listener"> The <c>IStoreListener</c> to receive callbacks for future transactions </param>
  19. /// <param name="builder"> The <c>ConfigurationBuilder</c> containing the product definitions mapped to stores </param>
  20. [Obsolete("Use Initialize(IDetailedStoreListener, ConfigurationBuilder)", false)]
  21. public static void Initialize(IStoreListener listener, ConfigurationBuilder builder)
  22. {
  23. var logger = Debug.unityLogger;
  24. var unityServicesInitializationChecker = new UnityServicesInitializationChecker(logger);
  25. var legacyAnalyticsWrapper = new LegacyAnalyticsWrapper(GenerateLegacyUnityAnalytics(), new EmptyAnalyticsAdapter());
  26. Initialize(listener, builder, logger, Application.persistentDataPath,
  27. GenerateUnityAnalytics(logger), legacyAnalyticsWrapper, builder.factory.GetCatalogProvider(),
  28. unityServicesInitializationChecker);
  29. }
  30. /// <summary>
  31. /// The main initialization call for Unity Purchasing.
  32. /// </summary>
  33. /// <param name="listener"> The <c>IDetailedStoreListener</c> to receive callbacks for future transactions </param>
  34. /// <param name="builder"> The <c>ConfigurationBuilder</c> containing the product definitions mapped to stores </param>
  35. public static void Initialize(IDetailedStoreListener listener, ConfigurationBuilder builder)
  36. {
  37. var logger = Debug.unityLogger;
  38. var unityServicesInitializationChecker = new UnityServicesInitializationChecker(logger);
  39. var legacyAnalyticsWrapper = new LegacyAnalyticsWrapper(GenerateLegacyUnityAnalytics(), new EmptyAnalyticsAdapter());
  40. Initialize(listener, builder, logger, Application.persistentDataPath,
  41. GenerateUnityAnalytics(logger), legacyAnalyticsWrapper, builder.factory.GetCatalogProvider(),
  42. unityServicesInitializationChecker);
  43. }
  44. private static IAnalyticsAdapter GenerateUnityAnalytics(ILogger logger)
  45. {
  46. #if DISABLE_RUNTIME_IAP_ANALYTICS || (!IAP_ANALYTICS_SERVICE_ENABLED && !IAP_ANALYTICS_SERVICE_ENABLED_WITH_SERVICE_COMPONENT)
  47. return new EmptyAnalyticsAdapter();
  48. #else
  49. try
  50. {
  51. #if IAP_ANALYTICS_SERVICE_ENABLED
  52. return new AnalyticsAdapter(AnalyticsService.Instance, logger);
  53. #elif IAP_ANALYTICS_SERVICE_ENABLED_WITH_SERVICE_COMPONENT
  54. return new CoreAnalyticsAdapter(AnalyticsService.Instance, logger);
  55. #endif
  56. }
  57. catch (ServicesInitializationException)
  58. {
  59. return new EmptyAnalyticsAdapter();
  60. }
  61. #endif
  62. }
  63. static IAnalyticsAdapter GenerateLegacyUnityAnalytics()
  64. {
  65. #if DISABLE_RUNTIME_IAP_ANALYTICS || !ENABLE_CLOUD_SERVICES_ANALYTICS || !IAP_LEGACY_ANALYTICS_SERVICE_ENABLED
  66. return new EmptyAnalyticsAdapter();
  67. #else
  68. return new LegacyAnalyticsAdapter(new LegacyUnityAnalytics());
  69. #endif
  70. }
  71. /// <summary>
  72. /// This is useful in certain test scenarios, such as repeatedly testing
  73. /// an App's behaviour when purchases are restored.
  74. ///
  75. /// This is a static method since developers may wish to clear the log before
  76. /// initialising IAP.
  77. /// </summary>
  78. public static void ClearTransactionLog()
  79. {
  80. var log = new TransactionLog(Debug.unityLogger, Application.persistentDataPath);
  81. log.Clear();
  82. }
  83. /// <summary>
  84. /// Created for integration testing.
  85. /// </summary>
  86. internal static void Initialize(IStoreListener listener, ConfigurationBuilder builder,
  87. ILogger logger, string persistentDatapath, IAnalyticsAdapter ugsAnalytics, IAnalyticsAdapter legacyAnalytics,
  88. ICatalogProvider catalog, IUnityServicesInitializationChecker unityServicesInitializationChecker)
  89. {
  90. unityServicesInitializationChecker.CheckAndLogWarning();
  91. var transactionLog = new TransactionLog(logger, persistentDatapath);
  92. var manager = new PurchasingManager(transactionLog, logger, builder.factory.service,
  93. builder.factory.storeName, unityServicesInitializationChecker, builder.logUnavailableProducts);
  94. var analyticsClient = new AnalyticsClient(ugsAnalytics, legacyAnalytics);
  95. // Proxy the PurchasingManager's callback interface to forward Transactions to Analytics.
  96. var proxy = new StoreListenerProxy(listener, analyticsClient, builder.factory);
  97. FetchAndMergeProducts(builder.useCatalogProvider, builder.products, catalog, response =>
  98. {
  99. manager.Initialize(proxy, response);
  100. });
  101. }
  102. internal static void FetchAndMergeProducts(bool useCatalog,
  103. HashSet<ProductDefinition> localProductSet, ICatalogProvider catalog, Action<HashSet<ProductDefinition>> callback)
  104. {
  105. if (useCatalog && catalog != null)
  106. {
  107. catalog.FetchProducts(cloudProducts =>
  108. {
  109. var updatedProductSet = new HashSet<ProductDefinition>(localProductSet);
  110. foreach (var product in cloudProducts)
  111. {
  112. // Products are hashed by id, so this should remove the local product with the same id before adding the cloud product
  113. updatedProductSet.Remove(product);
  114. updatedProductSet.Add(product);
  115. }
  116. callback(updatedProductSet);
  117. });
  118. }
  119. else
  120. {
  121. callback(localProductSet);
  122. }
  123. }
  124. }
  125. }