TalkingDataProfile.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. using UnityEngine;
  2. public enum TalkingDataProfileType
  3. {
  4. ANONYMOUS = 0,
  5. REGISTERED = 1,
  6. SINA_WEIBO = 2,
  7. QQ = 3,
  8. QQ_WEIBO = 4,
  9. ND91 = 5,
  10. WEIXIN = 6,
  11. TYPE1 = 11,
  12. TYPE2 = 12,
  13. TYPE3 = 13,
  14. TYPE4 = 14,
  15. TYPE5 = 15,
  16. TYPE6 = 16,
  17. TYPE7 = 17,
  18. TYPE8 = 18,
  19. TYPE9 = 19,
  20. TYPE10 = 20
  21. }
  22. public enum TalkingDataGender
  23. {
  24. UNKNOWN = 0,
  25. MALE = 1,
  26. FEMALE = 2
  27. }
  28. public class TalkingDataProfile
  29. {
  30. #if UNITY_ANDROID
  31. public AndroidJavaObject javaObj;
  32. #endif
  33. #if UNITY_IPHONE
  34. private string name;
  35. private TalkingDataProfileType type;
  36. private TalkingDataGender gender;
  37. private int age;
  38. private object property1;
  39. private object property2;
  40. private object property3;
  41. private object property4;
  42. private object property5;
  43. private object property6;
  44. private object property7;
  45. private object property8;
  46. private object property9;
  47. private object property10;
  48. #endif
  49. public static TalkingDataProfile CreateProfile()
  50. {
  51. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  52. {
  53. TalkingDataProfile profile = new TalkingDataProfile();
  54. #if UNITY_ANDROID
  55. AndroidJavaClass javaClass = new AndroidJavaClass("com.tendcloud.tenddata.TalkingDataProfile");
  56. profile.javaObj = javaClass.CallStatic<AndroidJavaObject>("createProfile");
  57. #endif
  58. return profile;
  59. }
  60. return null;
  61. }
  62. // 账户名称
  63. public TalkingDataProfile SetName(string name)
  64. {
  65. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  66. {
  67. #if UNITY_ANDROID
  68. if (javaObj != null)
  69. {
  70. javaObj.Call<AndroidJavaObject>("setName", name);
  71. }
  72. #endif
  73. #if UNITY_IPHONE
  74. this.name = name;
  75. #endif
  76. }
  77. return this;
  78. }
  79. // 账户类型
  80. public TalkingDataProfile SetType(TalkingDataProfileType type)
  81. {
  82. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  83. {
  84. #if UNITY_ANDROID
  85. if (javaObj != null)
  86. {
  87. AndroidJavaClass enumClass = new AndroidJavaClass("com.tendcloud.tenddata.TalkingDataProfileType");
  88. AndroidJavaObject typeObj = enumClass.CallStatic<AndroidJavaObject>("valueOf", type.ToString());
  89. javaObj.Call<AndroidJavaObject>("setType", typeObj);
  90. }
  91. #endif
  92. #if UNITY_IPHONE
  93. this.type = type;
  94. #endif
  95. }
  96. return this;
  97. }
  98. // 性别
  99. public TalkingDataProfile SetGender(TalkingDataGender gender)
  100. {
  101. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  102. {
  103. #if UNITY_ANDROID
  104. if (javaObj != null)
  105. {
  106. AndroidJavaClass enumClass = new AndroidJavaClass("com.tendcloud.tenddata.TalkingDataGender");
  107. AndroidJavaObject genderObj = enumClass.CallStatic<AndroidJavaObject>("valueOf", gender.ToString());
  108. javaObj.Call<AndroidJavaObject>("setGender", genderObj);
  109. }
  110. #endif
  111. #if UNITY_IPHONE
  112. this.gender = gender;
  113. #endif
  114. }
  115. return this;
  116. }
  117. // 年龄
  118. public TalkingDataProfile SetAge(int age)
  119. {
  120. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  121. {
  122. #if UNITY_ANDROID
  123. if (javaObj != null)
  124. {
  125. javaObj.Call<AndroidJavaObject>("setAge", age);
  126. }
  127. #endif
  128. #if UNITY_IPHONE
  129. this.age = age;
  130. #endif
  131. }
  132. return this;
  133. }
  134. // 用户属性1
  135. public TalkingDataProfile SetProperty1(object property)
  136. {
  137. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  138. {
  139. #if UNITY_ANDROID
  140. if (javaObj != null)
  141. {
  142. AndroidJavaObject androidObject = AndroidJavaObjectFromObject(property);
  143. javaObj.Call<AndroidJavaObject>("setProperty1", androidObject);
  144. }
  145. #endif
  146. #if UNITY_IPHONE
  147. this.property1 = property;
  148. #endif
  149. }
  150. return this;
  151. }
  152. // 用户属性2
  153. public TalkingDataProfile SetProperty2(object property)
  154. {
  155. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  156. {
  157. #if UNITY_ANDROID
  158. if (javaObj != null)
  159. {
  160. AndroidJavaObject androidObject = AndroidJavaObjectFromObject(property);
  161. javaObj.Call<AndroidJavaObject>("setProperty2", androidObject);
  162. }
  163. #endif
  164. #if UNITY_IPHONE
  165. this.property2 = property;
  166. #endif
  167. }
  168. return this;
  169. }
  170. // 用户属性3
  171. public TalkingDataProfile SetProperty3(object property)
  172. {
  173. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  174. {
  175. #if UNITY_ANDROID
  176. if (javaObj != null)
  177. {
  178. AndroidJavaObject androidObject = AndroidJavaObjectFromObject(property);
  179. javaObj.Call<AndroidJavaObject>("setProperty3", androidObject);
  180. }
  181. #endif
  182. #if UNITY_IPHONE
  183. this.property3 = property;
  184. #endif
  185. }
  186. return this;
  187. }
  188. // 用户属性4
  189. public TalkingDataProfile SetProperty4(object property)
  190. {
  191. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  192. {
  193. #if UNITY_ANDROID
  194. if (javaObj != null)
  195. {
  196. AndroidJavaObject androidObject = AndroidJavaObjectFromObject(property);
  197. javaObj.Call<AndroidJavaObject>("setProperty4", androidObject);
  198. }
  199. #endif
  200. #if UNITY_IPHONE
  201. this.property4 = property;
  202. #endif
  203. }
  204. return this;
  205. }
  206. // 用户属性5
  207. public TalkingDataProfile SetProperty5(object property)
  208. {
  209. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  210. {
  211. #if UNITY_ANDROID
  212. if (javaObj != null)
  213. {
  214. AndroidJavaObject androidObject = AndroidJavaObjectFromObject(property);
  215. javaObj.Call<AndroidJavaObject>("setProperty5", androidObject);
  216. }
  217. #endif
  218. #if UNITY_IPHONE
  219. this.property5 = property;
  220. #endif
  221. }
  222. return this;
  223. }
  224. // 用户属性6
  225. public TalkingDataProfile SetProperty6(object property)
  226. {
  227. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  228. {
  229. #if UNITY_ANDROID
  230. if (javaObj != null)
  231. {
  232. AndroidJavaObject androidObject = AndroidJavaObjectFromObject(property);
  233. javaObj.Call<AndroidJavaObject>("setProperty6", androidObject);
  234. }
  235. #endif
  236. #if UNITY_IPHONE
  237. this.property6 = property;
  238. #endif
  239. }
  240. return this;
  241. }
  242. // 用户属性7
  243. public TalkingDataProfile SetProperty7(object property)
  244. {
  245. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  246. {
  247. #if UNITY_ANDROID
  248. if (javaObj != null)
  249. {
  250. AndroidJavaObject androidObject = AndroidJavaObjectFromObject(property);
  251. javaObj.Call<AndroidJavaObject>("setProperty7", androidObject);
  252. }
  253. #endif
  254. #if UNITY_IPHONE
  255. this.property7 = property;
  256. #endif
  257. }
  258. return this;
  259. }
  260. // 用户属性8
  261. public TalkingDataProfile SetProperty8(object property)
  262. {
  263. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  264. {
  265. #if UNITY_ANDROID
  266. if (javaObj != null)
  267. {
  268. AndroidJavaObject androidObject = AndroidJavaObjectFromObject(property);
  269. javaObj.Call<AndroidJavaObject>("setProperty8", androidObject);
  270. }
  271. #endif
  272. #if UNITY_IPHONE
  273. this.property8 = property;
  274. #endif
  275. }
  276. return this;
  277. }
  278. // 用户属性9
  279. public TalkingDataProfile SetProperty9(object property)
  280. {
  281. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  282. {
  283. #if UNITY_ANDROID
  284. if (javaObj != null)
  285. {
  286. AndroidJavaObject androidObject = AndroidJavaObjectFromObject(property);
  287. javaObj.Call<AndroidJavaObject>("setProperty9", androidObject);
  288. }
  289. #endif
  290. #if UNITY_IPHONE
  291. this.property9 = property;
  292. #endif
  293. }
  294. return this;
  295. }
  296. // 用户属性10
  297. public TalkingDataProfile SetProperty10(object property)
  298. {
  299. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  300. {
  301. #if UNITY_ANDROID
  302. if (javaObj != null)
  303. {
  304. AndroidJavaObject androidObject = AndroidJavaObjectFromObject(property);
  305. javaObj.Call<AndroidJavaObject>("setProperty10", androidObject);
  306. }
  307. #endif
  308. #if UNITY_IPHONE
  309. this.property10 = property;
  310. #endif
  311. }
  312. return this;
  313. }
  314. #if UNITY_ANDROID
  315. private AndroidJavaObject AndroidJavaObjectFromObject(object parameter)
  316. {
  317. AndroidJavaObject androidObject = null;
  318. if (parameter is string)
  319. {
  320. androidObject = new AndroidJavaObject("java.lang.String", parameter);
  321. }
  322. else if (parameter is byte || parameter is sbyte)
  323. {
  324. androidObject = new AndroidJavaObject("java.lang.Byte", parameter);
  325. }
  326. else if (parameter is short || parameter is ushort)
  327. {
  328. androidObject = new AndroidJavaObject("java.lang.Short", parameter);
  329. }
  330. else if (parameter is int || parameter is uint)
  331. {
  332. androidObject = new AndroidJavaObject("java.lang.Integer", parameter);
  333. }
  334. else if (parameter is long || parameter is ulong)
  335. {
  336. androidObject = new AndroidJavaObject("java.lang.Long", parameter);
  337. }
  338. else if (parameter is float)
  339. {
  340. androidObject = new AndroidJavaObject("java.lang.Float", parameter);
  341. }
  342. else if (parameter is double)
  343. {
  344. androidObject = new AndroidJavaObject("java.lang.Double", parameter);
  345. }
  346. return androidObject;
  347. }
  348. #endif
  349. #if UNITY_IPHONE
  350. public override string ToString()
  351. {
  352. if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
  353. {
  354. string profileStr = "{\"name\":\"" + name + "\""
  355. + ",\"type\":" + (int)type
  356. + ",\"gender\":" + (int)gender
  357. + ",\"age\":" + age;
  358. if (property1 is string)
  359. {
  360. profileStr += ",\"property1\":\"" + property1 + "\"";
  361. }
  362. else
  363. {
  364. profileStr += ",\"property1\":" + property1;
  365. }
  366. if (property2 is string)
  367. {
  368. profileStr += ",\"property2\":\"" + property2 + "\"";
  369. }
  370. else
  371. {
  372. profileStr += ",\"property2\":" + property2;
  373. }
  374. if (property3 is string)
  375. {
  376. profileStr += ",\"property3\":\"" + property3 + "\"";
  377. }
  378. else
  379. {
  380. profileStr += ",\"property3\":" + property3;
  381. }
  382. if (property4 is string)
  383. {
  384. profileStr += ",\"property4\":\"" + property4 + "\"";
  385. }
  386. else
  387. {
  388. profileStr += ",\"property4\":" + property4;
  389. }
  390. if (property5 is string)
  391. {
  392. profileStr += ",\"property5\":\"" + property5 + "\"";
  393. }
  394. else
  395. {
  396. profileStr += ",\"property5\":" + property5;
  397. }
  398. if (property6 is string)
  399. {
  400. profileStr += ",\"property6\":\"" + property6 + "\"";
  401. }
  402. else
  403. {
  404. profileStr += ",\"property6\":" + property6;
  405. }
  406. if (property7 is string)
  407. {
  408. profileStr += ",\"property7\":\"" + property7 + "\"";
  409. }
  410. else
  411. {
  412. profileStr += ",\"property7\":" + property7;
  413. }
  414. if (property8 is string)
  415. {
  416. profileStr += ",\"property8\":\"" + property8 + "\"";
  417. }
  418. else
  419. {
  420. profileStr += ",\"property8\":" + property8;
  421. }
  422. if (property9 is string)
  423. {
  424. profileStr += ",\"property9\":\"" + property9 + "\"";
  425. }
  426. else
  427. {
  428. profileStr += ",\"property9\":" + property9;
  429. }
  430. if (property10 is string)
  431. {
  432. profileStr += ",\"property10\":\"" + property10 + "\"";
  433. }
  434. else
  435. {
  436. profileStr += ",\"property10\":" + property10;
  437. }
  438. profileStr += "}";
  439. return profileStr;
  440. }
  441. return null;
  442. }
  443. #endif
  444. }