繁体   English   中英

C#脚本将Google地图整合到Unity3D中

[英]C# script to integrate Google map into Unity3D error

我正在尝试将Google地图集成到我的Unity3D项目中,在我的地图游戏对象中使用以下C#代码,这是3D普通对象:

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

我发现这行错误:

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

表明:

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

如下图中的更多细节:

在此输入图像描述

在Unity3D中,存在一些错误,如下图所示:

在此输入图像描述

如您所见,在Inspector面板中,在Google地图脚本下,它显示:

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上的经验,请帮助我解决如何解决此错误。 提前致谢。

差不多了。

更换

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

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

如果你被运行时null上这行代码错误,使用MeshRenderer由于图像您上传您使用网渲染节目。

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

尝试将该行更改为:

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

这是使用GetComponent正确语法

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM