Coding Editor Resources, Canvas & Texture2D
Similarly to how we can specify a class in UnrealScript by using the class'SomeClass' syntax, we can specify any other type of resource that can be imported into a upk through the editor. This is done through the following syntax:
<ObjectType>'FullyQualifiedName'
In this case, <ObjectType> is any kind of UDK editor resource, for example a Texture2D. FullyQualifiedName is whatever is the fully qualified name of this resource, eg SomePackage.SomeGroup.Something or SomePackage.Something if it is not in a group. If you use the "Copy fully qualified name to clipboard" command from the editor right click menu, it will include the object type.
One of the most common cases where this will be useful is when passing textures or materials to a function but can also commonly apply to meshes and static meshes.
For example, to specify a particular Texture2D, we can use the following syntax: Texture2D'Package.Group.TextureName'
Texture2D
This is the UDK's version of an image, a so-called Texture2D; the 2D specification is needed because it is possible to have 1D and 3D textures as well. One of the most common reasons to use a Texture2D in code is when dealing with Canvas. A common use-case is placing an image on the screen. To do this, Canvas has the function DrawTexture with the prototype: Canvas.DrawTexture(Texture2D textureToUse, float scale);
- Texture2D textureToUse
- This is the texture that should be placed on the screen. It is alpha-blended, meaning that the alpha value at each pixel is used to determine the opacity of that pixel when blending it with what is already on the screen (alpha = 255 means complete replacement with this pixel, alpha = 0 means this pixel will have no effect)
- float scale
- This is the scale of the texture. For example, if we draw a 512x512 texture on a 1024x1024 screen at scale 1.0, it will occupy 1/4 of the screen. However, if we draw it at scale 2.0, it will occupy the entire screen
Here's an example of how we would use DrawTexture. For this example, assume that the texture AwesomePackage.AwesomeGroup.AwesomeTexture exists and is a valid Texture2D:
Canvas.SetPos(0, 0); // Set position to top-left of screen
Canvas.SetDrawColor(255, 255, 255); // Set the draw color to white
// Draw the texture at scale 1
Canvas.DrawTexture(Texture2D'AwesomePackage.AwesomeGroup.AwesomeTexture', 1.0);
But what part of the screen will this texture occupy? For that, we will need to get some of the properties of a Texture2D.
Working With Texture2D in Code
Like other data types, it is possible to declare variables that can hold instances of resources, such as a Texture2D in this case, and to get the properties of those resources. For example, consider the following function:
function AcceptTex(Texture2D myTex)
{
`Log("Texture size is "$myTex.SizeX$"x"$myTex.SizeY);
}
function CallingFunc()
{
local Texture2D tex;
tex = Texture2D'AwesomePackage.AwesomeGroup.AwesomeTexture';
AcceptTex(tex);
// Could also have called it as:
// AcceptTex(Texture2D'AwesomePackage.AwesomeGroup.AwesomeTexture');
}
Notice some things about the above:
- We use Texture2D as the data type of a function parameter and of a local variable
- We assign into the variable by using a Texture2D specifier, like we would with an UnrealScript class
- We pass the variable into the function like it was a literal specifier
- We query the size of the texture by retrieving its SizeX and SizeY properties
To find what properties a Texture2D has, we open Texture2D.uc (package Engine, or use Visual Studio Ctrl + , then Texture2D Enter). Here's a collection of interesting properties:
/** The width of the texture. */
var const int SizeX;
/** The height of the texture. */
var const int SizeY;
/** The original width of the texture source art we imported from. */
var const int OriginalSizeX;
/** The original height of the texture source art we imported from. */
var const int OriginalSizeY;
Most importantly, we can get the size of the texture this way. For novelty, we also have access to the size of the original file that was used to create this texture.
Like Texture2D, there is an UnrealScript file for every resource type that can be imported into the editor. Whenever you want to know what you have access to, simply open the relevant uc file.
Determining Texture Screen Size
Because we have access to the size of the texture, we can use this information in combination with the size of the screen to find how much of the screen the texture will occupy. For example, assume that we have the following HUD class:
class SomeHUD extends HUD;
function DrawHUD()
{
local Texture2D tex;
tex = Texture2D'Package.Group.GreatTexture';
Canvas.SetPos(0,0);
Canvas.SetDrawColor(255, 255, 255);
Canvas.DrawTexture(tex, 1.0);
}
Defaultproperties
{
}
In this example, how much of the horizontal screen did the texture occupy? To find out, we can use its SizeX as follows:
Texture.SizeX / SizeX -> proportion of horizontal screen occupied
So if we wanted to codify this, we would end up with a function like this (assume this function is in the above class):
function PrintProportion(Texture2D tex)
{
local xProp, yProp;
xProp = tex.SizeX / SizeX;
yProp = tex.SizeY / SizeY;
`Log("X proportion is: "$xProp$", Y proportion is: "$yProp);
}