unity3d与web api交互

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;
            }
        }
    }
}


展开阅读全文

Git 实用技巧

11-24
这几年越来越多的开发团队使用了Git,掌握Git的使用已经越来越重要,已经是一个开发者必备的一项技能;但很多人在刚开始学习Git的时候会遇到很多疑问,比如之前使用过SVN的开发者想不通Git提交代码为什么需要先commit然后再去push,而不是一条命令一次性搞定; 更多的开发者对Git已经入门,不过在遇到一些代码冲突、需要恢复Git代码时候就不知所措,这个时候哪些对 Git掌握得比较好的少数人,就像团队中的神一样,在队友遇到 Git 相关的问题的时候用各种流利的操作来帮助队友于水火。 我去年刚加入新团队,发现一些同事对Git的常规操作没太大问题,但对Git的理解还是比较生疏,比如说分支和分支之间的关联关系、合并代码时候的冲突解决、提交代码前未拉取新代码导致冲突问题的处理等,我在协助处理这些问题的时候也记录各种问题的解决办法,希望整理后通过教程帮助到更多对Git操作进阶的开发者。 本期教程学习方法分为“掌握基础——稳步进阶——熟悉协作”三个层次。从掌握基础的 Git的推送和拉取开始,以案例进行演示,分析每一个步骤的操作方式和原理,从理解Git 工具的操作到学会代码存储结构、演示不同场景下Git遇到问题的不同处理方案。循序渐进让同学们掌握Git工具在团队协作中的整体协作流程。 在教程中会通过大量案例进行分析,案例会模拟在工作中遇到的问题,从最基础的代码提交和拉取、代码冲突解决、代码仓库的数据维护、Git服务端搭建等。为了让同学们容易理解,对Git简单易懂,文章中详细记录了详细的操作步骤,提供大量演示截图和解析。在教程的最后部分,会从提升团队整体效率的角度对Git工具进行讲解,包括规范操作、Gitlab的搭建、钩子事件的应用等。 为了让同学们可以利用碎片化时间来灵活学习,在教程文章中大程度降低了上下文的依赖,让大家可以在工作之余进行学习与实战,并同时掌握里面涉及的Git不常见操作的相关知识,理解Git工具在工作遇到的问题解决思路和方法,相信一定会对大家的前端技能进阶大有帮助。
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值