TalkingDataShoppingCart.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using UnityEngine;
  2. #if TD_RETAIL
  3. public class TalkingDataShoppingCart
  4. {
  5. #if UNITY_ANDROID
  6. public AndroidJavaObject javaObj;
  7. #endif
  8. #if UNITY_IPHONE
  9. private string items = "";
  10. #endif
  11. public static TalkingDataShoppingCart CreateShoppingCart()
  12. {
  13. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  14. {
  15. TalkingDataShoppingCart shoppingCart = new TalkingDataShoppingCart();
  16. #if UNITY_ANDROID
  17. AndroidJavaClass javaClass = new AndroidJavaClass("com.tendcloud.tenddata.TalkingDataShoppingCart");
  18. shoppingCart.javaObj = javaClass.CallStatic<AndroidJavaObject>("createShoppingCart");
  19. #endif
  20. return shoppingCart;
  21. }
  22. return null;
  23. }
  24. public TalkingDataShoppingCart AddItem(string itemId, string category, string name, int unitPrice, int amount)
  25. {
  26. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  27. {
  28. #if UNITY_ANDROID
  29. if (javaObj != null)
  30. {
  31. javaObj.Call<AndroidJavaObject>("addItem", itemId, category, name, unitPrice, amount);
  32. }
  33. #endif
  34. #if UNITY_IPHONE
  35. string item = "{\"itemId\":\"" + itemId + "\",\"category\":\"" + category + "\",\"name\":\"" + name + "\",\"unitPrice\":" + unitPrice + ",\"amount\":" + amount + "}";
  36. if (items.Length > 0)
  37. {
  38. items += ",";
  39. }
  40. items += item;
  41. #endif
  42. }
  43. return this;
  44. }
  45. #if UNITY_IPHONE
  46. public override string ToString()
  47. {
  48. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  49. {
  50. string orderStr = "{\"items\":[" + items + "]}";
  51. return orderStr;
  52. }
  53. return null;
  54. }
  55. #endif
  56. }
  57. #endif