将GoogleMaps添加到unity5

时间:2018-03-26 11:44:45

标签: c# unity3d

我创建了一个名为Maps的空对象并创建了一个子rawimage。此脚本已分配给Maps,并且子rawimage对象已分配给检查器中的rawimage变量。

当我玩的时候,检查员没有显示分配的原始图像,并且说'#34;对象引用没有设置为对象的实例"。

using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class GoogleAPI : MonoBehaviour {

    public string url;
    public RawImage img;
    public float lon = 3.7533f;
    public float lat = 53.3047f;
    public int zoom;
    public int mapHeight;
    public int mapWidth;
    public int scale;
    LocationInfo li;
    public enum mapType { roadMap, satelite, hybrid, terrain };
    public mapType mapSelected;

    private IEnumerator Map() {
        url = "https://maps.googleapis.com/maps/api/staticmap?center=" + lat + "," + lon +
            "&zoom=" + zoom + "&size=" + mapHeight + "x" + mapWidth + "&Scale=" + scale
            + "&maptype=" + mapSelected +
            "&markers=color:blue%7Clabel:S%7C40.702147,-74.015794&markers=color:green%7Clabel:G%7C40.711614," +
            "-74.012318&markers=color:red%7Clabel:C%7C40.718217,-73.998284&key=AIzaSyDh1_nS-l7nWOFWvt0Gg9-9dY_11qWzK_Q";
        WWW www = new WWW(url);
        yield return www;
        img.texture = www.texture;
        img.SetNativeSize();
    }

    private void Start() {
        img = gameObject.GetComponent<RawImage>();
        StartCoroutine(Map());
    }
}

1 个答案:

答案 0 :(得分:0)

在您发布的代码中,您永远不会向img分配任何内容。因此,它是null,这导致&#34;对象引用未设置为对象的实例&#34;错误。

如果在线上放置断点,

 img.texture = www.texture;

您可能会发现imgnull

您需要在某处创建一个RawImage并将其分配给img字段。