Cameral Size with different resolution
In short, to establish a 1:1 pixel relationship between world and screen space, the camera Size
must be half the vertical resolution of your game. If your game window is 800×600 pixels, then Size
should be 300, because 600 / 2 = 300. If your game is 1024×768, then Size should be 384, because
768 / 2 = 384. If your game is designed to run at different resolutions, and if those resolutions can
be changed by the user at runtime, then you’ll need to script your camera to adjust its Size value
on each resolution change.
Finally, set the camera Size to 300, since 600 / 2 = 300
Depth
Depth order (Z-Ordering) is important for 2D games because it controls which objects should appear
in front of others. Given two game objects, the one with the lowest depth will appear in front of the
other. This is because lower-depth objects are closer to the camera. In traditional 2D games this
problem was usually solved with the painter’s algorithm: objects were sorted in a list based on their
distance from the camera, and then rendered in that order from furthest to nearest. The nearest
objects were always rendered last to ensure they were drawn over distant objects. Unity, however,
uses its own custom and internal process for rendering, which prioritizes objects and rendering in
quite a different way. Thankfully, however, this doesn’t prove a problem for us. Why? Because we
can simply use an object’s Z-Position (depth position) to control draw order. Objects nearer to the
camera will automatically draw on-top of more distant objects. If you want an object to appear in
front of all others in the scene, then ensure its Z (Depth) value is nearer to the camera than any
other object (see Figure 8-16).
By default the
camera with the highest Depth is rendered on-top of all other cameras.
Next select each camera, and use the Culling Mask setting from the Object Inspector to limit the
cameras renderable objects only to its associated layer. This means removing a check mark from all
options except its renderable layer, either LayerObject1 or LayerObject2. This way, each camera only
renders its assigned objects and no more. (Alternatively, you can select Nothing for the Culling Mask
to deselect everything, and then choose the item to be rendered.) See Figure 8-21.
Clearing the Depth Buffer
Now that we’ve configured the render layers, it’s time to set up the cameras to composite their
renders together, showing the output from both in a single image. To do this, select the topmost
camera (the camera with the highest depth) and change its Clear Flags setting in the Object
Inspector from Skybox to Depth Only (see Figure 8-22).


By setting the Clear Flags to Depth Only we instruct the topmost camera to turn its background
transparent to reveal the camera layers beneath. We can even apply these settings to additional
cameras further down the depth chain to create a complete stack of layered cameras that render
together into a single image, even though they exist in completely different parts of the scene.
Using this technique we gain high-level control of the rendering depth of scenes (see Figure 8-23).

浙公网安备 33010602011771号