C#脚本谷歌地图整合到Unity3D错误 [英] C# script to integrate Google map into Unity3D error

查看:789
本文介绍了C#脚本谷歌地图整合到Unity3D错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的地图游戏对象,它是3D平原对象利用这个低于C#代码到谷歌地图整合到我的Unity3D项目:

I am trying to integrate Google map into my Unity3D project by utilising this below C# code in my Map game object which is 3D plain object:

using UnityEngine;
    using System.Collections;

    public class GoogleMap : MonoBehaviour
    {
        public enum MapType
        {
            RoadMap,
            Satellite,
            Terrain,
            Hybrid
        }
        public bool loadOnStart = true;
        public bool autoLocateCenter = true;
        public GoogleMapLocation centerLocation;
        public int zoom = 13;
        public MapType mapType;
        public int size = 512;
        public bool doubleResolution = false;
        public GoogleMapMarker[] markers;
        public GoogleMapPath[] paths;

        void Start() {
            if(loadOnStart) Refresh();
        }

        public void Refresh() {
            if(autoLocateCenter && (markers.Length == 0 && paths.Length == 0)) {
                Debug.LogError("Auto Center will only work if paths or markers are used.");
            }
            StartCoroutine(_Refresh());
        }

        IEnumerator _Refresh ()
        {
            var url = "http://maps.googleapis.com/maps/api/staticmap";
            var qs = "";
            if (!autoLocateCenter) {
                if (centerLocation.address != "")
                qs += "center=" + WWW.UnEscapeURL (centerLocation.address);
                else {
                    qs += "center=" + WWW.UnEscapeURL (string.Format ("{0},{1}", centerLocation.latitude, centerLocation.longitude));
                }

                qs += "&zoom=" + zoom.ToString ();
            }
            qs += "&size=" + WWW.UnEscapeURL (string.Format ("{0}x{0}", size));
            qs += "&scale=" + (doubleResolution ? "2" : "1");
            qs += "&maptype=" + mapType.ToString ().ToLower ();
            var usingSensor = false;
    #if UNITY_IPHONE
            usingSensor = Input.location.isEnabledByUser && Input.location.status == LocationServiceStatus.Running;
    #endif
            qs += "&sensor=" + (usingSensor ? "true" : "false");

            foreach (var i in markers) {
                qs += "&markers=" + string.Format ("size:{0}|color:{1}|label:{2}", i.size.ToString ().ToLower (), i.color, i.label);
                foreach (var loc in i.locations) {
                    if (loc.address != "")
                    qs += "|" + WWW.UnEscapeURL (loc.address);
                    else
                    qs += "|" + WWW.UnEscapeURL (string.Format ("{0},{1}", loc.latitude, loc.longitude));
                }
            }

            foreach (var i in paths) {
                qs += "&path=" + string.Format ("weight:{0}|color:{1}", i.weight, i.color);
                if(i.fill) qs += "|fillcolor:" + i.fillColor;
                foreach (var loc in i.locations) {
                    if (loc.address != "")
                    qs += "|" + WWW.UnEscapeURL (loc.address);
                    else
                    qs += "|" + WWW.UnEscapeURL (string.Format ("{0},{1}", loc.latitude, loc.longitude));
                }
            }

            var req = new WWW (url + "?" + qs);
            yield return req;
            GetComponent().material.mainTexture = req.texture;
        }

    }

    public enum GoogleMapColor
    {
        black,
        brown,
        green,
        purple,
        yellow,
        blue,
        gray,
        orange,
        red,
        white
    }

    [System.Serializable]
    public class GoogleMapLocation
    {
        public string address;
        public float latitude;
        public float longitude;
    }

    [System.Serializable]
    public class GoogleMapMarker
    {
        public enum GoogleMapMarkerSize
        {
            Tiny,
            Small,
            Mid
        }
        public GoogleMapMarkerSize size;
        public GoogleMapColor color;
        public string label;
        public GoogleMapLocation[] locations;

    }

    [System.Serializable]
    public class GoogleMapPath
    {
        public int weight = 5;
        public GoogleMapColor color;
        public bool fill = false;
        public GoogleMapColor fillColor;
        public GoogleMapLocation[] locations;
    }



我发现错误在这条线:

I found error on this line:

GetComponent().material.mainTexture = req.texture;



这表明:

It shows:

Using the generic method `UnityEngine.Component.GetComponent<T>()' requires `1'type argument(s)

正如下面的图片的更多详细信息:

As more details in following pictures:

在Unity3D,还有如下面的图片显示一些错误:

In Unity3D, there are some errors as indicated in following pictures:

正如你所看到的,督察小组,在谷歌地图的脚本,它表明:

As you can see, in Inspector panel, under Google map script, it shows:

The associated script cannot be loaded. Please fix any compile errors and assign a valid script.

和底部时,它表明:

Assets/GoogleMap.cs(79,17): error CS0411: The type arguments for method `UnityEngine.Component.GetComponent<T>()' cannot be inferred from the usage. Try specifying the type arguments explicitly

通过我的Unity3D一点经验,请帮助我如何解决这个错误。先谢谢了。

With my little experience on Unity3D, please help me out on how to solve this error. Thanks in advance.

推荐答案

几乎没有。

替换

GetComponent().material.mainTexture = req.texture;



with

GetComponent<Material>().mainTexture = req.texture;

如果你得到运行时间错误在此行的代码,使用 MeshRenderer 由于图像您上传您使用网渲染节目。

If you get run-time null error on this line of code, the use MeshRenderer since the image you uploaded shows that you are using Mesh Renderer.

GetComponent<MeshRenderer>().material.mainTexture = req.texture;

这篇关于C#脚本谷歌地图整合到Unity3D错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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