- Home /
Rezise model to match canvas
I am trying to create a room with framed pictures on the wall, I have a model from blender that I have placed as a child under a ui canvas object. I already have code to set the image in the canvas based on a public variable, what I want to be able to do is make the frame model match the size of the parent canvas.
The idea being that I can save this as a prefab then recreate it with different image sources so that no matter what size image I use and its dimensions the 3d model will match it.
I'm not sure how to do this or if I should be using something other than ui.frame for this.
Why are you using a 3D model as a picture? Does it walk around and move? Why wouldn't it just be a render texture? Or an animation, or if it doesn't move, a screenshot?
@RobAnthem The ingame object is a canvas showing a piece of art that I am loading in from an api so I can periodically change the art on the wall.
The way it works is I have a ui image and a 3D model of a canvas frame inside a single parent object and I wanted it to resize properly based on the image size being pulled in.
Answer by youngwolf0 · May 24, 2017 at 05:33 PM
The answer I eventually came up with was I manually lined up the frame with the canvas, then in the code:
Load image source dynamically from url variable.
Get the pixel size of the image.
Resize the canvas to the image pixel size.
Work out the percentage difference in the height and width of the image from the original default size.
Apply this percentage difference to the scale attribute of the frame model.
Worked perfectly :)
Why wouldn't you of just used a flat plane, put the frame over it, and load the picture as a texture? It would automatically fit if the image was square. No canvas/ui mess either.
@RobAnthem Because the images aren't all square, i don't know what the aspect ratio of the image is going to be until it's pulled into the app. I really need the frame because of the nature of the app I'm making so this was the only way I could find to do it.
I feel like there is a more efficient method, but neither of us can think of it, so glad you got it solved :)
Yeah, thanks for the input as well, I'm really new to game development in general so it's all appreciated :)
Your answer
Follow this Question
Related Questions
How to make Image same size as UI Button 1 Answer
How to scale ui image eventually from all sides ? 1 Answer
MessageBox Panel (How to add resizable Text container?) 0 Answers
How to fine-control UI elements scale 0 Answers
Wrpping my head around canvas,Wraping my head around Canvas placement 2 Answers