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”的空游戏对象上:
我们的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") {