[Answered] Instantiating a new Texture in scipt

Is this even possible?

I was able to accomplish this in Unity, but I can’t use it because of the .net 3.5 limitation (I need to include some .net 4.5 libraries). I hope Xenko will be a great alternative with it’s up-to-date .net compatibility.

I want a dynamic UI, where I can load images from websites mid-runtime and change ImageElement’s sprite data on the fly. I’ve found libraries that will POSSIBLY let me download image data on runtime. Now I would like to know whether setting texture data in code is possible (or if I wasted a lot of time working on something that won’t work).

I got a pointer to background (ImageElement) and I want to set it’s Texture property as such:

background.Source.GetSprite().Texture = new Texture().SetData(/texture data/);

As seen here SetData method requires:
----- CommandList, for which I can’t find a working constructor (seems platform specific?),
----- DataPointer (source), which requires:
---------- IntPtr pointer - Memory pointer
--------------- I have no idea what to put here, I have (theoretical) image data as variable
---------- int size - lenght of data in memory? Again am I even supposed to be touching this?
the rest of the arguments have default values, so I don’t think they need to be included.

Anybody have experience with this? Any help is appreciated.


Yes, this is possible.
You can get a CommandList from Game.GraphicsContext.CommandList. This is the default command list, used in most places.

The IntPtr is the address of your texture data. If you have an array, you can use fixed to get a pointer. size is the size in bytes. Your image data needs to be in the proper PixelFormat (typically R8G8B8A8_UNrom).

There are also more convenient overloads of SetData. You can use SetData<Color>(CommandList, Color[]), which takes an array of pixels, straight.