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;
}

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.

use this line to get real size of the loaded sprite

height = GetComponent().sprite.texture.height;