将GoogleMaps添加到unity5 [英] Adding GoogleMaps to unity5

查看:177
本文介绍了将GoogleMaps添加到unity5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've created an empty object called Maps and created a child rawimage. This script is assigned to Maps and the child rawimage object has been assigned to the rawimage variable in the inspector.

我玩游戏时,检查器未显示未分配的原始图像,并说对象引用未设置为对象实例".

When I play, the inspector shows no rawimage assigned and says "object reference not set to instance of object".

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());
    }
}

推荐答案

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

In the code you posted, you're never assigning anything to img. So, it is null, which leads to the "object reference not set to instance of object" error.

如果在行上放置断点,

 img.texture = www.texture;

您可能会看到imgnull.

您需要在某个地方创建RawImage并将其分配给img字段.

You need to create a RawImage somewhere and assign it to the img field.

这篇关于将GoogleMaps添加到unity5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆