Hi,
In editor, I’m downloading images from a web service, and saving them in assets. Then, I build TextureLayers from these images:
Stream imgStream = (...) // contains the image data
string imgPath= "Assets\\Resources\\Textures\\test.jpg";
using (FileStream output = File.Create(imgPath))
{
imgStream.CopyTo(output);
}
AssetDatabase.Refresh();
Texture2D texture = Resources.Load<Texture2D>("Textures/test.jpg") as Texture2D;
TerrainLayer tlayer = new TerrainLayer();
// (...)
tlayer.diffuseTexture = texture;
// (...)
AssetDatabase.CreateAsset(tlayer, "Assets/Resources/TerrainLayers/TerrainLayerTest.asset");
AssetDatabase.SaveAssets();
I’d like to pass the image data stream to the Texture2D without having to store a “physical” image asset.
Here’s what I’m doing so far:
Stream imgStream = (...) // contains the image data
Texture2D texture = new Texture2D(1, 1); // Width and height don't matter
MemoryStream ms = new MemoryStream();
imgStream.CopyTo(ms);
texture.LoadImage(ms.ToArray());
TerrainLayer tlayer = new TerrainLayer();
// (...)
tlayer.diffuseTexture = texture;
// (...)
AssetDatabase.CreateAsset(tlayer, "Assets/Resources/TerrainLayers/TerrainLayerTest.asset");
AssetDatabase.SaveAssets();
This seems to work in editor, because the terrain correctly displays the texture. But when I enter play mode, the texture vanishes, the terrain goes back to its base material.
Is what I’m trying to achieve possible, or do I really have to store the image as an asset?