88 lines
3.1 KiB
C#
88 lines
3.1 KiB
C#
|
using System;
|
|||
|
using System.Collections;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Text;
|
|||
|
// using TalesFromTheRift;
|
|||
|
using UnityEngine;
|
|||
|
using UnityEngine.Networking;
|
|||
|
|
|||
|
public class MyIMEApi : MonoBehaviour
|
|||
|
{
|
|||
|
public static MyIMEApi instance;
|
|||
|
|
|||
|
private void Awake()
|
|||
|
{
|
|||
|
instance = this;
|
|||
|
}
|
|||
|
|
|||
|
public void SendApi(string inkData, DrawOnTexture keyboard)
|
|||
|
{
|
|||
|
StartCoroutine(Upload(inkData, keyboard));
|
|||
|
}
|
|||
|
|
|||
|
private string GetJson(string inkData)
|
|||
|
{
|
|||
|
//return "{\"options\": \"enable_pre_space\",\"requests\": [{\"writing_guide\":{\"writing_area_width\": 280,\"writing_area_height\": 280},\"max_num_results\": 10,\"max_completions\": 0,\"language\": \"zh\",\"ink\": [[[53, 53, 55, 58, 61, 65, 71, 77, 89, 97, 101, 110, 118, 126, 135, 142, 149, 156, 162, 168, 173, 177, 182, 186, 190, 192, 195, 197, 200, 201, 202, 203, 204, 205, 205, 205, 206, 206, 207, 209, 210, 212, 214, 217, 219, 221, 223, 225, 226, 227, 228, 229, 230, 230, 230, 231, 231, 231],[153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 152, 152, 152, 152, 152, 152, 152, 152, 152, 152, 152, 152, 152, 152, 153, 153, 153, 153, 153, 153, 153, 153, 153, 154, 154, 154, 154, 154, 155, 155, 155, 155, 155, 155, 155, 155, 155, 155, 155, 155],[]]]}]}";
|
|||
|
string json =
|
|||
|
"{\"options\": \"enable_pre_space\",\"requests\": [{\"writing_guide\":{\"writing_area_width\": 1080,\"writing_area_height\": 460},\"max_num_results\": 1,\"max_completions\": 0,\"language\": \"zh\",\"ink\": [[";
|
|||
|
json += inkData;
|
|||
|
json += "[]]]}]}";
|
|||
|
return json;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
const string basePath = "https://inputtools.google.com/request?ime=handwriting";
|
|||
|
|
|||
|
IEnumerator Upload(string inkData, DrawOnTexture keyboard)
|
|||
|
{
|
|||
|
string json = GetJson(inkData);
|
|||
|
Debug.Log(json);
|
|||
|
|
|||
|
var request = new UnityWebRequest(basePath, "POST");
|
|||
|
request.uploadHandler = (UploadHandler)new UploadHandlerRaw(Encoding.UTF8.GetBytes(json));
|
|||
|
request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
|
|||
|
request.SetRequestHeader("Content-Type", "application/json");
|
|||
|
yield return request.SendWebRequest();
|
|||
|
Debug.Log(request.downloadHandler.text);
|
|||
|
//keyboard.SendKeyString(ProcessJson(request.downloadHandler.text));
|
|||
|
|
|||
|
string processJson = "";
|
|||
|
try
|
|||
|
{
|
|||
|
processJson = ProcessJson(request.downloadHandler.text);
|
|||
|
}
|
|||
|
catch (Exception e)
|
|||
|
{
|
|||
|
processJson = "";
|
|||
|
Debug.Log(e);
|
|||
|
}
|
|||
|
|
|||
|
Debug.Log(processJson);
|
|||
|
keyboard.DoneLoadingDestroyStrokes(processJson);
|
|||
|
|
|||
|
//keyboard.DoneLoadingDestroyStrokes(ProcessJson(request.downloadHandler.text));
|
|||
|
}
|
|||
|
|
|||
|
private string ProcessJson(string json)
|
|||
|
{
|
|||
|
return json[34].ToString();
|
|||
|
}
|
|||
|
|
|||
|
public int GetNthIndex(string s, char t, int n)
|
|||
|
{
|
|||
|
int count = 0;
|
|||
|
for (int i = 0; i < s.Length; i++)
|
|||
|
{
|
|||
|
if (s[i] == t)
|
|||
|
{
|
|||
|
count++;
|
|||
|
if (count == n)
|
|||
|
{
|
|||
|
return i;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
return -1;
|
|||
|
}
|
|||
|
}
|