我正在做我的毕业设计,我所需要做的就是做一个数据库,里面有特定物体的照片、信息和3D模型。
使用相机,我可以扫描这个数据库中调用其对象的二维码,并在unity场景中显示照片、信息和3D模型……我看了很多视频,尝试了很多代码,但都没有帮助。
发布于 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中发布一个新问题,列出你尝试过的代码,以及你试图解决这个问题的方法,社区可以帮助你解决这个问题。祝好运!
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"
}
}https://stackoverflow.com/questions/58959467
复制相似问题