unity3D与web api交互最大的用处,除了下载各种资源以外,大概就是可以记录一些简单的数据。如果只是做个简单的打飞机的游戏,想记录下最高成绩的话,用web api足够了,而且,web的资源丰富,改起来快。
unity3D访问web api关键点就以下2处
1、UnityWebRequest:这个方法包含了所有的内容,替代就的WWW的那个类。
2、如果存在登录,需要在登录以后将传回来的Set-Cookie存下来,下次请求的时候包含在头部用Cookie发出去。
这个项目已经传到了github,地址:https://github.com/wuyt/RCSelesia
另外,写了这个测试用的web api,asp.net core写的,地址:https://github.com/wuyt/RCOhanashi
另外,因为对跨站攻击的有些内容不很熟悉,所以,没有考虑跨站攻击的问题。
直接上代码
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class WebApi : MonoBehaviour
{
string cookie;
public InputField inputRegisterEmail;
public InputField inputRegisterPassword;
public InputField inputLoginEmail;
public InputField inputLoginPassword;
public InputField inputGetID;
public InputField inputPostName;
public InputField inputPostNumber;
public InputField inputPutID;
public InputField inputPutName;
public InputField inputPutNumber;
public InputField inputDeleteID;
// Use this for initialization
void Start()
{
cookie = "";
}
public void Register()
{
StartCoroutine(IERegister());
}
IEnumerator IERegister()
{
using (UnityWebRequest www = new UnityWebRequest())
{
www.url = "http://localhost:2572/api/account/register";
www.method = UnityWebRequest.kHttpVerbPOST;
www.SetRequestHeader("Cookie", cookie);
byte[] bodyRaw = Encoding.UTF8.GetBytes("{\"Email\": \"" + inputRegisterEmail.text + "\",\"password\": \"" + inputRegisterPassword.text + "\",\"ConfirmPassword\": \"" + inputRegisterPassword.text + "\"}");
www.uploadHandler = new UploadHandlerRaw(bodyRaw);
www.SetRequestHeader("Content-Type", "application/json");
www.downloadHandler = new DownloadHandlerBuffer();
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
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;
}
}
}
public void Login()
{
StartCoroutine(IELogin());
}
IEnumerator IELogin()
{
using (UnityWebRequest www = new UnityWebRequest())
{
www.url = "http://localhost:2572/api/account/login";
www.method = UnityWebRequest.kHttpVerbPOST;
www.SetRequestHeader("Cookie", cookie);
byte[] bodyRaw = Encoding.UTF8.GetBytes("{\"Email\": \"" + inputLoginEmail.text + "\",\"password\": \"" + inputLoginPassword.text + "\",\"remeberme\": false }");
www.uploadHandler = new UploadHandlerRaw(bodyRaw);
www.SetRequestHeader("Content-Type", "application/json");
www.downloadHandler = new DownloadHandlerBuffer();
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
// Show results as text
Debug.Log(www.downloadHandler.text);
cookie = www.GetResponseHeader("Set-Cookie");
Debug.Log(cookie);
// Or retrieve results as binary data
//byte[] results = www.downloadHandler.data;
}
}
}
public void LoginOut()
{
StartCoroutine(IELoginOut());
}
IEnumerator IELoginOut()
{
using (UnityWebRequest www = new UnityWebRequest())
{
www.url = "http://localhost:2572/api/account/loginOut";
www.method = UnityWebRequest.kHttpVerbGET;
www.SetRequestHeader("Cookie", cookie);
www.downloadHandler = new DownloadHandlerBuffer();
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
// Show results as text
Debug.Log(www.downloadHandler.text);
cookie = www.GetResponseHeader("Set-Cookie");
Debug.Log(cookie);
// Or retrieve results as binary data
//byte[] results = www.downloadHandler.data;
}
}
}
public void AllAnimes()
{
StartCoroutine(IEAllAnimes());
}
IEnumerator IEAllAnimes()
{
using (UnityWebRequest www = new UnityWebRequest())
{
www.url = "http://localhost:2572/api/Animes";
www.method = UnityWebRequest.kHttpVerbGET;
www.SetRequestHeader("Cookie", cookie);
www.downloadHandler = new DownloadHandlerBuffer();
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
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;
}
}
}
public void Anime()
{
StartCoroutine(IEAnime());
}
IEnumerator IEAnime()
{
using (UnityWebRequest www = new UnityWebRequest())
{
www.url = "http://localhost:2572/api/Animes/" + inputGetID.text;
www.method = UnityWebRequest.kHttpVerbGET;
www.SetRequestHeader("Cookie", cookie);
www.downloadHandler = new DownloadHandlerBuffer();
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
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;
}
}
}
public void PostAnime()
{
StartCoroutine(IEPostAnime());
}
IEnumerator IEPostAnime()
{
using (UnityWebRequest www = new UnityWebRequest())
{
www.url = "http://localhost:2572/api/animes";
www.method = UnityWebRequest.kHttpVerbPOST;
www.SetRequestHeader("Cookie", cookie);
byte[] bodyRaw = Encoding.UTF8.GetBytes("{\"Name\": \"" + inputPostName.text + "\",\"Number\": \"" + inputPostNumber.text + "\",\"DateTime\": \"2011 - 08 - 01T00: 00:00\",\"IndexShow\":false}");
www.uploadHandler = new UploadHandlerRaw(bodyRaw);
www.SetRequestHeader("Content-Type", "application/json");
www.downloadHandler = new DownloadHandlerBuffer();
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
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;
}
}
}
public void PutAnime()
{
StartCoroutine(IEPutAnime());
}
IEnumerator IEPutAnime()
{
using (UnityWebRequest www = new UnityWebRequest())
{
www.url = "http://localhost:2572/api/animes/" + inputPutID.text;
www.method = UnityWebRequest.kHttpVerbPUT;
www.SetRequestHeader("Cookie", cookie);
byte[] bodyRaw = Encoding.UTF8.GetBytes("{\"ID\":" + inputPutID.text + ",\"Name\": \"" + inputPutName.text + "\",\"Number\": \"" + inputPutNumber.text + "\",\"DateTime\": \"2011 - 08 - 01T00: 00:00\",\"IndexShow\":false}");
www.uploadHandler = new UploadHandlerRaw(bodyRaw);
www.SetRequestHeader("Content-Type", "application/json");
www.downloadHandler = new DownloadHandlerBuffer();
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
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;
}
}
}
public void DeleteAnime()
{
StartCoroutine(IEDeleteAnime());
}
IEnumerator IEDeleteAnime()
{
using (UnityWebRequest www = new UnityWebRequest())
{
www.url = "http://localhost:2572/api/Animes/" + inputDeleteID.text;
www.method = UnityWebRequest.kHttpVerbDELETE;
www.SetRequestHeader("Cookie", cookie);
www.downloadHandler = new DownloadHandlerBuffer();
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
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;
}
}
}
}