iot和unity_Web API和Unity中的IoT

这篇博客介绍了如何将物联网(IoT)数据整合到Unity游戏中,通过连接到OpenWeatherMap API动态改变天空盒的纹理以反映真实天气。此外,还展示了如何使用Spark Core的光传感器数据控制Unity场景中的光照强度,实现实时交互。通过这样的技术,游戏可以根据现实世界的光照和天气条件做出反应,增加了沉浸感。
摘要由CSDN通过智能技术生成

iot和unity

For me, the Internet of Things is most exciting when you look at just how far reaching the capabilities are. There is so much technology out there, with new devices emerging all the time, that can benefit from connectivity to the web. Over the next few months at SitePoint, I’ll be looking at the various possibilities that the Internet of Things can bring, reviewing a different platform or device in each article. In the previous article, we looked at how to pull in data from the Jawbone Up.

对我来说,物联网是最令人兴奋的,只要您看一下这些功能的作用范围。 那里有太多的技术,并且新技术一直在涌现,可以从与网络的连接中受益。 在接下来的几个月中,我将在SitePoint上探讨物联网可以带来的各种可能性,并在每篇文章中回顾不同的平台或设备。 在上一篇文章中,我们研究了如何从Jawbone Up中提取数据。

This time around, we’ll be bringing IoT data into Unity, a widely used games engine that is used to build everything from iOS and Android games to console and Facebook games. It is also emerging as an engine that’ll be used on the Oculus Rift, Gear VR and more, so it’s a fun one to try pairing with the IoT. Imagine a game that adapts to real world conditions like weather and light, if it suddenly goes dark in your room, the game world suddenly goes dark too!

这次,我们将把物联网数据引入Unity,这是一种广泛使用的游戏引擎,用于构建从iOS和Android游戏到游戏机和Facebook游戏的所有内容。 它也正在成为一种引擎,将在Oculus Rift,Gear VR等平台上使用,因此尝试与IoT配对很有趣。 想象一下一款能够适应现实世界条件(例如天气和光照)的游戏,如果它突然在您的房间变暗,那么游戏世界也会突然变暗!

To start with, we’re going to connect up a Unity scene to a weather API, allowing us to use real-world weather data in our virtual world. Then we’ll connect up a Spark Core to use its light sensor data.

首先,我们将Unity场景连接到天气API,从而使我们能够在虚拟世界中使用真实的天气数据。 然后,我们将连接一个Spark Core以使用其光传感器数据。

In this article, we’re assuming that you know all about setting up a scene in Unity, with a skybox, terrain and lighting. We’ll be building from a scene that has all of this ready and set up. My code samples will be in C# but it is possible to do the same sorts of things in UnityScript if you’d prefer.

在本文中,我们假设您了解有关在Unity中设置天空,地形和照明的场景的全部知识。 我们将从一个已经准备好所有这些场景的场景中构建。 我的代码示例将使用C#,但是如果您愿意,也可以在UnityScript中执行相同的操作。

下载演示代码 (Download the Demo Code)

Working demo code for those who’d like to see this in action is available here.

想要看到实际效果的人可以在这里使用工作演示代码。

连接到天气 (Connecting Up to the Weather)

Our first example of bringing in real world data will be setting up a skybox to change texture depending on the weather. Our sky is going to change to reflect the weather in our real world city! We’ll use OpenWeatherMap to pull in the latest weather data into Unity.

我们引入现实世界数据的第一个示例将是设置一个天空盒,以根据天气改变纹理。 我们的天空将发生变化,以反映现实世界中的天气! 我们将使用OpenWeatherMap将最新的天气数据提取到Unity中。

In order to control our scene’s Skybox, we’ll attach a script called IoTSkybox to an Empty Game Object called “SkyboxController”:

为了控制场景的Skybox,我们将名为IoTSkybox的脚本IoTSkybox到名为“ SkyboxController”的空游戏对象上:

New Skybox Controller

我们的Skybox代码 (Our Skybox Code)

In IoTSkybox.c, we include the following code:

IoTSkybox.c ,我们包含以下代码:

using UnityEngine;
using System.Collections;

public class IoTSkybox : MonoBehaviour {
    public Material clearSky;
    public Material cloudySky;
    
    IEnumerator AdjustSkyToWeather() {
        while (true) {
            string weatherUrl = "http://api.openweathermap.org/data/2.5/weather?zip=2000,au";
            
            WWW weatherWWW = new WWW (weatherUrl);
            yield return weatherWWW;
            
            JSONObject tempData = new JSONObject (weatherWWW.text);
            
            JSONObject weatherDetails = tempData["weather"];
            string WeatherType = weatherDetails[0]["main"].str;
            
            if (WeatherType == "Clear") {
           
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值