使用C#从Unity中的REST Api检索数据而不会出现滞后或抖动 [英] Retrieving data from REST Api in Unity without lag or jitter using C#
问题描述
每5秒钟我使用C#脚本调用一次REST.但是问题是应用程序每10秒钟延迟一次(InvokeRepeating),然后等待服务器响应,然后在延迟之后给出结果.
Every 5 secs I am calling the REST using C# script. But the problem is the application lags every 10 secs(InvokeRepeating) and waits for server response and then gives out the result after the lag.
如何克服这个问题?我需要实时访问特定的行,以便可以毫无延迟地检索更改.
How to overcome this problem ?? I need to access a specific row in realtime so that changes is retrieved without any lag.
以下代码段用于读取REST Api
The Following code-snippet is used to read REST Api
using UnityEngine;
using System.Collections;
using System.Net;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public class Rows {
public string description ;
public float Efficiency;
public bool IsConnected;
public string Lastconnection;
public float MTBF;
public float MTTR;
public string Name;
public float Speed;
public float TemperatureSetPoint;
}
public class GetData : MonoBehaviour
{
string url="http:LocalHost/Data/Properties/";
string username="Administrator";
string password="admin";
string rt;
void Start () {
JToken tok = CallWeb();
}
public JToken CallWeb()
{
{
var httpRequest = (HttpWebRequest)WebRequest.Create (url);
httpRequest.ContentType = "application/json";
string encoded = System.Convert.ToBase64String (System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username+":"+password));
httpRequest.Headers.Add ("Authorization","Basic "+encoded);
httpRequest.Accept = "application/json";
using (HttpWebResponse response = (HttpWebResponse)httpRequest.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
rt = reader.ReadToEnd();
JObject data = JObject.Parse(rt);
JToken token = (data["rows"][0]);
return token;
}
}
}
}
从下面的代码中,我访问所需的行,并统一使用它.
From the below code I access the required row and use it in unity.
using UnityEngine;
using System.Collections;
using Newtonsoft.Json.Linq;
using UnityEngine.UI;
public class GetPropertyValues : MonoBehaviour
{
GetData thing = new GetData ();
public GameObject CountText;
public GameObject TempText;
static string CountValue1;
static string CountValue2;
static string TempValue1;
static string TempValue2;
void Start()
{
JToken token = thing.CallWeb ();
foreach (JProperty prop in token)
{
if (prop.Name == "ScrapCount")
{
CountValue1 = prop.Value.ToString ();
CountText.GetComponent<TextMesh> ().text = "Count= "+CountValue1.ToString() ;
Debug.Log ("This is the Count value" + prop.Value);
InvokeRepeating ("countDisplay", 5, 10);
}
if (prop.Name == "OvenTemperature")
{
TempValue1 = prop.Value.ToString ();
TempText.GetComponent<TextMesh> ().text = TempValue1.ToString () + "'C";
Debug.Log ("This is the Temp value" + prop.Value);
InvokeRepeating ("tempDisplay", 5, 10);
}
}
}
void countDisplay()
{
JToken token = thing.CallThingWorx ();
foreach (JProperty prop in token) {
if (prop.Name == "ScrapCount") {
CountValue2 = prop.Value.ToString ();
if (CountValue1 == CountValue2) {
Debug.Log ("count value is still " + CountValue1);
}
else if (CountValue1 != CountValue2) {
CountText.GetComponent<TextMesh> ().text = "Count= " + CountValue2.ToString();
Debug.Log ("count value Changed" + CountValue2);
}
}
}
}
void tempDisplay()
{
JToken token = thing.CallWeb ();
foreach (JProperty prop in token)
{
if (prop.Name == "OvenTemperature")
{
TempValue2 = prop.Value.ToString ();
if (TempValue1 == TempValue2)
{
Debug.Log ("Temp value is still " + TempValue1);
}
else if (TempValue1 != TempValue2)
{
TempText.GetComponent<TextMesh> ().text = TempValue2.ToString() +"'C";
Debug.Log ("Temp value Changed" + TempValue2);
}
}
}
}
}
推荐答案
您似乎正在执行同步请求.尝试使用Unity提供的功能
you seem to be doing a synchronous request. Try to use what Unity provides
https://docs.unity3d.com/Manual/UnityWebRequest.html
class MyBehaviour: public MonoBehaviour {
void Start() {
StartCoroutine(GetText());
}
IEnumerator GetText() {
UnityWebRequest www = UnityWebRequest.Get("http://www.my-server.com");
yield return www.Send();
if(www.isError) {
Debug.Log(www.error);
}
else {
// Show results as text
Debug.Log(www.downloadHandler.text);
// Or retrieve results as binary data
byte[] results = www.downloadHandler.data;
}
}
// Edit here
StartCoroutine(GetText());
}
这将使您的应用程序在获取数据时保持运行状态. else语句是下载完成后将要执行的操作.
This will keep your app running while fetching the data. The else statement is where your action will be once the download is done.
这篇关于使用C#从Unity中的REST Api检索数据而不会出现滞后或抖动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!