Hi Unity Community
I have a UI Image whose Source Image is assigned at runtime. These images will be of various sizes and I need them to be scaled to fit the bounds of the layout element so that the entire image is visible.
I have tried a number of approaches for this, however I can’t seem to get this to work; could anyone please provide me some pointers on how to achieve this?
Many thanks in advance,
Ryan
Current Code:
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;
}
Current image display:

Whole image to be displayed:

