12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100 |
- using UnityEngine;
- using UnityEngine.UI;
- using System.Collections;
- using System.Collections.Generic;
- using System;
- using System.Threading.Tasks;
- #if UNITY_IOS
- using AOT;
- using System.Runtime.InteropServices;
- #endif
- [Serializable]
- public class User
- {
- public int id; //用户id
- public string nickname; // 昵称
- public string avatar; // 头像 http地址
- public int gender; // 0: 未设置, 1:男 ,2:女
- public string address; // 地址
- }
- [Serializable]
- public class UserFriend
- {
- public User user; //用户id
- public string status; // 状态 [ONLINE,OFFLINE,GAME]
- public int rank; // 排名
- public int score; // 分数
- }
- public class Response<T>
- {
- public List<T> list;
- }
- enum CMD_MOTION
- {
- MOTION_STOP = 0,
- MOTION_RUN, // 跑
- MOTION_JUMP, // 跳
- MOTION_DOWN, // 蹲
- MOTION_LEFT, // 滑左
- MOTION_RIGHT, // 滑右
- MOTION_FRONT, // 滑前
- MOTION_BACK, // 滑后
- MOTION_LEFT_UP, // 点击-左上
- MOTION_LEFT_DOWN, // 点击-左下
- MOTION_RIGHT_UP, // 点击-右上
- MOTION_RIGHT_DOWN, // 点击-右下
- MOTION_STEP, // 点击-原地踩
- NUMBERS_OF_MOTION,
- MOTION_CANCEL = 0x0100,
- }
- enum BLE_STATE
- {
- STATE_DISCONNECTED = 0,
- STATE_CONNECTING, // 连接中
- STATE_CONNECTED, // 连接成功
- STATE_DISCONNECTING, // 断开连接
- }
- [Serializable]
- public class Shoe
- {
- public string name, address;
- public int status, electricity;
- public int stepLeftStatus, stepLeftFrag, stepRightStatus, stepRightFrag;
- public short lx, ly, lz, rx, ry, rz;
- public int[] cmd = new int[2];
- public int[] motion = new int[4 * 2];
- public bool Valid(int ts)
- {
- return (DateTime.Now.Millisecond - ts) >= (1000 / Application.targetFrameRate);
- }
- public int GetCmd()
- {
- if (Valid(cmd[1]))
- {
- return cmd[0];
- }
- return -1;
- }
- public int GetMotionLeft()
- {
- if (Valid(motion[1]))
- {
- return motion[0];
- }
- return -1;
- }
- public int GetMotionRight()
- {
- if (Valid(motion[3]))
- {
- return motion[2];
- }
- return -1;
- }
- }
- class NativeResponse
- {
- public int code;
- public string result;
- }
- class NativeCallback : AndroidJavaProxy
- {
- public NativeCallback() : base("com.ouj.shoe.sdklibrary.interface.IntergrationInterface") { }
- public void Callback(int code, string result)
- {
- Debug.Log(result);
- NativeResponse resp = new NativeResponse
- {
- code = code,
- result = result
- };
- onCallback(resp);
- }
- public Action<NativeResponse> onCallback;
- }
- public class MessageHandler : MonoBehaviour
- {
- private static bool _debug = true;
- private Transform left, right;
- private Transform cameraTransform;
- private static Vector3 lv = new Vector3(0, 0, 0), rv = new Vector3(0, 0, 0);
- private static Quaternion lqo = new Quaternion(0, 0, 0, 0), rqo = new Quaternion(0, 0, 0, 0);
- private static Quaternion lq = new Quaternion(0, 0, 0, 0), rq = new Quaternion(0, 0, 0, 0);
- private static Text montionTextPos, montionTextAtt, montionTextAttConver, montionTextStep;
- private static int motionLeftCode_0 = -1, motionRightCode_0 = -1;
- private static int motionLeftCode_1 = -1, motionRightCode_1 = -1;
- private static Text textDeviceOne, textDeviceOneStatus, textDeviceOneElectricity;
- private static Text textDeviceTwo, textDeviceTwoStatus, textDeviceTwoElectricity;
- private static Text textGetUserFriends;
- private static Text textGameMode;
- private static Text textDeviceOneMotion, textDeviceTwoMotion;
- private static Response<UserFriend> friends;
- private static string _name_0, _address_0;
- private static int _status_0, _electricity_0;
- private static string _name_1, _address_1;
- private static int _status_1, _electricity_1;
- private static bool _gameMode = false;
- private static int _leftStatus, _leftFrag, _rightStatus, _rightFrag;
- private static short _lx, _ly, _lz, _rx, _ry, _rz;
- private static Shoe[] shoes = new Shoe[2];
- /**
- * 获取鞋子数据信息
- */
- public Shoe GetShoe(int id) { return shoes[id]; }
- /**
- * 测试 或 使用方法参考
- *
- *
- */
- void test()
- {
- Debug.Log("test start");
- GameObject.Find("btnGameStart").GetComponent<Button>().onClick.AddListener(delegate ()
- {
- Debug.Log("click GameStart!");
- GameStart();
- });
- GameObject.Find("btnGameEnd").GetComponent<Button>().onClick.AddListener(delegate () { Debug.Log("click GameEnd!"); GameEnd(1, 1, 1, 1, 0); });
- GameObject.Find("btnGetUser").GetComponent<Button>().onClick.AddListener(delegate ()
- {
- Debug.Log("click GetUserInfo!");
- User user = GetUserInfo();
- GameObject.Find("textGetUserInfo").GetComponent<Text>().text = JsonUtility.ToJson(user);
- });
- GameObject.Find("btnGetUserFriends").GetComponent<Button>().onClick.AddListener(delegate ()
- {
- Debug.Log("click GetUserFriends!");
- GetUserFriends(0);
- });
- GameObject.Find("btnDeviceOneConnect").GetComponent<Button>().onClick.AddListener(delegate () { Debug.Log("click SearchDevice!"); SearchDevice(0); });
- GameObject.Find("btnDeviceTwoConnect").GetComponent<Button>().onClick.AddListener(delegate () { Debug.Log("click SearchDevice!"); SearchDevice(1); });
- GameObject.Find("btnInvite").GetComponent<Button>().onClick.AddListener(delegate ()
- {
- Debug.Log("click InviteFriend!");
- string input = GameObject.Find("InputUserFirend").GetComponent<InputField>().text.Trim();
- if (input.Length == 0)
- {
- return;
- }
- InviteFriend(int.Parse(input), "123456");
- });
- GameObject.Find("btnGetRank").GetComponent<Button>().onClick.AddListener(delegate () { Debug.Log("click btnGetRank!"); GetRank(0); });
- GameObject.Find("btnGetRankFriend").GetComponent<Button>().onClick.AddListener(delegate () { Debug.Log("click btnGetRankFriend!"); GetRank(1); });
- GameObject MontionLeft = GameObject.Find("MontionLeft");
- GameObject MontionRight = GameObject.Find("MontionRight");
- if (MontionRight != null)
- {
- right = MontionRight.GetComponent<Renderer>().transform;
- }
- if (MontionLeft != null)
- {
- left = MontionLeft.GetComponent<Renderer>().transform;
- }
- GameObject MainCamera = GameObject.Find("Main Camera");
- Debug.Log("test MainCamera: " + MainCamera);
- if (MainCamera != null)
- {
- cameraTransform = MainCamera.GetComponent<Camera>().transform;
- }
- GameObject.Find("MotionButton").GetComponent<Button>().onClick.AddListener(delegate ()
- {
- if (left != null)
- {
- lv = new Vector3(-1, 0, 50);
- lq = new Quaternion(0, 0, 0, 0);
- }
- if (right != null)
- {
- rv = new Vector3(1, 0, 50);
- rq = new Quaternion(0, 0, 0, 0);
- }
- if (cameraTransform != null)
- {
- cameraTransform.position = new Vector3(0, 0, -10);
- }
- GameObject.Find("SliderX").GetComponent<Slider>().value = 0;
- GameObject.Find("SliderY").GetComponent<Slider>().value = 0;
- montionTextAttConver.text = string.Format("{0:F2}, {1:F2}, {2:F2} ---- {3:F2}, {4:F2}, {5:F2}", 0, 0, 0, 0, 0, 0);
- });
- GameObject.Find("SliderX").GetComponent<Slider>().onValueChanged.AddListener((float value) =>
- {
- if (cameraTransform != null)
- {
- cameraTransform.Translate(new Vector3(value - cameraTransform.position.x, 0, 0));
- }
- });
- GameObject.Find("SliderY").GetComponent<Slider>().onValueChanged.AddListener((float value) =>
- {
- if (cameraTransform != null)
- {
- cameraTransform.Translate(new Vector3(0, value - cameraTransform.position.y, 0));
- }
- });
- montionTextPos = GameObject.Find("MontionTextPos").GetComponent<Text>();
- montionTextAtt = GameObject.Find("MontionTextAtt").GetComponent<Text>();
- montionTextAttConver = GameObject.Find("MontionTextAttConver").GetComponent<Text>();
- textDeviceOne = GameObject.Find("textDeviceOne").GetComponent<Text>();
- textDeviceOneStatus = GameObject.Find("textDeviceOneStatus").GetComponent<Text>();
- textDeviceOneElectricity = GameObject.Find("textDeviceOneElectricity").GetComponent<Text>();
- textDeviceTwo = GameObject.Find("textDeviceTwo").GetComponent<Text>();
- textDeviceTwoStatus = GameObject.Find("textDeviceTwoStatus").GetComponent<Text>();
- textDeviceTwoElectricity = GameObject.Find("textDeviceTwoElectricity").GetComponent<Text>();
- montionTextStep = GameObject.Find("MontionTextStep").GetComponent<Text>();
- textGetUserFriends = GameObject.Find("textGetUserFriends").GetComponent<Text>();
- textGameMode = GameObject.Find("textGameMode").GetComponent<Text>();
- textDeviceOneMotion = GameObject.Find("textDeviceOneMotion").GetComponent<Text>();
- textDeviceTwoMotion = GameObject.Find("textDeviceTwoMotion").GetComponent<Text>();
- Debug.Log("test montionTextPos: " + montionTextPos);
- Debug.Log("test montionTextAtt: " + montionTextAtt);
- Debug.Log("test montionTextAttConver: " + montionTextAttConver);
- Debug.Log("test textDeviceOne: " + textDeviceOne);
- Debug.Log("test textDeviceOneStatus: " + textDeviceOneStatus);
- Debug.Log("test textDeviceOneElectricity: " + textDeviceOneElectricity);
- Debug.Log("test textDeviceTwo: " + textDeviceTwo);
- Debug.Log("test textDeviceTwoStatus: " + textDeviceTwoStatus);
- Debug.Log("test textDeviceTwoElectricity: " + textDeviceTwoElectricity);
- Debug.Log("test montionTextStep: " + montionTextStep);
- Debug.Log("test textGetUserFriends: " + textGetUserFriends);
- Debug.Log("test textDeviceOneMotion: " + textDeviceOneMotion);
- Debug.Log("test textDeviceTwoMotion: " + textDeviceTwoMotion);
- Debug.Log("test end");
- }
- private void Awake()
- {
- Screen.sleepTimeout = SleepTimeout.NeverSleep;
- shoes = new Shoe[2];
- for (int i = 0; i < shoes.Length; i++)
- {
- shoes[i] = new Shoe();
- }
- }
- void Update()
- {
- if (MessageHandler._debug)
- {
- if (right != null)
- {
- right.position = rv;
- right.rotation = rq;
- }
- if (left != null)
- {
- left.position = lv;
- left.rotation = lq;
- }
- if(textGameMode != null)
- textGameMode.text = "游戏模式:" + _gameMode;
- if (montionTextAtt != null)
- montionTextPos.text = _lx + ", " + _ly + ", " + _lz + " ---- " + _rx + ", " + _ry + ", " + _rz;
- if (montionTextAtt != null)
- montionTextAtt.text = lqo.x + ", " + lqo.y + ", " + lqo.z + " ---- " + rqo.x + ", " + rqo.y + ", " + rqo.z;
- if (montionTextAttConver != null)
- 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);
- if (montionTextStep != null)
- montionTextStep.text = string.Format("{0},{1} ,,,, {2},{3}", _leftStatus, _leftFrag, _rightStatus, _rightFrag);
- if (textDeviceOne != null)
- textDeviceOne.text = _name_0;
- if (textDeviceOneStatus != null)
- textDeviceOneStatus.text = "" + Enum.GetName(typeof(BLE_STATE), _status_0);
- if (textDeviceOneMotion != null)
- textDeviceOneMotion.text = Enum.GetName(typeof(CMD_MOTION), motionLeftCode_0) + " | " + Enum.GetName(typeof(CMD_MOTION), motionRightCode_0);
- if (textDeviceOneElectricity != null)
- textDeviceOneElectricity.text = "" + _electricity_0;
- if (textDeviceTwo != null)
- textDeviceTwo.text = _name_1;
- if (textDeviceTwoStatus != null)
- textDeviceTwoStatus.text = "" + Enum.GetName(typeof(BLE_STATE), _status_1);
- if (textDeviceTwoMotion != null)
- textDeviceTwoMotion.text = Enum.GetName(typeof(CMD_MOTION), motionLeftCode_1) + " | " + Enum.GetName(typeof(CMD_MOTION), motionRightCode_1);
- if (textDeviceTwoElectricity != null)
- textDeviceTwoElectricity.text = "" + _electricity_1;
- if (friends != null)
- if (textGetUserFriends != null)
- textGetUserFriends.text = JsonUtility.ToJson(friends);
- }
- }
- static void GameModeInner(bool mode)
- {
- Debug.Log("gameMode : " + mode);
- if (MessageHandler._debug)
- {
- _gameMode = mode;
- }
- }
- static void ActionInner(int id, int code)
- {
- Debug.LogFormat("action: {0:D} {1:D}", id, code);
- Shoe shoe = shoes[id];
- shoe.cmd[0] = code;
- shoe.cmd[1] = DateTime.Now.Millisecond;
- // @see CMD_MOTION
- //MOTION_STOP = 0, 取消
- //MOTION_LEFT, // 左
- //MOTION_RIGHT, // 右
- //MOTION_FRONT, // 上
- //MOTION_BACK, // 下
- //MOTION_STEP, // 确定
- }
- static void MotionInner(int id, int left, int right)
- {
- Debug.LogFormat("motion id: {0:D}, left: {1:D}, right: {2:D}", id, left, right);
- if (MessageHandler._debug)
- {
- if(id == 0)
- {
- if (left > -1 && motionLeftCode_0 != left)
- {
- motionLeftCode_0 = left;
- }
- if (right > -1 && motionRightCode_0 != right)
- {
- motionRightCode_0 = right;
- }
- }
- else
- {
- if (left > -1 && motionLeftCode_1 != left)
- {
- motionLeftCode_1 = left;
- }
- if (right > -1 && motionRightCode_1 != right)
- {
- motionRightCode_1 = right;
- }
- }
-
- }
- // 是否跳
- bool jump = left == (int)CMD_MOTION.MOTION_JUMP || right == (int)CMD_MOTION.MOTION_JUMP;
- // 是否蹲
- bool squat = left == (int)CMD_MOTION.MOTION_DOWN || right == (int)CMD_MOTION.MOTION_DOWN;
- int[] code = { left, right };
- int now = DateTime.Now.Millisecond;
- Shoe shoe = shoes[id];
- if(left > 0)
- {
- shoe.motion[0] = left;
- shoe.motion[1] = now;
- }
- if(right > 0)
- {
- shoe.motion[2] = right;
- shoe.motion[3] = now;
- }
-
- // 动作分发处理
- for (int i = 0; i < code.Length; i++)
- {
- int cmd = code[i];
- if (cmd < 0)
- continue;
- // i:0 = 左鞋动作
- // i:1 = 右鞋动作
- switch (cmd)
- {
- case (int)CMD_MOTION.MOTION_STOP:
- break;
- case (int)CMD_MOTION.MOTION_RUN:
- break;
- case (int)CMD_MOTION.MOTION_JUMP:
- break;
- case (int)CMD_MOTION.MOTION_DOWN:
- break;
- case (int)CMD_MOTION.MOTION_LEFT:
- break;
- case (int)CMD_MOTION.MOTION_RIGHT:
- break;
- case (int)CMD_MOTION.MOTION_FRONT:
- break;
- case (int)CMD_MOTION.MOTION_BACK:
- break;
- case (int)CMD_MOTION.MOTION_LEFT_UP:
- break;
- case (int)CMD_MOTION.MOTION_LEFT_DOWN:
- break;
- case (int)CMD_MOTION.MOTION_RIGHT_UP:
- break;
- case (int)CMD_MOTION.MOTION_RIGHT_DOWN:
- break;
- case (int)CMD_MOTION.MOTION_STEP:
- break;
- case (int)CMD_MOTION.MOTION_LEFT_UP | (int)CMD_MOTION.MOTION_CANCEL: // 取消 - 左上
- break;
- case (int)CMD_MOTION.MOTION_LEFT_DOWN | (int)CMD_MOTION.MOTION_CANCEL: // 取消 - 左下
- break;
- case (int)CMD_MOTION.MOTION_RIGHT_UP | (int)CMD_MOTION.MOTION_CANCEL: // 取消 - 右上
- break;
- case (int)CMD_MOTION.MOTION_RIGHT_DOWN | (int)CMD_MOTION.MOTION_CANCEL: // 取消 - 右下
- break;
- case (int)CMD_MOTION.MOTION_STEP | (int)CMD_MOTION.MOTION_CANCEL: // 取消 - 中间
- break;
- }
- }
- }
- static void StepInner(int id, int leftStatus, int rightStatus, int leftFrag, int rightFrag)
- {
- Debug.LogFormat("step id: {0:D}, leftStatus: {1:D}, rightStatus: {2:D}, leftFrag: {3:D}, rightFrag: {4:D}", id, leftStatus, rightStatus, leftFrag, rightFrag);
- if (MessageHandler._debug && id == 0)
- {
- _leftStatus = leftStatus;
- _leftFrag = leftFrag;
- _rightStatus = rightStatus;
- _rightFrag = rightFrag;
- }
- Shoe shoe = shoes[id];
- shoe.stepLeftFrag = leftFrag;
- shoe.stepLeftStatus = leftStatus;
- shoe.stepRightFrag = rightFrag;
- shoe.stepRightStatus = rightStatus;
- }
- static void TranslateInner(int id, short lx, short ly, short lz, short rx, short ry, short rz)
- {
- //Debug.Log("translate id: " + id);
- if (MessageHandler._debug && id == 0)
- {
- _lx = lx;
- _ly = ly;
- _lz = lz;
- _rx = rx;
- _ry = ry;
- _rz = rz;
- rv = new Vector3(-rx / 10f + 1, -ry / 10f, -rz / 10f + 50);
- lv = new Vector3(lx / 10f - 1, ly / 10f, -lz / 10f + 50);
- }
- }
- static void RotateInner(int id, short lx, short ly, short lz, short rx, short ry, short rz)
- {
- //Debug.Log("rotate id: " + id);
- if (MessageHandler._debug && id == 0)
- {
- rqo = new Quaternion(rx, ry, rz, 0);
- lqo = new Quaternion(lx, ly, lz, 0);
- rq = new Quaternion(-rx / 10000f * Mathf.Rad2Deg, -ry / 10000f * Mathf.Rad2Deg, -rz / 10000f * Mathf.Rad2Deg, 0);
- lq = new Quaternion(-lx / 10000f * Mathf.Rad2Deg, -ly / 10000f * Mathf.Rad2Deg, -lz / 10000f * Mathf.Rad2Deg, 0);
- }
- }
- static void DeviceInner(int id, string name, string address, int status, int electricity)
- {
- Debug.Log("device id: " + id + ", name: " + name + ", address: " + address + ", status: " + status + ", electricity: " + electricity);
- if (MessageHandler._debug)
- {
- if (id == 0)
- {
- _name_0 = name;
- _address_0 = address;
- _status_0 = status;
- _electricity_0 = electricity;
- }
- else if (id == 1)
- {
- _name_1 = name;
- _address_1 = address;
- _status_1 = status;
- _electricity_1 = electricity;
- }
- }
- Shoe shoe = shoes[id];
- if (!address.Equals(shoe.address))
- {
- shoe = new Shoe
- {
- name = name,
- address = address,
- status = status,
- electricity = electricity
- };
- shoes[id] = shoe;
- }
-
- }
- static void UserFriendsInner(int code, string json)
- {
- Debug.Log("userFriends Response<UserFriend> code: " + code + " -> " + json);
- if (MessageHandler._debug)
- {
- string jsonStr = "{ \"list\": " + json + "}";
- friends = JsonUtility.FromJson<Response<UserFriend>>(jsonStr);
- }
- if (code == 0)
- {
- // 显示列表
- }
- }
- static void InviteFriendInner(int code, string userJson, string inviteInfo)
- {
- Debug.Log("InviteFriendInner code: " + code + " -> " + userJson + ", " + inviteInfo);
- string json = userJson;
- if (json != null)
- {
- User user = JsonUtility.FromJson<User>(json);
- if(inviteInfo != null) //
- {
- }
- }
- }
- static void OnGetRankInner(int code, string json)
- {
- Debug.Log("OnGetRankInner -> " + json);
- Response<UserFriend> resp = JsonUtility.FromJson<Response<UserFriend>>(json);
- Debug.Log("GetRank resp: " + resp.list.Count);
- //for(int i = 0; i<resp.list.Count; i++)
- //{
- // UserFriend item = resp.list[i];
- // User user = item.user;
- // Debug.Log("GetRank user: " + user.nickname + ", " + user.avatar + ", score: " + item.score );
- //}
- }
- #if UNITY_IOS
- // IOS TO U3D start =================================
- [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
- public delegate void MotionHandler(int id, int left, int right);
- [MonoPInvokeCallback(typeof(MotionHandler))]
- static void motionHandler(int id, int left, int right)
- {
- MotionInner(id, left, right);
- }
- [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
- public delegate void ActionHandler(int id, int code);
- [MonoPInvokeCallback(typeof(ActionHandler))]
- static void actionHandler(int id, int code)
- {
- ActionInner(id, code);
- }
- [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
- public delegate void StepHandler(int id, int leftStatus, int rightStatus, int leftFrag, int rightFrag);
- [MonoPInvokeCallback(typeof(StepHandler))]
- static void stepHandler(int id, int leftStatus, int rightStatus, int leftFrag, int rightFrag)
- {
- StepInner(id, leftStatus, rightStatus, leftFrag, rightFrag);
- }
- [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
- public delegate void DeviceHandler(int id, string name, string address, int status, int electricity);
- [MonoPInvokeCallback(typeof(DeviceHandler))]
- static void deviceHandler(int id, string name, string address, int status, int electricity)
- {
- DeviceInner(id, name, address, status, electricity);
- }
- [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
- public delegate void UserFriendsHandler(int code, string json);
- [MonoPInvokeCallback(typeof(UserFriendsHandler))]
- static void userFriendsHandler(int code, string json)
- {
- UserFriendsInner(code, json);
- }
- [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
- public delegate void InviteFriendHandler(int code, string userJson, string inviteInfo);
- [MonoPInvokeCallback(typeof(InviteFriendHandler))]
- static void inviteFriendHandler(int code, string userJson, string inviteInfo)
- {
- InviteFriendInner(code, userJson, inviteInfo);
- }
- [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
- public delegate void GetRankHandler(int code, string userJson);
- [MonoPInvokeCallback(typeof(GetRankHandler))]
- static void getRankHandler(int code, string userJson)
- {
- OnGetRankInner(code, userJson);
- }
- // U3D TO IOS start =================================
- [DllImport("__Internal")]
- static extern void GameStart();
- [DllImport("__Internal")]
- static extern void GameEnd(int level, double score, int record, int mode, int opponentId);
- [DllImport("__Internal")]
- static extern string GetUserInfoJson();
- [DllImport("__Internal")]
- static extern void GetUserFriends(int code);
- [DllImport("__Internal")]
- static extern void SearchDevice(int type);
- [DllImport("__Internal")]
- static extern void ConnectDevice(int type);
-
- [DllImport("__Internal")]
- static extern void DisConnectDevice(int type);
- [DllImport("__Internal")]
- static extern void InviteFriend(int friendId, string info);
- [DllImport("__Internal")]
- static extern void GetInviteInfo();
- [DllImport("__Internal")]
- static extern void Vibrate(int type, int duration);
-
- [DllImport("__Internal")]
- static extern void ScreenProjection();
-
- [DllImport("__Internal")]
- static extern void ShowInviteFriend(int code, string info);
- [DllImport("__Internal")]
- static extern void GetRank(int type);
- [DllImport("__Internal")]
- static extern void PointerActionHandler(IntPtr resultHandler);
- [DllImport("__Internal")]
- static extern void PointerMotionHandler(IntPtr resultHandler);
- [DllImport("__Internal")]
- static extern void PointerStepHandler(IntPtr resultHandler);
- [DllImport("__Internal")]
- static extern void PointerDeviceHandler(IntPtr resultHandler);
- [DllImport("__Internal")]
- static extern void PointerUserFriendsHandler(IntPtr resultHandler);
- [DllImport("__Internal")]
- static extern void PointerInviteFriendHandler(IntPtr resultHandler);
-
- [DllImport("__Internal")]
- static extern void PointerGetRankHandler(IntPtr resultHandler);
- public User GetUserInfo()
- {
- string json = GetUserInfoJson();
- if (json != null)
- {
- return JsonUtility.FromJson<User>(json);
- }
- return null;
- }
- void Start()
- {
- PointerActionHandler(Marshal.GetFunctionPointerForDelegate(new ActionHandler(actionHandler)));
- PointerMotionHandler( Marshal.GetFunctionPointerForDelegate(new MotionHandler(motionHandler)));
- PointerStepHandler(Marshal.GetFunctionPointerForDelegate(new StepHandler(stepHandler)));
- PointerDeviceHandler( Marshal.GetFunctionPointerForDelegate(new DeviceHandler(deviceHandler)));
- PointerUserFriendsHandler(Marshal.GetFunctionPointerForDelegate(new UserFriendsHandler(userFriendsHandler)));
- PointerInviteFriendHandler(Marshal.GetFunctionPointerForDelegate(new InviteFriendHandler(inviteFriendHandler)));
- PointerGetRankHandler(Marshal.GetFunctionPointerForDelegate(new GetRankHandler(getRankHandler)));
- if (MessageHandler._debug)
- {
- test();
- }
- }
- #elif UNITY_ANDROID
- /**
- * id: 设备类型
- * 0: 主设备
- * 1: 副设备
- * 2:
- * 3:
- */
- private class MessageChannel : AndroidJavaProxy
- {
- const int MOTION_CANCEL = 0x0100;
- public MessageChannel() : base("com.ouj.shoe.sdklibrary.MessageChannel")
- {
- }
- /**
- * mode: 游戏模式开头
- * 可忽略处理
- */
- public void gameMode(bool mode)
- {
- GameModeInner(mode);
- }
- /**
- * code: 操作代码
- * 对应 enum CMD_MOTION
- */
- public void action(int id, int code)
- {
- ActionInner(id, code);
- }
- /**
- * left: 左鞋动作代码
- * right: 右鞋动作代码
- * 对应 enum CMD_MOTION
- */
- public void motion(int id, int left, int right)
- {
- MotionInner(id, left, right);
- }
- /**
- * 踏步状态,频率
- * leftStatus: 左鞋(0:停止, 1:慢,2:快)
- * rightStatus: 右鞋(0:停止, 1:慢,2:快)
- * leftFrag: 左鞋(每分钟步频)
- * rightFrag: 右鞋(每分钟步频)
- */
- public void step(int id, int leftStatus, int rightStatus, int leftFrag, int rightFrag)
- {
- StepInner(id, leftStatus, rightStatus, leftFrag, rightFrag);
- }
- /**
- * array: x,y坐标数据
- * 单位:cm
- */
- public void translate(int id, short lx, short ly, short lz, short rx, short ry, short rz)
- {
- TranslateInner(id, lx, ly, lz, rx, ry, rz);
- }
- /**
- * array: x,y,z姿态数据
- * 单位:弧度
- * 使用时要将数据 除以 10000
- * 转换角度可以使用 Mathf.Rad2Deg
- */
- public void rotate(int id, short lx, short ly, short lz, short rx, short ry, short rz)
- {
- RotateInner(id, lx, ly, lz, rx, ry, rz);
- }
- /**
- * 返回蓝牙设备的基本信息,用于界面上显示当前的设备状况
- *
- * name: 设备名称
- * status: 设备状态 @see enum BLE_STATE
- * electricity: 设备电量 取值范围[0 - 100]
- */
- public void device(int id, string name, string address, int status, int electricity)
- {
- DeviceInner(id, name, address, status, electricity);
- }
- /**
- * 调用api GetUserFriends() 的回调
- *
- * code: 请求结果代码 0: 成功, 非0: 根据业务处理
- * json: 好友列表数据 , json 数组
- *
- */
- public void userFriends(int code, string json)
- {
- UserFriendsInner(code, json);
- }
- /**
- * 接收到邀请的回调
- *
- * code: 请求结果代码 0: 成功, 非0: 根据业务处理
- * user: json 结构
- * inviteInfo: 发起的信息
- *
- */
- public void onInviteFriend(int code, string user, string inviteInfo)
- {
- InviteFriendInner(code, user, inviteInfo);
- }
- }
- private AndroidJavaClass _jc;
- private AndroidJavaObject _jo;
- // Start is called before the first frame update
- void Start()
- {
- try
- {
- // 注入通信接口
- Debug.Log("sdk init start!");
- _jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
- _jo = _jc.GetStatic<AndroidJavaObject>("currentActivity");
- MessageChannel channel = new MessageChannel();
- _jo.Call("setMessageChannel", channel);
- if (MessageHandler._debug)
- {
- GameObject.Find("textLinkSdk").GetComponent<Text>().text = "" + channel;
- }
- }
- catch (Exception e)
- {
- Debug.Log("sdk init error! " + e);
- }
- if (MessageHandler._debug)
- {
- test();
- }
- }
- /// <summary>
- /// 游戏开发商获取信息接口
- /// 获取用户基本信息,请开发商根据返回的信息创建玩家资料,做到sdk和游戏内玩家信息一致
- /// @see User
- /// </summary>
- public User GetUserInfo()
- {
- Debug.Log("GetUserInfo");
- string json = _jo.Call<string>("getUserInfo");
- if (json != null)
- {
- return JsonUtility.FromJson<User>(json);
- }
- return null;
- }
- /// <summary>
- /// 游戏开发商获取信息接口
- /// 请求好友列表,结果会在MessageChannel中回调
- /// @see MessageChannel.userFriends(code, json)
- /// @see UserFriend
- /// </summary>
- public void GetUserFriends(int code)
- {
- Debug.Log("GetUserFriends");
- _jo.Call("getUserFriends", code);
- }
- /// <summary>
- /// 游戏开发商事件
- /// 请在每局游戏开始调用, 通知SDK开始记录
- /// motion事件要调用GameStart()后才有回调
- /// </summary>
- public void GameStart()
- {
- Debug.Log("GameStart");
- _jo.Call("gameStart");
- }
- /// <summary>
- /// 游戏开发商事件
- /// 请在每局游戏结束后, 调用通知SDK上传对战信息,并添加相关参数
- /// </summary>
- /// <param name="level">游戏难度</param>
- /// <param name="score">游戏评分</param>
- /// <param name="record">游戏战绩,0: 负, 1: 胜</param>
- /// <param name="mode">游戏模式,0: 单人, 1: 匹配, 2: 好友对战</param>
- /// <param name="opponentId">房间ID或对手用户ID,可为 0</param>
- public void GameEnd(int level, double score, int record, int mode, int opponentId)
- {
- Debug.Log("GameEnd");
- _jo.Call("gameEnd", level, score, record, mode, opponentId);
- }
- /// <summary>
- /// 游戏开发商事件
- /// 重新连接蓝牙设备
- /// 当蓝牙设备异常,无法连接时可重新连接蓝牙设备
- /// </summary>
- /// <param name="type">设备类型 0: 主, 1: 副</param>
- public void ConnectDevice(int type)
- {
- Debug.Log("ConnectDevice type: " + type);
- _jo.Call("connectDevice", type);
- }
- /// <summary>
- /// 游戏开发商事件
- /// 当蓝牙设备异常,无法连接时可重新连接蓝牙设备
- /// </summary>
- /// <param name="address">设备地址:可更换设备,前提是设备不在连接状态</param>
- /// <param name="type">设备类型 0: 主, 1: 副</param>
- public void ConnectDevice(string address, int type)
- {
- Debug.Log("ConnectDevice type: " + type + ", " + address);
- _jo.Call("connectDevice", address, type);
- }
- /// <summary>
- /// 游戏开发商事件
- /// 断开已连接连接的设备
- /// </summary>
- /// <param name="type">设备类型 0: 主, 1: 副</param>
- public void DisConnectDevice(int type)
- {
- Debug.Log("disConnectDevice type: " + type);
- _jo.Call("disConnectDevice", type);
- }
- /// <summary>
- /// 游戏开发商事件
- /// 弹出搜索蓝牙设备的对话框
- /// </summary>
- /// <param name="type">设备类型 0: 主, 1: 副,连接后的设备绑定为相应的类型,单人模式请用 0 </param>
- public void SearchDevice(int type)
- {
- Debug.Log("SearchDevice type: " + type);
- _jo.Call("searchDevice", type);
- }
- /// <summary>
- /// 游戏开发商事件
- /// 邀请好友
- /// </summary>
- /// <param name="friendId">用户ID, sdk发送邀请信息给对应的好友</param>
- /// <param name="info">邀请的信息,文本信息一直带给好友,在onInviteFriend(User, string)中传入</param>
- /// @see UserFriend.User.id
- public void InviteFriend(int friendId, string info)
- {
- Debug.Log("InviteFriend friend: " + friendId + ", " + info);
- _jo.Call("inviteFriend", friendId, info);
- }
-
- /// <summary>
- /// 界面准备好后可以申请邀请信息
- /// </summary>
- public void GetInviteInfo()
- {
- Debug.Log("GetInviteInfo");
- _jo.Call("getInviteInfo");
- }
- /// <summary>
- /// 游戏开发商事件
- /// 搜索可投屏的设备
- /// </summary>
- public void ScreenProjection()
- {
- _jo.Call("screenProjection");
- }
- /// <summary>
- /// 游戏开发商事件
- /// 震动事件
- /// </summary>
- /// <param name="type">设备类型 0: 主, 1: 副,连接后的设备绑定为相应的类型,单人模式请用 0 </param>
- /// <param name="duration">时长 ms [100 .. 1000]</param>
- public void Vibrate(int type, int duration)
- {
- _jo.Call("vibrate", type, duration);
- }
- /// <summary>
- /// 获取玩家排名
- /// </summary>
- /// <param name="type">榜单类型 0: 世界, 1: 好友</param>
- public void GetRank(int type)
- {
- Debug.Log("GetRank");
- var callback = new NativeCallback
- {
- onCallback = delegate (NativeResponse resp)
- {
- OnGetRankInner(resp.code, resp.result);
- }
- };
- _jo.Call("getRank", type, callback);
- }
- /// <summary>
- /// 弹出邀请好友列表
- /// /**
- /// * 获取好友列表
- /// * code:
- /// * 0: 好友
- /// * 1: 我关注的
- /// * 2: 我粉丝
- /// */
- /// <param name="code">类型</param>
- /// <param name="info">附加的邀请信息</param>
- /// </summary>
- public void ShowInviteFriend(int code, string info)
- {
- Debug.Log("ShowFriend friend: " + code + ", " + info);
- _jo.Call("showInviteFriend", code, info);
- }
- #else
- void Start()
- {
- if (MessageHandler._debug)
- {
- test();
- }
- }
- #endif
- }
|