缩放UI图像源图像

时间:2015-09-14 01:56:25

标签: layout unity3d unityscript unity3d-gui

我有 UI图像,其源图像是在运行时分配的。这些图像将具有各种尺寸,我需要缩放它们以适应布局元素的边界,以便整个图像可见。

我已经为此尝试了很多方法,但是我似乎无法让它发挥作用;有谁能请我提供一些如何实现这个目标的指示?

非常感谢提前

NB :布局是通过布局组组件来控制的,这些组件与使用其他可能有用的布局组件(例如宽高比Fitter <)控制矩形时会发生冲突/ em>和 Content Sizer (如此问题的文档解决方案中所述)

public
function ContextImgImp() {

  Debug.Log("Context Img Located at: " + texLocation);

  var wwwDirectory = "file://" + texLocation; //this will probably need to change for other OS (PC = file:/ [I think?]) - **REVISE**
  Debug.Log("Tex located at: " + wwwDirectory);

  newImgTex = new Texture2D(512, 512, TextureFormat.DXT1, false);

  while (true) {

    var www: WWW = new WWW(wwwDirectory);

    yield www;

    Debug.Log("Done Downloading Texture");
    www.LoadImageIntoTexture(newImgTex);

    if (www.isDone) {
      break; //if done downloading image break loop
    }
  }

  var imgSprite = contImg.sprite;
  var imgRect = imgSprite.rect;
  var imgPivot = imgRectTrans.pivot;
  var pixelScale = 100;
  Debug.Log("Pixel Scale:" + pixelScale);

  newImgSprite = Sprite.Create(newImgTex, imgRect, imgPivot, pixelScale);
  contImg.sprite = newImgSprite;

  //imgRectTrans.rect.size = Vector2(130,130);
  //contImg.sprite.rect.size = Vector2(130,130);
  //contImg.type = Image.Type.Simple;

  //***HERE***	
  if (assetFromFile) {
    ImgNameSplit(); //only execute namesplit if from file
  }
}

1 个答案:

答案 0 :(得分:1)

有“SetNativeSize”函数,但是如果您使用布局组件,则在下一个更新循环中将覆盖此大小。