将Google地图集成到Unity3D中的C#脚本错误
我正在尝试将谷歌地图整合到我的Unity3D项目中,方法是在我的地图游戏对象(3D普通对象)中使用以下C#代码:将Google地图集成到Unity3D中的C#脚本错误,c#,google-maps,unity3d,C#,Google Maps,Unity3d,我正在尝试将谷歌地图整合到我的Unity3D项目中,方法是在我的地图游戏对象(3D普通对象)中使用以下C#代码: using UnityEngine; using System.Collections; public class GoogleMap : MonoBehaviour { public enum MapType { RoadMap, Satellite,
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)
而底部向下,则显示:
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
Assets/GoogleMap.cs(79,17):错误CS0411:无法从用法推断方法“UnityEngine.Component.GetComponent()”的类型参数。尝试显式指定类型参数
以我在Unity3D上的一点经验,请帮助我解决这个错误。提前谢谢。快到了
替换
GetComponent().material.mainTexture = req.texture;
与
尝试将该行更改为:
GetComponent<Material>().mainTexture = req.texture;
GetComponent().mainTexture=req.texture;
这是使用
GetComponent
的正确语法。在接受正确答案时,请选择@Programmer的答案。我欠他一个人情:)@UmairM-lol我们相隔几秒钟,所以我不在乎他用哪一个答案。假设我们相隔5分钟,那么是的。时间不应该决定帖子的答案。Quality@FrankerZ是的,但两者都是一样的,应该能解决他的问题。只是我们两人同时打字。^更正:你早了8秒。无论如何,谢谢你,我只需要选择@Programmer's,因为他在我看来只提前了几秒。没问题,伙计。只要你得到了帮助,这并不重要:)谢谢,特别是关于Mesh渲染器的额外文章。这对我帮助更大。
GetComponent<MeshRenderer>().material.mainTexture = req.texture;
GetComponent<Material>().mainTexture = req.texture;