查找在运行时导入的图像的大小 [英] Finding size of images imported at runtime

查看:113
本文介绍了查找在运行时导入的图像的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在运行时从存储在XML文件中的目录加载图像,并通过WWW类将它们分配给RawImage组件。虽然这很好用,但是图像会偏斜以适应新的纹理大小。

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.

我想知道如何获得图像的原始大小或宽高比以便我可以更改图像rect的大小以适应。要导入的图像大小不一,因此使用的方法需要对导入图像的原始大小做出响应。

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.

任何建议都会非常感激。 [使用uJS编写脚本]

Any suggestions would be much appreciated. [Scripting in uJS]

非常感谢,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;
}


推荐答案

如果你不能使用图像(出于正当理由),您可以获得纹理的宽度和高度:

If you cannot use an Image (for nay valid reasons), you can get the width and height of the texture:

WWW www = new WWW(url);
yield return www;
Texture2D tex = www.texture; 
float aspectRatio = tex.height / tex.width;
rawImage.width = width;
rawImage.height = width * aspectRatio;

这应该使图像的矩形具有适当的纹理比例。

This should make the rect of the image of the appropriate ratio of the texture.

如果你可以使用Image和preserveAspectRatio,你可以通过Unity完成它。结果不一定相同,因为它将保持盒子的尺寸并使Sprite在保持比率的同时占据尽可能多的空间。

If you can use Image and preserveAspectRatio, you get it done by Unity. The result is not necessarily the same since it will keep the dimensions of the box and make the Sprite occupies as much space while keeping ratio.

这篇关于查找在运行时导入的图像的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆