首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有没有办法把webservices json导入unity?

有没有办法把webservices json导入unity?
EN

Stack Overflow用户
提问于 2019-11-21 00:47:56
回答 1查看 70关注 0票数 0

我正在做我的毕业设计,我所需要做的就是做一个数据库,里面有特定物体的照片、信息和3D模型。

使用相机,我可以扫描这个数据库中调用其对象的二维码,并在unity场景中显示照片、信息和3D模型……我看了很多视频,尝试了很多代码,但都没有帮助。

EN

回答 1

Stack Overflow用户

发布于 2019-12-18 14:12:52

如果web服务有开放的REST API,您可以在Unity中使用StartCoroutine、you和UnityWebRequest来调用它们。

您可以首先将SimpleJSON添加到您的项目https://wiki.unity3d.com/index.php/SimpleJSON中,这将帮助您为POST/PUT请求创建JSON数据,并从GET请求中解密JSON响应数据。在这个页面中有一个关于SimpleJSON设置的指南-这只是一个简单的过程,将一个文件复制到你的项目中的一个文件夹。

至于发出调用,这里有一个发出GET请求的示例。试着从这个开始,看看你是否能为你的项目量身定制它。如果你再次陷入困境,在stackoverflow中发布一个新问题,列出你尝试过的代码,以及你试图解决这个问题的方法,社区可以帮助你解决这个问题。祝好运!

代码语言:javascript
复制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using SimpleJSON;

public class myExampleCode : MonoBehaviour
{
    void Start()
    {
        StartCoroutine(
            functionToCallRestAPIs(
                (JSONNode JSONresponse) => 
                {
                    functionToHandleApiResponse(JSONresponse);
                },
            )
        );
    }

    private IEnumerator functionToCallRestAPIs(System.Action<JSONNode> callBack)
    {
        //Get ready to call the request
        string myRequestURL = "https://**********" 
        UnityWebRequest WebRequest = UnityWebRequest.Get(myRequestURL);
        WebRequest.SetRequestHeader("SomeRequestHeader", "SomeHeaderValue");

        //Send the request
        yield return WebRequest.SendWebRequest();

        //Place response into a JSONNode type
        string JSONstring = WebRequest.downloadHandler.text;
        Debug.Log("JSON string contents --> " + JSONstring);
        JSONNode JSONnode = JSON.Parse(JSONstring);

        //Pass on the JSONNode type variable to the callback
        callBack(JSONnode);
    }

    private void functionToHandleApiResponse(JSONNode APIresponse)
    {
        // Example response -> {"version": "1.0","data": {"sampleArray": ["string value",5,{"name": "sub object"}]}}
        Debug.Log("Data inside the response --> " + APIresponse["data"]["sampleArray"][0].Value)
        // Prints a string containing "string value"
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58959467

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档