- Home /
Finding size of images imported at runtime
Hi Unity Community
I am currently loading images at runtime from directories stored in an XML file, and assigning them to RawImage components via the WWW class. While this is working fine, the image is skewed to fit into the new texture size.
I am wondering how to get an image’s original size or aspect ratio so that I can change the size of the image rect to suit. The images to be imported are at varying sizes and therefore the approach used needs to be responsive to the original size of imported images.
Any suggestions would be much appreciated. [Scripting in uJS]
Many thanks in advance,
Ryan
function loadContextImage(texLocation : String)
{
if (!imageView.activeSelf)
{
imageView.SetActive(true);
}
var wwwDirectory = "file://" + texLocation; //this will probably need to change for other OS (PC = file:/ [I think?]) - **REVISE**
var newImgTex = new Texture2D(512, 512);
while(true){
var www : WWW = new WWW(wwwDirectory);
yield www;
www.LoadImageIntoTexture(newImgTex);
if (www.isDone){
break; //if done downloading image break loop
}
}
var imageRender : UI.RawImage = imageView.GetComponent.<RawImage>();
imageRender.texture = newImgTex;
}
Answer by RyanAchtenSoma · Feb 11, 2016 at 08:53 PM
Can find the size of images imported at runtime using the www.texture reference.
i.e.
var texWidth = www.texture.width;
var texHeight = www.texture.height;
From here an appropriate Image/RawImage texture size can be assigned for proper aspect ratio. Hope this helps anyone in a similar situation.
Answer by H-Rat · Sep 04, 2017 at 09:08 AM
use this line to get real size of the loaded sprite
height = GetComponent().sprite.texture.height;
Your answer
Follow this Question
Related Questions
Detect WWW Image bitmap dimensions? 2 Answers
How to load an image at runtime via WWW correctly 1 Answer
Display Raw Images at runtime in unity 4.6 0 Answers
Loading 8192x8192 textures at runtime on android 0 Answers
Image is not loading into Texture 0 Answers