2016-08-16 72 views
3

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

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) 

如以下PICT更多细节数目字:

enter image description here

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

enter image description here

正如你所看到的,在Inspector面板中,在谷歌地图的脚本,它显示:

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上的经验很少,请帮我解决这个错误。提前致谢。

+0

当接受的答案是正确的,请去@Programmer的回答。我欠他一个:) –

+0

@UmairM哈哈,我们只相隔几秒钟,所以我不在乎他跟谁一起回答。假设我们是5分钟或分开,那么是的。 – Programmer

+0

时间不应该确定一个职位的答案。质量 – FrankerZ

回答

3

几乎在那里。

更换

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

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

如果您收到了这行代码运行时null错误,因为您上传您所使用网格渲染器显示的图像使用MeshRenderer

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

非常感谢,特别是关于MeshRenderer的额外部分。它可以帮助我更多。 – SanitLee

2

尝试该行改成这样:

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

这是正确的语法使用GetComponent

+0

不管怎么样,我只需要为@ Programmer's去,因为他在几分钟之前就已经出现在我身边了 – SanitLee

+2

没问题的队友。只要你得到了帮助,它并不重要:) –