リアルでは出歩かないので、ゲームをリアルに近づけることにした。
ということで
TerrainにGoogleMap貼り付けて人形歩かせよって思った。
やり方
1.テレインを作成2.下記のコードをスクリプトにコピペ
3.スクリプトを適当なゲームオブジェクトに追加
4.実行
using UnityEngine;
using System.Collections;
using System;
public class TerrainScript : MonoBehaviour {
string Url = @"http://maps.googleapis.com/maps/api/staticmap?size=512x512&maptype=satellite&visible=41.832423,140.823471|41.828586,140.821347&sensor=false";
// Use this for initialization
void Start () {
StartCoroutine(Download(this.Url, tex => addSplatPrototype(tex)));
}
// Update is called once per frame
void Update () {
}
///
/// GoogleMapsAPIから地図画像をダウンロードする
///
/// ダウンロード元
/// ダウンロード後に実行されるコールバック関数
IEnumerator Download(string url, Action callback)
{
var www = new WWW(url);
yield return www; // Wait for download to complete
callback(www.texture);
}
///
/// テレインにテクスチャを貼り付ける
///
///
public void addSplatPrototype(Texture2D tex)
{
var splatPrototypes = Terrain.activeTerrain.terrainData.splatPrototypes;
int index = 0;
SplatPrototype[] newSplatPrototypes = new SplatPrototype[index + 1];
for (int i = 0; i <= index; i++)
{
newSplatPrototypes[i] = new SplatPrototype();
if (i == index)
{
newSplatPrototypes[i].texture = tex;
newSplatPrototypes[i].tileSize = new Vector2(512, 512);
}
else
{
newSplatPrototypes[i].texture = splatPrototypes[i].texture;
newSplatPrototypes[i].tileSize = splatPrototypes[i].tileSize;
}
}
splatPrototypes = newSplatPrototypes;
Terrain.activeTerrain.terrainData.splatPrototypes = splatPrototypes;
}
}
GoogleMapからダウンロードする画像サイズとテレインのタイルサイズを合わせないと画像がきれいにでなかったような。あとテレイン自体のサイズも合わせた方がいんだけど、スクリプトからの指定方法がわかんなかったからやってない。
書いてみたら割と簡単だったけど、テレインをスクリプトから弄るサンプルがあんまりなくて大変だったな。外人さんは多言語対応しようと思わないのかな。
ところでTerrainの読み方ってテレインであってるのかな?