Unity - 使用 www 加载图像会导致应用程序中的所有图像发生变化
Unity - Loading image using www cause all images in application to change
我是 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);
}
}
}
然后我将图像添加为 public Image img;
谁能告诉我为什么 unity 将图像加载到我的应用程序中的所有图像视图而不是只加载一个图像视图?
你说
I have many different images,
但我猜你指的是不同的 Image
组件,其中你很可能多次引用资产中的相同纹理
所以您的代码实际做的是覆盖您的图像引用的任何纹理资产 => 它也在引用相同纹理资产的所有其他 images/materials 等中发生更改。
您应该创建一个新纹理,将数据加载到其中并更改图像的纹理参考:
// Create new texture
// Size values don't matter because texture will be overwritten
var newTexture = new Texture2D(2,2);
// Load image I new texture
www.LoadImageToTexture(newTexture);
// Use the reference to that new texture
img.mainTexture = newTexture;
我是 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);
}
}
}
然后我将图像添加为 public Image img;
谁能告诉我为什么 unity 将图像加载到我的应用程序中的所有图像视图而不是只加载一个图像视图?
你说
I have many different images,
但我猜你指的是不同的 Image
组件,其中你很可能多次引用资产中的相同纹理
所以您的代码实际做的是覆盖您的图像引用的任何纹理资产 => 它也在引用相同纹理资产的所有其他 images/materials 等中发生更改。
您应该创建一个新纹理,将数据加载到其中并更改图像的纹理参考:
// Create new texture
// Size values don't matter because texture will be overwritten
var newTexture = new Texture2D(2,2);
// Load image I new texture
www.LoadImageToTexture(newTexture);
// Use the reference to that new texture
img.mainTexture = newTexture;