- Home /
Scaling guitexture gameobject
Hi, i'm working for mobile games and i have currently 3 guitexture gameobject that use for virtual joystick, how can i scale these guitexture? since i did not drawing this texture using script so it's not draw by calling function OnGUI() so i can't use this code for scaling
function OnGUI()
{
var svMat = GUI.matrix;
scale.x = Screen.width/originalWidth;
scale.y = Screen.height/originalHeight;
scale.z = 1;
GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, scale);
drawPause();
GUI.matrix = svMat;
i generely find code WAYYYYY easier to use than the GUITexture component
yea but unfortunately i don't have any idea how to make guitexture from code to work like joystick so i kind of stuck using this guitexture component
Answer by robertbu · Dec 13, 2013 at 05:22 AM
One solution is to modify the GUITexture.pixelInset. You can resize by changing the 'width' and 'height', but if you have it anchored in the center, you must also adjust the 'x' and 'y'.
Here is a function that resizes the GUITexture on the same game object as this script:
function ResizeGUITexture(widthScale : float, heightScale : float) {
var rect = guiTexture.pixelInset;
rect.width *= widthScale;
rect.height *= heightScale;
rect.x = -rect.width / 2.0;
rect.y = -rect.height / 2.0;
guiTexture.pixelInset = rect;
}
wow thank you ,and what is the value for widthscale and heightscale? screen resolution?
It is a fraction. So if you want to size it to 1/2 size on width and height, you would call it:
ResizeGUITexture(0.5, 0.5);
Note if you wanted to use pixels rather than fractions, you can change the code to:
function ResizeGUITexture(w : float, h : float) {
var rect = guiTexture.pixelInset;
rect.width = w;
rect.height = h;
rect.x = -rect.width / 2.0;
rect.y = -rect.height / 2.0;
guiTexture.pixelInset = rect;
}
hmm so i just need to detect what resolution being used by the game and giving some value to ResizeGUITexture that will make gui texture big enough to use into that screen resolution?
or maybe i just put all this ResizeGUITexture script inside my gui.matrix? so i treat it like other guitexture?
GUI and GUITexture are two different things. GUITextures do not use the GUI.matrix. As for scaling for different displays, I start by calculating the ratio of the authored screen resolution to the device screen resolution. Say you did all of your authoring for a device that has a height of 1024. You could do in a script attached to the GUITextures in start:
function Start() {
var fraction = Screen.height / 1024;
ResizeGUITexture(fraction, fraction);
}