- Home /
Scaling run-time instantiated UI objects
Hello, I looked around for someone with my same problem but I didn't find anything until now.
I'm currently working on a project for my University with Unity 5.3. I'm stuck on the UI because I need to resize all the elements on the canvas according to the screen size. I selected "Scale with Screen Size" on Canvas Scaler and it's working perfectly fine, but unfortunately the project included the use of a Database which needs to be consulted by the player. So I need to look for information in the database and to visualize them somehow so that the user knows what's saved inside.
For this purpose, I created a prefab holding a bunch of text boxes, each corresponding to a variable, and I use a script to dynamically create an object for each entity currently saved in the database, this way:
while (reader.Read ()) {
reference = (GameObject)Instantiate (container, pos, Quaternion.identity);
reference.transform.SetParent (fatherOnCanvas.transform);
... (a bunch of other things)
}
where reader is an object I get from the database interaction and container is the gameobject I created. fatherOnCanvas is an empty object present on the canvas, I need it to drag all the gameobject I create around and because the object are created without any parent, and if I don't put their transform as child of the canvas, or another object on the canvas, they won't be visible.
This also works fine, the element is instantiate in the right position and displays the right information, BUT it doesn't scale with the canvas. The UI elements inside of container remains of the dimension they had on my PC, being cropped in smaller screens.
So, basically the problem is how to scale those dynamically instantiated UI objects. I tried to drag and drop those object in the level editor.They don't scale unless I don't revert the prefab or I don't drag and drop them directly as child of the canvas. I tried to get the same effect via script using PrefabUtility.RevertPrefabInstance, but it didn't work. I am a bit out of ideas. Should I try to change the scale values of the objects while I create them? If so, how can I access to some value which can help me scale them properly? Anyone has got any better idea?
Answer by radiantboy · Mar 21 at 11:13 AM
Did you fix this? same issue here, it seems instantiated prefabs dont scale correctly when you hit start. Silly thing is everythign scales perfectly if I adjust the resolution after hitting play.
Your answer

Follow this Question
Related Questions
Video Player time slider 2 Answers
Rotate GameObject When Mouse/Player is Within Certain Distance From it 1 Answer
World Space UI Scale To Fit Screen At DIfferent Aspect Ratios 1 Answer
Resize a 3D prefab without affecting scale 2 Answers
How can I make the player stop moving when a UI dialogue box is active? 2 Answers