【问题标题】:Fit the length and width of the image with the screen, larger size / c# unity使图片长宽与屏幕适配,更大尺寸/c# unity
【发布时间】:2021-05-30 00:11:12
【问题描述】:

我有一个下载图片的脚本 我希望图像的高度和宽度与屏幕匹配

图片 [1]:https://i.stack.imgur.com/0VcAT.png

图像应该在用户的最佳视野中 顺便说一句,这是一个看漫画的应用程序

" 我非常努力地用最好的方式传达这个想法,我希望我成功了^_^"

感谢您的帮助

脚本

public IEnumerator GetImage(string url)
{
    UnityWebRequest www = UnityWebRequestTexture.GetTexture(url);
    yield return www.SendWebRequest();

    Texture2D myTexture = DownloadHandlerTexture.GetContent(www);


    //خوارزمية تعديل حجم الصورة 
    // Give all 100 of the length or width one number
   for(int i = 1; i < 40;)
   {
       if(i*100 > myTexture.width)
       {
           for(int u = 1; u < 40;)
           {
              if(u*100 > myTexture.height)
              {
                WidthX = i;
                HeightX = u;
                u = 100;
                i = 100;
              } 
               u++;
           }
       }
       i++;
   }

    // After reducing all 100 to one, scale it down to fit the screen
    WidthX = WidthX / 3;
    HeightX = HeightX / 3;

    //Create sprite and set a Width and Height
    Rect rec = new Rect(0, 0, myTexture.width, myTexture.height);
    OImg.sprite = Sprite.Create(myTexture, rec,  Vector2.zero);
    OImg.GetComponent<RectTransform>().localScale = new Vector3 (WidthX,HeightX,1);        
    

}

我通过(localScale)改变了图像的大小,我认为这是一个错误

必须为图片修改“矩形变换宽度和高度”

【问题讨论】:

    标签: c# image unity3d rect


    【解决方案1】:

    当您使用RecTransformlocalScale 时,您正在使用RawImageImage 来显示此纹理。不要尝试调整它的大小,只需使用anchors

    当它们出现在检查器中带有一些箭头的小盒子中时,在那里形成它们更容易。您是否希望此图像不占据全屏?如果您希望它始终占据全屏,我可以为此添加一个解决方案,但这里有一个代码解决方案,它将强制您的 ImageRawImage 占据全屏,而不管给出的图像如何.

    [SerializeField] private Transform CanvasTransform = null;
    
    private RectTransform OImgRect = null;
    
    private void Start()
    {
        OImgRect = OImg.GetComponent<RectTransform>();
    }
    
    public IEnumerator GetImage(string url)
    {
        ...   
        
        // set your image 
        OImg.sprite = Sprite.Create(myTexture, rec,  Vector2.zero);
        
        // set our parent to the canvas while storing our previous parent
        Transform prevParent = transform.parent;
        
        // set the new parent to the canvas
        transform.SetParent(CanvasTransform, false);
        
        // fit it to screen
        FitToImageToScreen();
        
        // now set it back but using the true parameter to keep our scaling
        transform.SetParent(prevParent, true);
    }
    
    private void FitToImageToScreen()
    {
        OImgRect.anchorMin = new Vector2(0, 0);
        OImgRect.anchorMax = new Vector2(1, 1);
        OImgRect.pivot = new Vector2(0.5f, 0.5f);   
    }
    

    删除您尝试使其适合屏幕的所有其他代码。只需使用锚点。如果您想了解我正在使用的字段的来源,有anchorMinanchorMaxpivot。它们是规范化的值 [-1,1],描述了图像如何适合其父容器。只要您的Canvas 组件适合您的屏幕并且OIImgImageRawImage 是它的直接子级,图像就会扩展为Canvas

    【讨论】:

    • 谢谢回答 我已经尝试在Anchor Min中玩了 但是我使用滚动条内的图像 这就是为什么我想要具有最佳长度和宽度的图像,如果图像没有问题比屏幕大 在问题中添加图片
    • 您可以将图像子化为Canvas,设置锚点,然后将图像再次重新子化到滚动点。使用SetParent 的重载使用worldPositionStays。如果将此设置为 true,它将保留其当前的比例、位置和旋转。我更新了代码以反映这个想法。它不需要是适合图像的Canvas,但可以是任何其他变换对象。
    • 现在工作正常谢谢您的帮助
    猜你喜欢
    • 2013-01-05
    • 1970-01-01
    • 2023-04-11
    • 2021-03-29
    • 2012-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多