Unity中的REST(从网络服务器发送消息到统一)
有没有办法让一个简单的网络服务器向Unity发送消息?
目前,我们正在使用更新方法中的UnityWebRequest.Get()
进行GET
。 这是代码:
// Update is called once per frame void Update () { StartCoroutine(GetData()); } IEnumerator GetData() { UnityWebRequest uwr = UnityWebRequest.Get(url); yield return uwr.Send(); if (uwr.isError) { Debug.Log(uwr.error); }else { Debug.Log((float.Parse(uwr.downloadHandler.text) / 100)); fnumber = ((float.Parse(uwr.downloadHandler.text) / 100)); transform.position.Set(oldX, fnumber, oldZ); } }
但是,这会抛出此错误:
无法解决目的地主机问题
我发现这个错误报告,它说,它本来是固定的,但似乎并非如此。
那么,有没有办法让服务器向Unity发送消息?
谢谢
扩展我的评论更具描述性和具体性,我的建议是创建一个简单的Thread
,作为服务器的通信管理器。
首先,您必须实现此类以在Unity的UI线程上调用方法。
实现这个时,只需创建一个类:
public class CommunicationManager { static readonly object syncRoot = new object(); static CommunicationManager _Instance; public static CommunicationManager Instance { get { if ( _Instance == null ) { lock ( syncRoot ) { if ( _Instance == null ) { _Instance = new CommunicationManager(); } } } return _Instance; } } volatile bool working = false; Queue _messages; private CommunicationManager() { _messages = new Queue (); InitializeCommunication(); } void InitializeCommunication() { Thread t = new Thread(CommunicationLoop); t.Start(); } void CommunicationLoop() { Socket s = null; try { Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); s.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1337)); working = true; } catch(Exception ex) { working = false; } while ( working ) { lock ( syncRoot ) { while ( _messages.Count > 0 ) { Message message = _messages.Dequeue(); MessageResult result = message.Process(s); result.Notify(); } } Thread.Sleep(100); } } public void EnqueueMessage(Message message) { lock ( syncRoot ) { _messages.Enqueue( message ); } } }
现在你必须创建Message
和MessageResult
对象:
public class Message { const string REQUEST_SCHEME = "{0} {1} HTTP/1.1rnHost: {hostname}rnContent-Length: 0rnrn"; string request; Action whenCompleted; public Message(string requestUri, string requestType, Action whenCompleted) { request = string.Format(REQUEST_SCHEME, requestType, requestUri); this.whenCompleted = whenCompleted; } public MessageResult Process(Socket s) { IPEndPoint endPoint = (IPEndPoint)s.RemoteEndPoint; IPAddress ipAddress = endPoint.Address; request = request.Replace("{hostname}", ipAddress.ToString()); s.Send(Encoding.UTF8.GetBytes(request)); // receive header here which should look somewhat like this : /* HTTP/1.1 Date: Accept-Ranges: bytes Content-Length: Content-Type: */ // when you receive this all that matters is int contentLength = ; byte[] msgBuffer = new byte[contentLength]; if (s.Receive(msgBuffer) != contentLength ) { return null; } return new MessageResult(msgBuffer, whenCompleted); } }
最后创建MessageResult
对象:
public class MessageResult { Action notifier; byte[] messageBuffer; public MessageResult(byte[] message, Action notifier) { notifier = notifier; messageBuffer = message; } public void Notify() { UnityThread.executeInUpdate(() => { notifier(this); }); } }
此方法将从您的主应用程序中运行,以便不会产生任何冻结等等。
要使用它,你可以调用这样的东西:
Message message = new Message("/index.html", "GET", IndexDownloaded); CommunicationManager.Instance.EnqueueMessage(message); // ... public void IndexDownloaded(MessageResult result) { // result available here }
有关更多信息,请阅读此维基百科有关HTTP的文章。
这是从RESTful服务器获取请求的简单方法:D。
private string m_URL = "https://jsonblob.com/api/d58d4507-15f7-11e7-a0ba-014f05ea0ed4"; IEnumerator Start () { var webRequest = new WWW (m_URL); yield return webRequest; if (webRequest.error != null) { Debug.Log (webRequest.error); } else { Debug.Log (webRequest.text); } }
尝试使用WWW
调用而不是破坏的UnityWebRequest
。 您使用什么版本的统一?
上述就是C#学习教程:Unity中的REST(从网络服务器发送消息到统一)分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1003521.html