Texture2D loading

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?

Up?

It’s unclear to me what your lifecycle is. Is this during runtime, or purely during editor time?

Or are you looking to transiently hoover down images and assign them to the terrain? If so, it has to be saved in some kind of persistent way or else it’s gone.

Script is executed during editor time, and I was indeed hoping to have the textures kept at runtime, which is not possible if I’m reading you right.

At build time Unity starts afresh and goes looking for what to put in your build.

If something isn’t on disk as an asset, I don’t think there’s any other way to get it in. Perhaps I’m wrong, but that’s the assumption I have always operated under.

I guess you’re right, thanks!