unity 微信开发小游戏,网络资源获取数据

1.创建数据结构

cs 复制代码
public class JsCode
{
    public string js_code;
}

public class OpenID
{
    public string openid;
}

2.网络请求

cs 复制代码
using System;
using System.Collections;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;

public class HttpManager : MonoBehaviour
{
    public static HttpManager instance;

    private void Awake()
    {
        if (instance == null)
        {
            instance = this;
        }
        else
        {
            Destroy(this);
        }
    }

    /// <summary>
    /// 请求数据
    /// </summary>
    /// <typeparam name="Treq">请求数据类型</typeparam>
    /// <typeparam name="Tres">接收数据类型<</typeparam>
    /// <param name="url">请求地址</param>
    /// <param name="req">请求数据</param>
    /// <param name="callback">回显</param>
    public void GetData<Treq, Tres>(string url, Treq req, Action<Tres> callback) where Treq : class where Tres : class
    {
        StartCoroutine(LoadData<Treq, Tres>(url, req, callback));
    }
    /// <summary>
    /// 请求数据
    /// </summary>
    /// <typeparam name="Treq">请求数据类型</typeparam>
    /// <typeparam name="Tres">接收数据类型</typeparam>
    /// <param name="url">请求地址</param>
    /// <param name="req">请求数据</param>
    /// <param name="callback">回显</param>
    /// <returns></returns>
    IEnumerator LoadData<Treq, Tres>(string url, Treq req, Action<Tres> callback) where Treq : class where Tres : class
    {
        string json = JsonUtility.ToJson(req);
        //创建一个UnityWebRequest对象,这里以POST方法为例
        using (UnityWebRequest request = new UnityWebRequest(url, "POST"))
        {
            //创建一个UploadHandlerRaw对象,并设置内容类型

            Debug.Log("请求地址:" + url);
            Debug.Log("请求json:" + json);
            request.uploadHandler = new UploadHandlerRaw(Encoding.UTF8.GetBytes(json));
            //为请求分配 DownloadHandlerBuffer,用于存储下载数据
            request.downloadHandler = new DownloadHandlerBuffer();
            //设置请求头
            request.SetRequestHeader("content-type", "application/json;charset=utf-8");
            //签名验证采用Bearer Token认证方式
            //request.SetRequestHeader("Authorization", "Bearer YOUR_ACCESS_TOKEN");
            //发送网络请求并等待完成
            yield return request.SendWebRequest();
            //网络有响应并且成功了
            if (request.isDone && request.result == UnityWebRequest.Result.Success)
            {
                Debug.Log("请求数据:" + request.downloadHandler.text);
                Tres res = JsonUtility.FromJson<Tres>(request.downloadHandler.text);
                callback?.Invoke(res);
            }
            else
            {
                Debug.Log("网络请求失败: " + request.error);
                callback?.Invoke(null);
            }

        }

    }


}

3.运用

cs 复制代码
//成功获取到用户Code
JsCode msg = new JsCode();
msg.js_code = e.code;

HttpManager.instance.GetData<JsCode, OpenID>(WXCodeUrl, msg, (res) => {
    Debug.Log("整体接收:" + res);
    Debug.Log("openid:" + res.openid);
    WXOpenID = res.openid;

});
相关推荐
FairGuard手游加固12 小时前
手游加固怎么做?SO、IL2CPP、AssetBundle 与运行时保护详解
安全·游戏·unity
阿松爱学习2 天前
【Unity开发】监听Scene场景事件
unity·c#
xiaogai_gai2 天前
BOM 物料清单同步实战:金蝶云星空 → 四化智造 MES 修改场景深度拆解
unity·游戏引擎
淡海水2 天前
03-03-架构篇-SGen GC架构解析
unity·架构·c#·gc·sgen
/**书香门第*/2 天前
Cocos Creator 打螺丝游戏的实现
游戏引擎·cocos2d·螺丝·打螺丝
萌萌的提莫队长3 天前
Unity相机捕捉,图像识别,抠像+合成
opencv·unity
mxwin4 天前
Unity Shader 光线追踪原理与实战案例
unity·游戏引擎
地狱为王4 天前
Unity使用DPDFNet实现实时语音增强(降噪)
unity·语音增强·降噪·dpdfnet
平行云PVT4 天前
国产信创实时云渲染解决方案:如何实现数字孪生与三维应用云化部署?
unity·webrtc·unreal engine·实时云渲染·国产信创·摩尔线程·砺算科技
夏炎黄4 天前
Terrain地形剪裁
unity