1
0

ProductCollection.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace UnityEngine.Purchasing
  5. {
  6. /// <summary>
  7. /// Provides helper methods to retrieve products by
  8. /// store independent/store specific id.
  9. /// </summary>
  10. public class ProductCollection
  11. {
  12. private Dictionary<string, Product> m_IdToProduct;
  13. private Dictionary<string, Product> m_StoreSpecificIdToProduct;
  14. private Product[] m_Products;
  15. private HashSet<Product> m_ProductSet = new HashSet<Product>();
  16. internal ProductCollection(Product[] products)
  17. {
  18. AddProducts(products);
  19. }
  20. internal void AddProducts(IEnumerable<Product> products)
  21. {
  22. m_ProductSet.UnionWith(products);
  23. m_Products = m_ProductSet.ToArray();
  24. m_IdToProduct = m_Products.ToDictionary(x => x.definition.id);
  25. m_StoreSpecificIdToProduct = m_Products.ToDictionary(x => x.definition.storeSpecificId);
  26. }
  27. /// <summary>
  28. /// The hash set of all products
  29. /// </summary>
  30. public HashSet<Product> set
  31. {
  32. get { return m_ProductSet; }
  33. }
  34. /// <summary>
  35. /// The array of all products
  36. /// </summary>
  37. public Product[] all
  38. {
  39. get { return m_Products; }
  40. }
  41. /// <summary>
  42. /// Gets a product matching an id
  43. /// </summary>
  44. /// <param name="id"> The id of the desired product </param>
  45. /// <returns> The product matching the id, or null if not found </returns>
  46. public Product WithID(string id)
  47. {
  48. Product result = null;
  49. m_IdToProduct.TryGetValue(id, out result);
  50. return result;
  51. }
  52. /// <summary>
  53. /// Gets a product matching a store-specific id
  54. /// </summary>
  55. /// <param name="id"> The store-specific id of the desired product </param>
  56. /// <returns> The product matching the id, or null if not found </returns>
  57. public Product WithStoreSpecificID(string id)
  58. {
  59. Product result = null;
  60. if (id != null)
  61. {
  62. m_StoreSpecificIdToProduct.TryGetValue(id, out result);
  63. }
  64. return result;
  65. }
  66. }
  67. }