gpt4 book ai didi

将谷歌地图集成到 Unity3D 错误的 C# 脚本

转载 作者:行者123 更新时间:2023-11-30 14:49:20 25 4
gpt4 key购买 nike

我正在尝试通过在我的 map 游戏对象(3D 普通对象)中使用下面的 C# 代码,将 Google map 集成到我的 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)

如以下图片中的更多详细信息:

enter image description here

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

enter image description here

如您所见,在检查器面板中,在 Google map 脚本下,它显示:

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,因为您上传的图像显示您正在使用 Mesh Renderer。

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

关于将谷歌地图集成到 Unity3D 错误的 C# 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38978088/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com