using UnityEngine; using System; using System.Runtime.InteropServices; namespace EduCoderTool { /* * web端通讯中转器 * @author lz **/ public class WebConnecter : MonoBehaviour { private static WebConnecter instance = null; /// /// 单例 /// public static WebConnecter Singleton { get { if(instance == null) { instance = GameObject.FindObjectOfType(); if(instance == null) { GameObject go = new GameObject("WebConnecter"); instance = go.AddComponent(); } } return instance; } } //引用jslib的方法 [DllImport("__Internal")] private static extern void SendMsgToWeb(string str); //开始 public Action startHandle; //暂停 public Action pauseHandle; //恢复暂停 public Action unPauseHandle; //结束 public Action finishHandle; //接收数据 public Action dataHandle; void Awake() { if(instance == null) { instance = this; }else { Destroy(gameObject); } } void Start() { } void Update() { if (Input.GetKeyDown(KeyCode.O)) { RecData("test data test data"); } if (Input.GetKeyDown(KeyCode.S)) { RecStart(); } if (Input.GetKeyDown(KeyCode.P)) { RecPause(); } if (Input.GetKeyDown(KeyCode.R)) { RecUnPause(); } if (Input.GetKeyDown(KeyCode.F)) { RecFinish(); } } /// /// 接收web端发送数据 /// /// public void RecData(string jsonStr) { dataHandle?.Invoke(jsonStr); } /// /// 接收开始 /// public void RecStart() { startHandle?.Invoke(); } /// /// 接收暂停 /// public void RecPause() { pauseHandle?.Invoke(); } /// /// 接收恢复暂停 /// public void RecUnPause() { unPauseHandle?.Invoke(); } /// /// 接收结束 /// public void RecFinish() { finishHandle?.Invoke(); } /// /// 发送数据到web端 /// /// public void SendDataToWeb(string data) { Debug.LogFormat(">>>>>> unity send web data{0}",data); #if UNITY_EDITOR #else SendMsgToWeb(data); #endif } } }