我开始为Android编写学校游戏作为家庭作业。这是一个包含图像和文本的拖放游戏,您必须将文本拖到相应的图像上。问题是,当我尝试使用网页上的http请求下载图像时,它可以完美地统一工作。但是,如果我将应用程序作为apk部署在android上并安装,则图像似乎无法加载。 我认为要求和我拍照的方式可能有问题,但是我不确定,这是我第一次统一开发游戏。 另外,我不知道如何突出显示我的代码,因此,抱歉它的外观。
IEnumerator GetTexture()
{
string uri = "http://134.209.234.39/games/";
WebRequest request = WebRequest.Create(uri);
WebResponse response = request.GetResponse();
Regex regex = new Regex("<a href=\".*\">(?<name>.*.jpg)</a>");
List<string> links = new List<string>();
int numberofLinks = 0;
using (var reader = new StreamReader(response.GetResponseStream()))
{
string result = reader.ReadToEnd();
MatchCollection matches = regex.Matches(result);
if (matches.Count == 0)
{
Debug.Log("parse failed.");
}
foreach (Match match in matches)
{
if (!match.Success) { continue; }
links.Add((match.Groups["name"]).ToString());
}
}
for (int i = 0; i <5; i++)
{
Random rnd = new Random();
int x = Random.Range(0, links.Count);
UnityWebRequest www = UnityWebRequestTexture.GetTexture("http://134.209.234.39/games/"+links[x]);
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
Texture2D myTexture = ((DownloadHandlerTexture)www.downloadHandler).texture;
if (i == 0) { setImage(firstPhoto, myTexture); firstName.GetComponentInChildren<Text>().text = links[x].Substring(0, links[x].Length - 4); }
else if (i == 1) { setImage(secondPhoto, myTexture); secondName.GetComponentInChildren<Text>().text = links[x].Substring(0, links[x].Length - 4); }
else if (i == 2) { setImage(thirdPhoto, myTexture); thirdName.GetComponentInChildren<Text>().text = links[x].Substring(0,links[x].Length-4); }
else if (i == 3) { setImage(fourthPhoto, myTexture);fourthName.GetComponentInChildren<Text>().text = links[x].Substring(0, links[x].Length - 4); }
else { setImage(fifthPhoto, myTexture);fifthName.GetComponentInChildren<Text>().text = links[x].Substring(0, links[x].Length - 4); }
}
links.Remove(links[x]);
}
}