You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
170 lines
3.2 KiB
170 lines
3.2 KiB
|
|
using UnityEngine;
|
|
using System;
|
|
using System.Runtime.InteropServices;
|
|
|
|
namespace EduCoderTool
|
|
{
|
|
/*
|
|
* web端通讯中转器
|
|
* @author lz
|
|
**/
|
|
public class WebConnecter : MonoBehaviour
|
|
{
|
|
|
|
private static WebConnecter instance = null;
|
|
/// <summary>
|
|
/// 单例
|
|
/// </summary>
|
|
public static WebConnecter Singleton
|
|
{
|
|
get
|
|
{
|
|
if(instance == null)
|
|
{
|
|
instance = GameObject.FindObjectOfType<WebConnecter>();
|
|
if(instance == null)
|
|
{
|
|
GameObject go = new GameObject("WebConnecter");
|
|
instance = go.AddComponent<WebConnecter>();
|
|
}
|
|
}
|
|
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<string> 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();
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 接收web端发送数据
|
|
/// </summary>
|
|
/// <param name="jsonStr"></param>
|
|
public void RecData(string jsonStr)
|
|
{
|
|
dataHandle?.Invoke(jsonStr);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 接收开始
|
|
/// </summary>
|
|
public void RecStart()
|
|
{
|
|
startHandle?.Invoke();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 接收暂停
|
|
/// </summary>
|
|
public void RecPause()
|
|
{
|
|
pauseHandle?.Invoke();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 接收恢复暂停
|
|
/// </summary>
|
|
public void RecUnPause()
|
|
{
|
|
unPauseHandle?.Invoke();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 接收结束
|
|
/// </summary>
|
|
public void RecFinish()
|
|
{
|
|
finishHandle?.Invoke();
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 发送数据到web端
|
|
/// </summary>
|
|
/// <param name="data"></param>
|
|
public void SendDataToWeb(string data)
|
|
{
|
|
Debug.LogFormat(">>>>>> unity send web data{0}",data);
|
|
#if UNITY_EDITOR
|
|
|
|
#else
|
|
|
|
SendMsgToWeb(data);
|
|
#endif
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|