Scaling Objects to Container Size

7393571--902900--upload_2021-8-5_18-34-16.png

…So this happens when I just plop my item game objects into the UI. The scale is weirdly wrong, so as to combat this I found that Mesh objects have a bounds field. Thus, I tried creating the following code;

this.transform.localPosition = Vector3.zero;
                Rect mR = ((RectTransform)this.transform.parent.transform).rect;
                Vector3 cornerMin = new Vector3(mR.xMin,10,mR.yMin);
                Vector3 cornerMax = new Vector3(mR.xMax,10,mR.yMax);
               
                myMeshFilter.mesh.bounds.Encapsulate(cornerMax);
                myMeshFilter.mesh.bounds.Encapsulate(cornerMin);

…And damn near nothing changes. I’m guessing I’m not updating the mesh properly, or the changes to the mesh aren’t updating on the MeshRenderer component? I’m not sure.

Help, advice, alternative solutions, or just plain pointing at what I’m doing wrong would be wonderful.
Thanks in advance!

Are you trying to stick 3D objects into your UI? Canvases can operate at really whacky scales, based on the render mode and the canvas scaler mode. They also don’t render the same way as 3D objects.

When you drop a regular old 3D mesh into a UI, Unity tries to make happy-happy and “back the scale out,” which usually doesn’t go the way you expect.

Yes. I wanted to render objects in 3D to my item system UI, as opposed to trying to create textures for each item. The colors and materials of each item will be capable of changing in game, so I decided against making 1,001 sprites to coincide with each material/item combination. I discovered that having lights effect the inventory UI objects was a cool mechanic, so I went ahead with my current method.


And thus the scaling is off and odd.

Well, the good news is that it is solvable and doable… I think you want to either bring the canvas to the GameObjects by making it a world space canvas, or else bring the Canvas to the camera by making it screen space - Camera, but I honestly haven’t actually done it in so long I’d have to tinker a bit.

Perhaps you just need to divide out the transform.lossyScale at that point in the hierarchy? Or perhaps tally up (multiply cascade) all the .localScales including the top one in the Canvas??

Keep tinkering, you’ll get it! Strip it down to the simplest set of examples, and BE SURE to change aspect ratios and screen pixel sizes to make sure happy-happy. Here’s my standard blurb on that:

Here are some notes on UI Anchoring, Scaling, CanvasScaler, etc:

https://discussions.unity.com/t/845782/4

https://discussions.unity.com/t/848795/5

1 Like

I appreciate the encouragement, really! Working solo on these kinds of things is always daunting, but I suppose its a passion project and not a job for a reason!
So it sounds like I need to go throughout all those scales and factor in their calculations if I wanted to go that route, or just move the Canvas around in real space (which from how it sounds, wouldn’t change the issue? There would still be hierarchy scaling issues). I mentioned the Mesh object and its associated functions/bounds, are there any functionalities there that might simplify the issue?

I don’t think so. I also would stay away from Bounds. It seems to have surprising results sometimes. They say “it is X” but I see a lot of people posting that the bounds are not what they thought it would be.

As I said though, strip stuff down, make simple-simple, engineer the scale to be correct, then test that you really have it, etc. , different aspect ratios, pixel sizes, etc.