【发布时间】:2019-01-03 20:51:20
【问题描述】:
我对 Unity 还很陌生,所以这可能很容易。
我正在尝试将 URL 中的图像加载到我的应用程序中的图像中。在我的应用程序中,我有许多不同的图像,但由于某种原因,所有图像都更改为从我的 url 加载的图像。
我制作了一个名为LoadImage 的组件,并将其仅添加到我想要更改的一个图像中。我加载图像的代码如下所示:
public class LoadImage : MonoBehaviour
{
public Image img;
// Use this for initialization
void Start ()
{
DownloadViaURL();
}
void DownloadViaURL()
{
Debug.Log("Called DownloadViaURL");
FirebaseDatabase.DefaultInstance
.GetReference("Child1").Child("Child2").Child("ImageURL")
.GetValueAsync().ContinueWith(task =>
{
Debug.Log("Default Instance entered");
if (task.IsFaulted)
{
Debug.Log("Error retrieving data from server");
}
else if (task.IsCompleted)
{
DataSnapshot snapshot = task.Result;
string data_URL = snapshot.GetValue(true).ToString();
//Start coroutine to download image
StartCoroutine(AccessURL(data_URL));
}
});
}
IEnumerator AccessURL(string url)
{
using (WWW www = new WWW(url))
{
yield return www;
www.LoadImageIntoTexture(img.mainTexture as Texture2D);
Debug.Log("Texture URL: " + www.url);
}
}
}
然后我将图像添加为公共图像 img;
谁能告诉我为什么统一将图像加载到我的应用程序中的所有图像视图中,而不仅仅是一个?
【问题讨论】:
-
可能是因为您使用的是同一个目标 Texture2D?