Hello, everyone! I have a simple question that causes me some trouble. We all know the “Aspect Ratio Filter” component. I have a problem with my textures, because they lose the aspect ratio and they get stretched (the position stays the same thanks to my matrix calculations in the code). Can I use something like the Aspect Ratio Filter in my OnGUI function?
Use on your Image components set the “Preserve Aspect” to true.
If you draw a texture inside OnGUI it’s drawn as it is. However keep in mind that Unity usually imports textures with a resolution that is a power of two. You have to import the texture with the texture type: “Editor GUI and Legacy GUI”. Otherwise your texture might be stretched or distorted to fit into a power of two resolution. For example an image with the size 400x300 would most likely be imported as 512x512. So the width would be upscaled from 400 to 512. To keep the aspect ratio the height would need to be “384”. However that is not a power ot two (2,4,8,16,32,64,128,256,512,1024, …).
The requirement for power of two is hardware related. textures that are not a power of two have problems generating mipmaps. Most hardware is optimised for power of two textures. Some older GPUs even required square textures, so this was not possible: 512x256.
When you import the texture as “Editor GUI and Legacy GUI”, Unity will import the texture “as it is”, using any odd resolution that the texture might have. Though Unity might be forced to use power of two textures due to hardware limitation, in this case Unity will create a texture that has a size of power of two and add some padding inside the texture. Unity will automatically adjust the used texture coordinates to only show the part of the texture that contains the image information. Note that this only applied to the IMGUI system.
ps: If that doesn’t solve your issues, you have to manually tinker with the projection by changing the matrix in a strange way. If you did that you should include your code.
@Bunny83 , thank you for this answer! My textures are already “Editor GUI
and Legacy GUI”.I still get that problem. I use Matrix for making my elements stay on their positions no matter the resolution, and that works. The problem is the size and the losing of aspect ratio. I am adding two screenshots - one in 16:9, the other in 3:2. You can see what I am talking about.Simple math. Just divide to get the aspect ratio of your texture and apply it in your size
Texture2D tex;
tex=new Texture2D(400,250);
float ratio;
//devide to get ratio between width and height
ratio = (float)tex.height/(float)tex.width;
// simply multiply this number to the height when needed
//displays the original ratio
GUI.DrawTexture (new Rect(10,10,400,400*ratio), tex);
@toddisarockstar , It does not work properly, I don’t know why. The texture is still getting stretched in different resolution. Do you think this might happen because your works, but I am resizing GUIMatrix?
void ResizeGUIMatrix()
{
// Set matrix
Vector2 ratio = new Vector2(Screen.width/defaultScreenRes.x , Screen.height/defaultScreenRes.y );
Matrix4x4 guiMatrix = Matrix4x4.identity;
guiMatrix.SetTRS(new Vector3(1, 1, 1), Quaternion.identity, new Vector3(ratio.x, ratio.y, 1));
GUI.matrix = guiMatrix;
}
I will be really thankful if you help me! :))