TalkingDataOrder.cs 2.2 KB

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