Reputation: 525
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
NB: the layout is controlled via Layout Group components, which conflicts for control over the rect with other potentially useful layout components such as Aspect Ratio Fitter and Content Sizer (as mentioned in documented solves for this issue)
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
}
}
Upvotes: 0
Views: 103
Reputation: 1739
There is the "SetNativeSize" function, however if you are using layout components then this size will be overridden on the next update loop.
Upvotes: 1