Im new to UNity. I can’t figure out what I need to do to get my objects to spawn on normal positions onto the canvas.
My canvas is preset to 960x, 540y by default and it’s static on Render mode Overlay. that’s weird to start with.
On Rendermode Camera setting the Main Camera as the renderer the transform is reset to 0,0 .
sounds good but then the scale is messed up.
When I try to instantiate stuff their position is all wack. I bet there’s something I need to do to fix this issue, i bet its a normal newbe issue. What do I do?
Instantiating things into a UnityEngine.UI (aka, com.unity.ugui) Canvas hierarchy:
Everything instantiated in a UI MUST be parented to something valid in the hierarchy.
Therefore, if you instantiate prefabs into your UI hierarchy, always use the version of Instantiate that takes two arguments, with the second argument being the parent.
ALWAYS supply a valid under-a-canvas parent Transform where you want the UI chunk to appear.
Supplying positions to anything in UI space via code is almost always an exercise in futility. This is due to the inscrutable nature of the RectTransform object and how it interacts with other RectTransforms, anchors, and the CanvasScaler as well as the canvas mode.
You are of course welcome to supply positions but it is on you to test on EVERY possible combination of aspect ratio and resolution to verify your positioning and anchoring. Expect it to fail if you do not do this testing.
Instead of supplying positions, either use a flavor of the Layout family of components to position things automatically, or else use invisible pre-existing Transforms in your UI as positional inputs.
Here are some notes on UI Anchoring, Scaling, CanvasScaler, etc:
Usually you need to choose a suitable ScaleMode and MatchMode in the Canvas Scaler and stick with it 100%. Generally if you change those settings you will often need to redo your UI entirely.
I also use this CanvasScalerOrientationDriver utility to make sharing UI for Landscape / Portrait easier. Read what it does carefully.