MessageHandler.cs 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System;
  6. using System.Threading.Tasks;
  7. #if UNITY_IOS
  8. using AOT;
  9. using System.Runtime.InteropServices;
  10. #endif
  11. [Serializable]
  12. public class User
  13. {
  14. public int id; //用户id
  15. public string nickname; // 昵称
  16. public string avatar; // 头像 http地址
  17. public int gender; // 0: 未设置, 1:男 ,2:女
  18. public string address; // 地址
  19. }
  20. [Serializable]
  21. public class UserFriend
  22. {
  23. public User user; //用户id
  24. public string status; // 状态 [ONLINE,OFFLINE,GAME]
  25. public int rank; // 排名
  26. public int score; // 分数
  27. }
  28. public class Response<T>
  29. {
  30. public List<T> list;
  31. }
  32. enum CMD_MOTION
  33. {
  34. MOTION_STOP = 0,
  35. MOTION_RUN, // 跑
  36. MOTION_JUMP, // 跳
  37. MOTION_DOWN, // 蹲
  38. MOTION_LEFT, // 滑左
  39. MOTION_RIGHT, // 滑右
  40. MOTION_FRONT, // 滑前
  41. MOTION_BACK, // 滑后
  42. MOTION_LEFT_UP, // 点击-左上
  43. MOTION_LEFT_DOWN, // 点击-左下
  44. MOTION_RIGHT_UP, // 点击-右上
  45. MOTION_RIGHT_DOWN, // 点击-右下
  46. MOTION_STEP, // 点击-原地踩
  47. NUMBERS_OF_MOTION,
  48. MOTION_CANCEL = 0x0100,
  49. }
  50. enum BLE_STATE
  51. {
  52. STATE_DISCONNECTED = 0,
  53. STATE_CONNECTING, // 连接中
  54. STATE_CONNECTED, // 连接成功
  55. STATE_DISCONNECTING, // 断开连接
  56. }
  57. [Serializable]
  58. public class Shoe
  59. {
  60. public string name, address;
  61. public int status, electricity;
  62. public int stepLeftStatus, stepLeftFrag, stepRightStatus, stepRightFrag;
  63. public short lx, ly, lz, rx, ry, rz;
  64. public int[] cmd = new int[2];
  65. public int[] motion = new int[4 * 2];
  66. public bool Valid(int ts)
  67. {
  68. return (DateTime.Now.Millisecond - ts) >= (1000 / Application.targetFrameRate);
  69. }
  70. public int GetCmd()
  71. {
  72. if (Valid(cmd[1]))
  73. {
  74. return cmd[0];
  75. }
  76. return -1;
  77. }
  78. public int GetMotionLeft()
  79. {
  80. if (Valid(motion[1]))
  81. {
  82. return motion[0];
  83. }
  84. return -1;
  85. }
  86. public int GetMotionRight()
  87. {
  88. if (Valid(motion[3]))
  89. {
  90. return motion[2];
  91. }
  92. return -1;
  93. }
  94. }
  95. class NativeResponse
  96. {
  97. public int code;
  98. public string result;
  99. }
  100. class NativeCallback : AndroidJavaProxy
  101. {
  102. public NativeCallback() : base("com.ouj.shoe.sdklibrary.interface.IntergrationInterface") { }
  103. public void Callback(int code, string result)
  104. {
  105. Debug.Log(result);
  106. NativeResponse resp = new NativeResponse
  107. {
  108. code = code,
  109. result = result
  110. };
  111. onCallback(resp);
  112. }
  113. public Action<NativeResponse> onCallback;
  114. }
  115. public class MessageHandler : MonoBehaviour
  116. {
  117. private static bool _debug = true;
  118. private Transform left, right;
  119. private Transform cameraTransform;
  120. private static Vector3 lv = new Vector3(0, 0, 0), rv = new Vector3(0, 0, 0);
  121. private static Quaternion lqo = new Quaternion(0, 0, 0, 0), rqo = new Quaternion(0, 0, 0, 0);
  122. private static Quaternion lq = new Quaternion(0, 0, 0, 0), rq = new Quaternion(0, 0, 0, 0);
  123. private static Text montionTextPos, montionTextAtt, montionTextAttConver, montionTextStep;
  124. private static int motionLeftCode_0 = -1, motionRightCode_0 = -1;
  125. private static int motionLeftCode_1 = -1, motionRightCode_1 = -1;
  126. private static Text textDeviceOne, textDeviceOneStatus, textDeviceOneElectricity;
  127. private static Text textDeviceTwo, textDeviceTwoStatus, textDeviceTwoElectricity;
  128. private static Text textGetUserFriends;
  129. private static Text textGameMode;
  130. private static Text textDeviceOneMotion, textDeviceTwoMotion;
  131. private static Response<UserFriend> friends;
  132. private static string _name_0, _address_0;
  133. private static int _status_0, _electricity_0;
  134. private static string _name_1, _address_1;
  135. private static int _status_1, _electricity_1;
  136. private static bool _gameMode = false;
  137. private static int _leftStatus, _leftFrag, _rightStatus, _rightFrag;
  138. private static short _lx, _ly, _lz, _rx, _ry, _rz;
  139. private static Shoe[] shoes = new Shoe[2];
  140. /**
  141. * 获取鞋子数据信息
  142. */
  143. public Shoe GetShoe(int id) { return shoes[id]; }
  144. /**
  145. * 测试 或 使用方法参考
  146. *
  147. *
  148. */
  149. void test()
  150. {
  151. Debug.Log("test start");
  152. GameObject.Find("btnGameStart").GetComponent<Button>().onClick.AddListener(delegate ()
  153. {
  154. Debug.Log("click GameStart!");
  155. GameStart();
  156. });
  157. GameObject.Find("btnGameEnd").GetComponent<Button>().onClick.AddListener(delegate () { Debug.Log("click GameEnd!"); GameEnd(1, 1, 1, 1, 0); });
  158. GameObject.Find("btnGetUser").GetComponent<Button>().onClick.AddListener(delegate ()
  159. {
  160. Debug.Log("click GetUserInfo!");
  161. User user = GetUserInfo();
  162. GameObject.Find("textGetUserInfo").GetComponent<Text>().text = JsonUtility.ToJson(user);
  163. });
  164. GameObject.Find("btnGetUserFriends").GetComponent<Button>().onClick.AddListener(delegate ()
  165. {
  166. Debug.Log("click GetUserFriends!");
  167. GetUserFriends(0);
  168. });
  169. GameObject.Find("btnDeviceOneConnect").GetComponent<Button>().onClick.AddListener(delegate () { Debug.Log("click SearchDevice!"); SearchDevice(0); });
  170. GameObject.Find("btnDeviceTwoConnect").GetComponent<Button>().onClick.AddListener(delegate () { Debug.Log("click SearchDevice!"); SearchDevice(1); });
  171. GameObject.Find("btnInvite").GetComponent<Button>().onClick.AddListener(delegate ()
  172. {
  173. Debug.Log("click InviteFriend!");
  174. string input = GameObject.Find("InputUserFirend").GetComponent<InputField>().text.Trim();
  175. if (input.Length == 0)
  176. {
  177. return;
  178. }
  179. InviteFriend(int.Parse(input), "123456");
  180. });
  181. GameObject.Find("btnGetRank").GetComponent<Button>().onClick.AddListener(delegate () { Debug.Log("click btnGetRank!"); GetRank(0); });
  182. GameObject.Find("btnGetRankFriend").GetComponent<Button>().onClick.AddListener(delegate () { Debug.Log("click btnGetRankFriend!"); GetRank(1); });
  183. GameObject MontionLeft = GameObject.Find("MontionLeft");
  184. GameObject MontionRight = GameObject.Find("MontionRight");
  185. if (MontionRight != null)
  186. {
  187. right = MontionRight.GetComponent<Renderer>().transform;
  188. }
  189. if (MontionLeft != null)
  190. {
  191. left = MontionLeft.GetComponent<Renderer>().transform;
  192. }
  193. GameObject MainCamera = GameObject.Find("Main Camera");
  194. Debug.Log("test MainCamera: " + MainCamera);
  195. if (MainCamera != null)
  196. {
  197. cameraTransform = MainCamera.GetComponent<Camera>().transform;
  198. }
  199. GameObject.Find("MotionButton").GetComponent<Button>().onClick.AddListener(delegate ()
  200. {
  201. if (left != null)
  202. {
  203. lv = new Vector3(-1, 0, 50);
  204. lq = new Quaternion(0, 0, 0, 0);
  205. }
  206. if (right != null)
  207. {
  208. rv = new Vector3(1, 0, 50);
  209. rq = new Quaternion(0, 0, 0, 0);
  210. }
  211. if (cameraTransform != null)
  212. {
  213. cameraTransform.position = new Vector3(0, 0, -10);
  214. }
  215. GameObject.Find("SliderX").GetComponent<Slider>().value = 0;
  216. GameObject.Find("SliderY").GetComponent<Slider>().value = 0;
  217. montionTextAttConver.text = string.Format("{0:F2}, {1:F2}, {2:F2} ---- {3:F2}, {4:F2}, {5:F2}", 0, 0, 0, 0, 0, 0);
  218. });
  219. GameObject.Find("SliderX").GetComponent<Slider>().onValueChanged.AddListener((float value) =>
  220. {
  221. if (cameraTransform != null)
  222. {
  223. cameraTransform.Translate(new Vector3(value - cameraTransform.position.x, 0, 0));
  224. }
  225. });
  226. GameObject.Find("SliderY").GetComponent<Slider>().onValueChanged.AddListener((float value) =>
  227. {
  228. if (cameraTransform != null)
  229. {
  230. cameraTransform.Translate(new Vector3(0, value - cameraTransform.position.y, 0));
  231. }
  232. });
  233. montionTextPos = GameObject.Find("MontionTextPos").GetComponent<Text>();
  234. montionTextAtt = GameObject.Find("MontionTextAtt").GetComponent<Text>();
  235. montionTextAttConver = GameObject.Find("MontionTextAttConver").GetComponent<Text>();
  236. textDeviceOne = GameObject.Find("textDeviceOne").GetComponent<Text>();
  237. textDeviceOneStatus = GameObject.Find("textDeviceOneStatus").GetComponent<Text>();
  238. textDeviceOneElectricity = GameObject.Find("textDeviceOneElectricity").GetComponent<Text>();
  239. textDeviceTwo = GameObject.Find("textDeviceTwo").GetComponent<Text>();
  240. textDeviceTwoStatus = GameObject.Find("textDeviceTwoStatus").GetComponent<Text>();
  241. textDeviceTwoElectricity = GameObject.Find("textDeviceTwoElectricity").GetComponent<Text>();
  242. montionTextStep = GameObject.Find("MontionTextStep").GetComponent<Text>();
  243. textGetUserFriends = GameObject.Find("textGetUserFriends").GetComponent<Text>();
  244. textGameMode = GameObject.Find("textGameMode").GetComponent<Text>();
  245. textDeviceOneMotion = GameObject.Find("textDeviceOneMotion").GetComponent<Text>();
  246. textDeviceTwoMotion = GameObject.Find("textDeviceTwoMotion").GetComponent<Text>();
  247. Debug.Log("test montionTextPos: " + montionTextPos);
  248. Debug.Log("test montionTextAtt: " + montionTextAtt);
  249. Debug.Log("test montionTextAttConver: " + montionTextAttConver);
  250. Debug.Log("test textDeviceOne: " + textDeviceOne);
  251. Debug.Log("test textDeviceOneStatus: " + textDeviceOneStatus);
  252. Debug.Log("test textDeviceOneElectricity: " + textDeviceOneElectricity);
  253. Debug.Log("test textDeviceTwo: " + textDeviceTwo);
  254. Debug.Log("test textDeviceTwoStatus: " + textDeviceTwoStatus);
  255. Debug.Log("test textDeviceTwoElectricity: " + textDeviceTwoElectricity);
  256. Debug.Log("test montionTextStep: " + montionTextStep);
  257. Debug.Log("test textGetUserFriends: " + textGetUserFriends);
  258. Debug.Log("test textDeviceOneMotion: " + textDeviceOneMotion);
  259. Debug.Log("test textDeviceTwoMotion: " + textDeviceTwoMotion);
  260. Debug.Log("test end");
  261. }
  262. private void Awake()
  263. {
  264. Screen.sleepTimeout = SleepTimeout.NeverSleep;
  265. shoes = new Shoe[2];
  266. for (int i = 0; i < shoes.Length; i++)
  267. {
  268. shoes[i] = new Shoe();
  269. }
  270. }
  271. void Update()
  272. {
  273. if (MessageHandler._debug)
  274. {
  275. if (right != null)
  276. {
  277. right.position = rv;
  278. right.rotation = rq;
  279. }
  280. if (left != null)
  281. {
  282. left.position = lv;
  283. left.rotation = lq;
  284. }
  285. if(textGameMode != null)
  286. textGameMode.text = "游戏模式:" + _gameMode;
  287. if (montionTextAtt != null)
  288. montionTextPos.text = _lx + ", " + _ly + ", " + _lz + " ---- " + _rx + ", " + _ry + ", " + _rz;
  289. if (montionTextAtt != null)
  290. montionTextAtt.text = lqo.x + ", " + lqo.y + ", " + lqo.z + " ---- " + rqo.x + ", " + rqo.y + ", " + rqo.z;
  291. if (montionTextAttConver != null)
  292. montionTextAttConver.text = string.Format("{0:F2}, {1:F2}, {2:F2} ---- {3:F2}, {4:F2}, {5:F2}", lq.x, lq.y, lq.z, rq.x, rq.y, rq.z);
  293. if (montionTextStep != null)
  294. montionTextStep.text = string.Format("{0},{1} ,,,, {2},{3}", _leftStatus, _leftFrag, _rightStatus, _rightFrag);
  295. if (textDeviceOne != null)
  296. textDeviceOne.text = _name_0;
  297. if (textDeviceOneStatus != null)
  298. textDeviceOneStatus.text = "" + Enum.GetName(typeof(BLE_STATE), _status_0);
  299. if (textDeviceOneMotion != null)
  300. textDeviceOneMotion.text = Enum.GetName(typeof(CMD_MOTION), motionLeftCode_0) + " | " + Enum.GetName(typeof(CMD_MOTION), motionRightCode_0);
  301. if (textDeviceOneElectricity != null)
  302. textDeviceOneElectricity.text = "" + _electricity_0;
  303. if (textDeviceTwo != null)
  304. textDeviceTwo.text = _name_1;
  305. if (textDeviceTwoStatus != null)
  306. textDeviceTwoStatus.text = "" + Enum.GetName(typeof(BLE_STATE), _status_1);
  307. if (textDeviceTwoMotion != null)
  308. textDeviceTwoMotion.text = Enum.GetName(typeof(CMD_MOTION), motionLeftCode_1) + " | " + Enum.GetName(typeof(CMD_MOTION), motionRightCode_1);
  309. if (textDeviceTwoElectricity != null)
  310. textDeviceTwoElectricity.text = "" + _electricity_1;
  311. if (friends != null)
  312. if (textGetUserFriends != null)
  313. textGetUserFriends.text = JsonUtility.ToJson(friends);
  314. }
  315. }
  316. static void GameModeInner(bool mode)
  317. {
  318. Debug.Log("gameMode : " + mode);
  319. if (MessageHandler._debug)
  320. {
  321. _gameMode = mode;
  322. }
  323. }
  324. static void ActionInner(int id, int code)
  325. {
  326. Debug.LogFormat("action: {0:D} {1:D}", id, code);
  327. Shoe shoe = shoes[id];
  328. shoe.cmd[0] = code;
  329. shoe.cmd[1] = DateTime.Now.Millisecond;
  330. // @see CMD_MOTION
  331. //MOTION_STOP = 0, 取消
  332. //MOTION_LEFT, // 左
  333. //MOTION_RIGHT, // 右
  334. //MOTION_FRONT, // 上
  335. //MOTION_BACK, // 下
  336. //MOTION_STEP, // 确定
  337. }
  338. static void MotionInner(int id, int left, int right)
  339. {
  340. Debug.LogFormat("motion id: {0:D}, left: {1:D}, right: {2:D}", id, left, right);
  341. if (MessageHandler._debug)
  342. {
  343. if(id == 0)
  344. {
  345. if (left > -1 && motionLeftCode_0 != left)
  346. {
  347. motionLeftCode_0 = left;
  348. }
  349. if (right > -1 && motionRightCode_0 != right)
  350. {
  351. motionRightCode_0 = right;
  352. }
  353. }
  354. else
  355. {
  356. if (left > -1 && motionLeftCode_1 != left)
  357. {
  358. motionLeftCode_1 = left;
  359. }
  360. if (right > -1 && motionRightCode_1 != right)
  361. {
  362. motionRightCode_1 = right;
  363. }
  364. }
  365. }
  366. // 是否跳
  367. bool jump = left == (int)CMD_MOTION.MOTION_JUMP || right == (int)CMD_MOTION.MOTION_JUMP;
  368. // 是否蹲
  369. bool squat = left == (int)CMD_MOTION.MOTION_DOWN || right == (int)CMD_MOTION.MOTION_DOWN;
  370. int[] code = { left, right };
  371. int now = DateTime.Now.Millisecond;
  372. Shoe shoe = shoes[id];
  373. if(left > 0)
  374. {
  375. shoe.motion[0] = left;
  376. shoe.motion[1] = now;
  377. }
  378. if(right > 0)
  379. {
  380. shoe.motion[2] = right;
  381. shoe.motion[3] = now;
  382. }
  383. // 动作分发处理
  384. for (int i = 0; i < code.Length; i++)
  385. {
  386. int cmd = code[i];
  387. if (cmd < 0)
  388. continue;
  389. // i:0 = 左鞋动作
  390. // i:1 = 右鞋动作
  391. switch (cmd)
  392. {
  393. case (int)CMD_MOTION.MOTION_STOP:
  394. break;
  395. case (int)CMD_MOTION.MOTION_RUN:
  396. break;
  397. case (int)CMD_MOTION.MOTION_JUMP:
  398. break;
  399. case (int)CMD_MOTION.MOTION_DOWN:
  400. break;
  401. case (int)CMD_MOTION.MOTION_LEFT:
  402. break;
  403. case (int)CMD_MOTION.MOTION_RIGHT:
  404. break;
  405. case (int)CMD_MOTION.MOTION_FRONT:
  406. break;
  407. case (int)CMD_MOTION.MOTION_BACK:
  408. break;
  409. case (int)CMD_MOTION.MOTION_LEFT_UP:
  410. break;
  411. case (int)CMD_MOTION.MOTION_LEFT_DOWN:
  412. break;
  413. case (int)CMD_MOTION.MOTION_RIGHT_UP:
  414. break;
  415. case (int)CMD_MOTION.MOTION_RIGHT_DOWN:
  416. break;
  417. case (int)CMD_MOTION.MOTION_STEP:
  418. break;
  419. case (int)CMD_MOTION.MOTION_LEFT_UP | (int)CMD_MOTION.MOTION_CANCEL: // 取消 - 左上
  420. break;
  421. case (int)CMD_MOTION.MOTION_LEFT_DOWN | (int)CMD_MOTION.MOTION_CANCEL: // 取消 - 左下
  422. break;
  423. case (int)CMD_MOTION.MOTION_RIGHT_UP | (int)CMD_MOTION.MOTION_CANCEL: // 取消 - 右上
  424. break;
  425. case (int)CMD_MOTION.MOTION_RIGHT_DOWN | (int)CMD_MOTION.MOTION_CANCEL: // 取消 - 右下
  426. break;
  427. case (int)CMD_MOTION.MOTION_STEP | (int)CMD_MOTION.MOTION_CANCEL: // 取消 - 中间
  428. break;
  429. }
  430. }
  431. }
  432. static void StepInner(int id, int leftStatus, int rightStatus, int leftFrag, int rightFrag)
  433. {
  434. Debug.LogFormat("step id: {0:D}, leftStatus: {1:D}, rightStatus: {2:D}, leftFrag: {3:D}, rightFrag: {4:D}", id, leftStatus, rightStatus, leftFrag, rightFrag);
  435. if (MessageHandler._debug && id == 0)
  436. {
  437. _leftStatus = leftStatus;
  438. _leftFrag = leftFrag;
  439. _rightStatus = rightStatus;
  440. _rightFrag = rightFrag;
  441. }
  442. Shoe shoe = shoes[id];
  443. shoe.stepLeftFrag = leftFrag;
  444. shoe.stepLeftStatus = leftStatus;
  445. shoe.stepRightFrag = rightFrag;
  446. shoe.stepRightStatus = rightStatus;
  447. }
  448. static void TranslateInner(int id, short lx, short ly, short lz, short rx, short ry, short rz)
  449. {
  450. //Debug.Log("translate id: " + id);
  451. if (MessageHandler._debug && id == 0)
  452. {
  453. _lx = lx;
  454. _ly = ly;
  455. _lz = lz;
  456. _rx = rx;
  457. _ry = ry;
  458. _rz = rz;
  459. rv = new Vector3(-rx / 10f + 1, -ry / 10f, -rz / 10f + 50);
  460. lv = new Vector3(lx / 10f - 1, ly / 10f, -lz / 10f + 50);
  461. }
  462. }
  463. static void RotateInner(int id, short lx, short ly, short lz, short rx, short ry, short rz)
  464. {
  465. //Debug.Log("rotate id: " + id);
  466. if (MessageHandler._debug && id == 0)
  467. {
  468. rqo = new Quaternion(rx, ry, rz, 0);
  469. lqo = new Quaternion(lx, ly, lz, 0);
  470. rq = new Quaternion(-rx / 10000f * Mathf.Rad2Deg, -ry / 10000f * Mathf.Rad2Deg, -rz / 10000f * Mathf.Rad2Deg, 0);
  471. lq = new Quaternion(-lx / 10000f * Mathf.Rad2Deg, -ly / 10000f * Mathf.Rad2Deg, -lz / 10000f * Mathf.Rad2Deg, 0);
  472. }
  473. }
  474. static void DeviceInner(int id, string name, string address, int status, int electricity)
  475. {
  476. Debug.Log("device id: " + id + ", name: " + name + ", address: " + address + ", status: " + status + ", electricity: " + electricity);
  477. if (MessageHandler._debug)
  478. {
  479. if (id == 0)
  480. {
  481. _name_0 = name;
  482. _address_0 = address;
  483. _status_0 = status;
  484. _electricity_0 = electricity;
  485. }
  486. else if (id == 1)
  487. {
  488. _name_1 = name;
  489. _address_1 = address;
  490. _status_1 = status;
  491. _electricity_1 = electricity;
  492. }
  493. }
  494. Shoe shoe = shoes[id];
  495. if (!address.Equals(shoe.address))
  496. {
  497. shoe = new Shoe
  498. {
  499. name = name,
  500. address = address,
  501. status = status,
  502. electricity = electricity
  503. };
  504. shoes[id] = shoe;
  505. }
  506. }
  507. static void UserFriendsInner(int code, string json)
  508. {
  509. Debug.Log("userFriends Response<UserFriend> code: " + code + " -> " + json);
  510. if (MessageHandler._debug)
  511. {
  512. string jsonStr = "{ \"list\": " + json + "}";
  513. friends = JsonUtility.FromJson<Response<UserFriend>>(jsonStr);
  514. }
  515. if (code == 0)
  516. {
  517. // 显示列表
  518. }
  519. }
  520. static void InviteFriendInner(int code, string userJson, string inviteInfo)
  521. {
  522. Debug.Log("InviteFriendInner code: " + code + " -> " + userJson + ", " + inviteInfo);
  523. string json = userJson;
  524. if (json != null)
  525. {
  526. User user = JsonUtility.FromJson<User>(json);
  527. if(inviteInfo != null) //
  528. {
  529. }
  530. }
  531. }
  532. static void OnGetRankInner(int code, string json)
  533. {
  534. Debug.Log("OnGetRankInner -> " + json);
  535. Response<UserFriend> resp = JsonUtility.FromJson<Response<UserFriend>>(json);
  536. Debug.Log("GetRank resp: " + resp.list.Count);
  537. //for(int i = 0; i<resp.list.Count; i++)
  538. //{
  539. // UserFriend item = resp.list[i];
  540. // User user = item.user;
  541. // Debug.Log("GetRank user: " + user.nickname + ", " + user.avatar + ", score: " + item.score );
  542. //}
  543. }
  544. #if UNITY_IOS
  545. // IOS TO U3D start =================================
  546. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  547. public delegate void MotionHandler(int id, int left, int right);
  548. [MonoPInvokeCallback(typeof(MotionHandler))]
  549. static void motionHandler(int id, int left, int right)
  550. {
  551. MotionInner(id, left, right);
  552. }
  553. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  554. public delegate void ActionHandler(int id, int code);
  555. [MonoPInvokeCallback(typeof(ActionHandler))]
  556. static void actionHandler(int id, int code)
  557. {
  558. ActionInner(id, code);
  559. }
  560. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  561. public delegate void StepHandler(int id, int leftStatus, int rightStatus, int leftFrag, int rightFrag);
  562. [MonoPInvokeCallback(typeof(StepHandler))]
  563. static void stepHandler(int id, int leftStatus, int rightStatus, int leftFrag, int rightFrag)
  564. {
  565. StepInner(id, leftStatus, rightStatus, leftFrag, rightFrag);
  566. }
  567. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  568. public delegate void DeviceHandler(int id, string name, string address, int status, int electricity);
  569. [MonoPInvokeCallback(typeof(DeviceHandler))]
  570. static void deviceHandler(int id, string name, string address, int status, int electricity)
  571. {
  572. DeviceInner(id, name, address, status, electricity);
  573. }
  574. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  575. public delegate void UserFriendsHandler(int code, string json);
  576. [MonoPInvokeCallback(typeof(UserFriendsHandler))]
  577. static void userFriendsHandler(int code, string json)
  578. {
  579. UserFriendsInner(code, json);
  580. }
  581. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  582. public delegate void InviteFriendHandler(int code, string userJson, string inviteInfo);
  583. [MonoPInvokeCallback(typeof(InviteFriendHandler))]
  584. static void inviteFriendHandler(int code, string userJson, string inviteInfo)
  585. {
  586. InviteFriendInner(code, userJson, inviteInfo);
  587. }
  588. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  589. public delegate void GetRankHandler(int code, string userJson);
  590. [MonoPInvokeCallback(typeof(GetRankHandler))]
  591. static void getRankHandler(int code, string userJson)
  592. {
  593. OnGetRankInner(code, userJson);
  594. }
  595. // U3D TO IOS start =================================
  596. [DllImport("__Internal")]
  597. static extern void GameStart();
  598. [DllImport("__Internal")]
  599. static extern void GameEnd(int level, double score, int record, int mode, int opponentId);
  600. [DllImport("__Internal")]
  601. static extern string GetUserInfoJson();
  602. [DllImport("__Internal")]
  603. static extern void GetUserFriends(int code);
  604. [DllImport("__Internal")]
  605. static extern void SearchDevice(int type);
  606. [DllImport("__Internal")]
  607. static extern void ConnectDevice(int type);
  608. [DllImport("__Internal")]
  609. static extern void DisConnectDevice(int type);
  610. [DllImport("__Internal")]
  611. static extern void InviteFriend(int friendId, string info);
  612. [DllImport("__Internal")]
  613. static extern void GetInviteInfo();
  614. [DllImport("__Internal")]
  615. static extern void Vibrate(int type, int duration);
  616. [DllImport("__Internal")]
  617. static extern void ScreenProjection();
  618. [DllImport("__Internal")]
  619. static extern void ShowInviteFriend(int code, string info);
  620. [DllImport("__Internal")]
  621. static extern void GetRank(int type);
  622. [DllImport("__Internal")]
  623. static extern void PointerActionHandler(IntPtr resultHandler);
  624. [DllImport("__Internal")]
  625. static extern void PointerMotionHandler(IntPtr resultHandler);
  626. [DllImport("__Internal")]
  627. static extern void PointerStepHandler(IntPtr resultHandler);
  628. [DllImport("__Internal")]
  629. static extern void PointerDeviceHandler(IntPtr resultHandler);
  630. [DllImport("__Internal")]
  631. static extern void PointerUserFriendsHandler(IntPtr resultHandler);
  632. [DllImport("__Internal")]
  633. static extern void PointerInviteFriendHandler(IntPtr resultHandler);
  634. [DllImport("__Internal")]
  635. static extern void PointerGetRankHandler(IntPtr resultHandler);
  636. public User GetUserInfo()
  637. {
  638. string json = GetUserInfoJson();
  639. if (json != null)
  640. {
  641. return JsonUtility.FromJson<User>(json);
  642. }
  643. return null;
  644. }
  645. void Start()
  646. {
  647. PointerActionHandler(Marshal.GetFunctionPointerForDelegate(new ActionHandler(actionHandler)));
  648. PointerMotionHandler( Marshal.GetFunctionPointerForDelegate(new MotionHandler(motionHandler)));
  649. PointerStepHandler(Marshal.GetFunctionPointerForDelegate(new StepHandler(stepHandler)));
  650. PointerDeviceHandler( Marshal.GetFunctionPointerForDelegate(new DeviceHandler(deviceHandler)));
  651. PointerUserFriendsHandler(Marshal.GetFunctionPointerForDelegate(new UserFriendsHandler(userFriendsHandler)));
  652. PointerInviteFriendHandler(Marshal.GetFunctionPointerForDelegate(new InviteFriendHandler(inviteFriendHandler)));
  653. PointerGetRankHandler(Marshal.GetFunctionPointerForDelegate(new GetRankHandler(getRankHandler)));
  654. if (MessageHandler._debug)
  655. {
  656. test();
  657. }
  658. }
  659. #elif UNITY_ANDROID
  660. /**
  661. * id: 设备类型
  662. * 0: 主设备
  663. * 1: 副设备
  664. * 2:
  665. * 3:
  666. */
  667. private class MessageChannel : AndroidJavaProxy
  668. {
  669. const int MOTION_CANCEL = 0x0100;
  670. public MessageChannel() : base("com.ouj.shoe.sdklibrary.MessageChannel")
  671. {
  672. }
  673. /**
  674. * mode: 游戏模式开头
  675. * 可忽略处理
  676. */
  677. public void gameMode(bool mode)
  678. {
  679. GameModeInner(mode);
  680. }
  681. /**
  682. * code: 操作代码
  683. * 对应 enum CMD_MOTION
  684. */
  685. public void action(int id, int code)
  686. {
  687. ActionInner(id, code);
  688. }
  689. /**
  690. * left: 左鞋动作代码
  691. * right: 右鞋动作代码
  692. * 对应 enum CMD_MOTION
  693. */
  694. public void motion(int id, int left, int right)
  695. {
  696. MotionInner(id, left, right);
  697. }
  698. /**
  699. * 踏步状态,频率
  700. * leftStatus: 左鞋(0:停止, 1:慢,2:快)
  701. * rightStatus: 右鞋(0:停止, 1:慢,2:快)
  702. * leftFrag: 左鞋(每分钟步频)
  703. * rightFrag: 右鞋(每分钟步频)
  704. */
  705. public void step(int id, int leftStatus, int rightStatus, int leftFrag, int rightFrag)
  706. {
  707. StepInner(id, leftStatus, rightStatus, leftFrag, rightFrag);
  708. }
  709. /**
  710. * array: x,y坐标数据
  711. * 单位:cm
  712. */
  713. public void translate(int id, short lx, short ly, short lz, short rx, short ry, short rz)
  714. {
  715. TranslateInner(id, lx, ly, lz, rx, ry, rz);
  716. }
  717. /**
  718. * array: x,y,z姿态数据
  719. * 单位:弧度
  720. * 使用时要将数据 除以 10000
  721. * 转换角度可以使用 Mathf.Rad2Deg
  722. */
  723. public void rotate(int id, short lx, short ly, short lz, short rx, short ry, short rz)
  724. {
  725. RotateInner(id, lx, ly, lz, rx, ry, rz);
  726. }
  727. /**
  728. * 返回蓝牙设备的基本信息,用于界面上显示当前的设备状况
  729. *
  730. * name: 设备名称
  731. * status: 设备状态 @see enum BLE_STATE
  732. * electricity: 设备电量 取值范围[0 - 100]
  733. */
  734. public void device(int id, string name, string address, int status, int electricity)
  735. {
  736. DeviceInner(id, name, address, status, electricity);
  737. }
  738. /**
  739. * 调用api GetUserFriends() 的回调
  740. *
  741. * code: 请求结果代码 0: 成功, 非0: 根据业务处理
  742. * json: 好友列表数据 , json 数组
  743. *
  744. */
  745. public void userFriends(int code, string json)
  746. {
  747. UserFriendsInner(code, json);
  748. }
  749. /**
  750. * 接收到邀请的回调
  751. *
  752. * code: 请求结果代码 0: 成功, 非0: 根据业务处理
  753. * user: json 结构
  754. * inviteInfo: 发起的信息
  755. *
  756. */
  757. public void onInviteFriend(int code, string user, string inviteInfo)
  758. {
  759. InviteFriendInner(code, user, inviteInfo);
  760. }
  761. }
  762. private AndroidJavaClass _jc;
  763. private AndroidJavaObject _jo;
  764. // Start is called before the first frame update
  765. void Start()
  766. {
  767. try
  768. {
  769. // 注入通信接口
  770. Debug.Log("sdk init start!");
  771. _jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
  772. _jo = _jc.GetStatic<AndroidJavaObject>("currentActivity");
  773. MessageChannel channel = new MessageChannel();
  774. _jo.Call("setMessageChannel", channel);
  775. if (MessageHandler._debug)
  776. {
  777. GameObject.Find("textLinkSdk").GetComponent<Text>().text = "" + channel;
  778. }
  779. }
  780. catch (Exception e)
  781. {
  782. Debug.Log("sdk init error! " + e);
  783. }
  784. if (MessageHandler._debug)
  785. {
  786. test();
  787. }
  788. }
  789. /// <summary>
  790. /// 游戏开发商获取信息接口
  791. /// 获取用户基本信息,请开发商根据返回的信息创建玩家资料,做到sdk和游戏内玩家信息一致
  792. /// @see User
  793. /// </summary>
  794. public User GetUserInfo()
  795. {
  796. Debug.Log("GetUserInfo");
  797. string json = _jo.Call<string>("getUserInfo");
  798. if (json != null)
  799. {
  800. return JsonUtility.FromJson<User>(json);
  801. }
  802. return null;
  803. }
  804. /// <summary>
  805. /// 游戏开发商获取信息接口
  806. /// 请求好友列表,结果会在MessageChannel中回调
  807. /// @see MessageChannel.userFriends(code, json)
  808. /// @see UserFriend
  809. /// </summary>
  810. public void GetUserFriends(int code)
  811. {
  812. Debug.Log("GetUserFriends");
  813. _jo.Call("getUserFriends", code);
  814. }
  815. /// <summary>
  816. /// 游戏开发商事件
  817. /// 请在每局游戏开始调用, 通知SDK开始记录
  818. /// motion事件要调用GameStart()后才有回调
  819. /// </summary>
  820. public void GameStart()
  821. {
  822. Debug.Log("GameStart");
  823. _jo.Call("gameStart");
  824. }
  825. /// <summary>
  826. /// 游戏开发商事件
  827. /// 请在每局游戏结束后, 调用通知SDK上传对战信息,并添加相关参数
  828. /// </summary>
  829. /// <param name="level">游戏难度</param>
  830. /// <param name="score">游戏评分</param>
  831. /// <param name="record">游戏战绩,0: 负, 1: 胜</param>
  832. /// <param name="mode">游戏模式,0: 单人, 1: 匹配, 2: 好友对战</param>
  833. /// <param name="opponentId">房间ID或对手用户ID,可为 0</param>
  834. public void GameEnd(int level, double score, int record, int mode, int opponentId)
  835. {
  836. Debug.Log("GameEnd");
  837. _jo.Call("gameEnd", level, score, record, mode, opponentId);
  838. }
  839. /// <summary>
  840. /// 游戏开发商事件
  841. /// 重新连接蓝牙设备
  842. /// 当蓝牙设备异常,无法连接时可重新连接蓝牙设备
  843. /// </summary>
  844. /// <param name="type">设备类型 0: 主, 1: 副</param>
  845. public void ConnectDevice(int type)
  846. {
  847. Debug.Log("ConnectDevice type: " + type);
  848. _jo.Call("connectDevice", type);
  849. }
  850. /// <summary>
  851. /// 游戏开发商事件
  852. /// 当蓝牙设备异常,无法连接时可重新连接蓝牙设备
  853. /// </summary>
  854. /// <param name="address">设备地址:可更换设备,前提是设备不在连接状态</param>
  855. /// <param name="type">设备类型 0: 主, 1: 副</param>
  856. public void ConnectDevice(string address, int type)
  857. {
  858. Debug.Log("ConnectDevice type: " + type + ", " + address);
  859. _jo.Call("connectDevice", address, type);
  860. }
  861. /// <summary>
  862. /// 游戏开发商事件
  863. /// 断开已连接连接的设备
  864. /// </summary>
  865. /// <param name="type">设备类型 0: 主, 1: 副</param>
  866. public void DisConnectDevice(int type)
  867. {
  868. Debug.Log("disConnectDevice type: " + type);
  869. _jo.Call("disConnectDevice", type);
  870. }
  871. /// <summary>
  872. /// 游戏开发商事件
  873. /// 弹出搜索蓝牙设备的对话框
  874. /// </summary>
  875. /// <param name="type">设备类型 0: 主, 1: 副,连接后的设备绑定为相应的类型,单人模式请用 0 </param>
  876. public void SearchDevice(int type)
  877. {
  878. Debug.Log("SearchDevice type: " + type);
  879. _jo.Call("searchDevice", type);
  880. }
  881. /// <summary>
  882. /// 游戏开发商事件
  883. /// 邀请好友
  884. /// </summary>
  885. /// <param name="friendId">用户ID, sdk发送邀请信息给对应的好友</param>
  886. /// <param name="info">邀请的信息,文本信息一直带给好友,在onInviteFriend(User, string)中传入</param>
  887. /// @see UserFriend.User.id
  888. public void InviteFriend(int friendId, string info)
  889. {
  890. Debug.Log("InviteFriend friend: " + friendId + ", " + info);
  891. _jo.Call("inviteFriend", friendId, info);
  892. }
  893. /// <summary>
  894. /// 界面准备好后可以申请邀请信息
  895. /// </summary>
  896. public void GetInviteInfo()
  897. {
  898. Debug.Log("GetInviteInfo");
  899. _jo.Call("getInviteInfo");
  900. }
  901. /// <summary>
  902. /// 游戏开发商事件
  903. /// 搜索可投屏的设备
  904. /// </summary>
  905. public void ScreenProjection()
  906. {
  907. _jo.Call("screenProjection");
  908. }
  909. /// <summary>
  910. /// 游戏开发商事件
  911. /// 震动事件
  912. /// </summary>
  913. /// <param name="type">设备类型 0: 主, 1: 副,连接后的设备绑定为相应的类型,单人模式请用 0 </param>
  914. /// <param name="duration">时长 ms [100 .. 1000]</param>
  915. public void Vibrate(int type, int duration)
  916. {
  917. _jo.Call("vibrate", type, duration);
  918. }
  919. /// <summary>
  920. /// 获取玩家排名
  921. /// </summary>
  922. /// <param name="type">榜单类型 0: 世界, 1: 好友</param>
  923. public void GetRank(int type)
  924. {
  925. Debug.Log("GetRank");
  926. var callback = new NativeCallback
  927. {
  928. onCallback = delegate (NativeResponse resp)
  929. {
  930. OnGetRankInner(resp.code, resp.result);
  931. }
  932. };
  933. _jo.Call("getRank", type, callback);
  934. }
  935. /// <summary>
  936. /// 弹出邀请好友列表
  937. /// /**
  938. /// * 获取好友列表
  939. /// * code:
  940. /// * 0: 好友
  941. /// * 1: 我关注的
  942. /// * 2: 我粉丝
  943. /// */
  944. /// <param name="code">类型</param>
  945. /// <param name="info">附加的邀请信息</param>
  946. /// </summary>
  947. public void ShowInviteFriend(int code, string info)
  948. {
  949. Debug.Log("ShowFriend friend: " + code + ", " + info);
  950. _jo.Call("showInviteFriend", code, info);
  951. }
  952. #else
  953. void Start()
  954. {
  955. if (MessageHandler._debug)
  956. {
  957. test();
  958. }
  959. }
  960. #endif
  961. }