unity connect to server
时间: 2023-12-22 12:05:37 浏览: 47
To connect Unity to a server, you can use the UnityWebRequest API. Here is a sample code snippet that demonstrates how to connect to a server using UnityWebRequest:
```csharp
IEnumerator GetRequest(string url)
{
using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
{
// Send the request
yield return webRequest.SendWebRequest();
// Check for errors
if (webRequest.result != UnityWebRequest.Result.Success)
{
Debug.Log(webRequest.error);
}
else
{
// Print the response
Debug.Log(webRequest.downloadHandler.text);
}
}
}
```
To use this code, simply call the GetRequest function with the URL of the server you want to connect to. This will send a GET request to the server and print the response to the Unity console. You can modify this code to send other types of requests (e.g. POST, PUT, DELETE) as well.