NineSliceRuntime
Last updated
Was this helpful?
Last updated
Was this helpful?
The NineSliceRuntime object is used to draw a visual object which references a Texture2D, but which does not stretch the corner pieces of a texture, and which only stretches the edges between the corners along their axis. In other words, the NineSlice (sometimes also referred to as a sprite frame) is used to draw frames in UI which can stretch without introducing visual artifacts.
For more information about the NineSlice type, see the .
The following code can be used to instantiate a NineSliceRuntime which uses a Texture (png file) named Frame.png.
NineSlice textures can be assigned using a string property or Texture2D instance. When assigning using a string, the ToolsUtilities.FileManager.RelativeDirectory
is used to determine the file's directory.
For example, consider a file called Frame.png which is located in the Content directory:
This file can be used as a texture by assigning the RelativeDirectory and then loading Frame.png. Note that RelativeDirectory is usually set to Content, or to the location of the .gumx file.
Alternatively, a Texture2D can be assigned directly
Once a SourceFileName is assigned, the SourceFile property references a valid Texture2D which can be reused.
By default a NineSlice uses it entire texture. This can be customized using texture coordinate and TextureAddress properties.
For example, consider drawing a portion of the following texture:
We can obtain a portion of this texture by assigning the coordinate values. Before doing so, find the coordinates of the area you would like to draw. For example, we can use Paint.NET to find the coordinates of a frame in the image.
We can use these values to set TextureLeft
, TextureTop
, TextureWidth
, and TextureHeight
values as shown in the following code:
Note that if TextureAddress isn't set to Custom, then the four coordinate values are ignored and the entire texture is used.