Unity 4.x game development Tips in the 2nd Chapter the application of the camera

Source: Internet
Author: User

Unity 4.x game development Tips in the 2nd Chapter the application of the cameraAs a game developer, don't overlook the importance of the camera. After all, the player is through the camera, only to see the view of the game. This chapter describes some common applications for cameras. This article is selected from the "Unity 4.x game development Skills Collection" Unity 4.x Game development Tips Collection 2.1 setting up a dual-game viewIn many games, a view of a game often contains another view of the game. The two views presented are images taken by two cameras in different locations of the scene. For example, "QQ rook", in addition to the third person view, the game view of the right side there is a runway location Preview view, 2-1 shows. This section simulates this situation, and then explains how to generate this view effect. This article is selected from the "Unity 4.x game development Skills Collection"
Figure 2-1 "QQ speed" in the dual-game view Unity 4.x game development Tips Collection 2.1.1 Environment PreparationFirst, in addition to the main Camera object that is generated by default in the scene, you need to add 4 game objects to the scene: directional light, Camera, cube, and sphere. Change the position of the two cameras so that they see different game objects in the scene, as shown in 2-2.
Figure 2-2 Adding a game object to the scene and adjusting their positionThe view captured by Main camera has a sphere, and the camera captures a cube in the view that modifies the background color of the camera capture view by selecting Camera, Modifying the Background property of a component named camera in the viewer is a different color, as shown in 2-3. The background color can distinguish the boundaries of two views when the final two game views are merged. This article is selected from the "Unity 4.x game development Skills Collection"
                 Figure 2-3 Modifying the background color of the view taken by the camera Unity 4.x game development tips 2.1.2 scriptingThen, create a C # script file in Project view and name it pictureinpicture. Open the script file and add the following code to it: This article is selected from the "Unity 4.x game development Tips Collection"
  • using Unityengine;
  • Geneva
  • public class Pictureinpicture:monobehaviour
  • {
  • //define enum type
  • public enum Horizontalalignment{left, center, right};
  • public enum verticalalignment{top, Middle, bottom};
  • public enum screendimensions{pixels, screen_percentage};
  • //define variables for enum type
  • Ten public horizontalalignment HorizontalAlignment = HorizontalAlignment.Left;
  • one public verticalalignment verticalalignment = verticalalignment.top;
  • public Screendimensions dimensionsin = screendimensions.pixels;
  • -
  • the public int width = 50;
  • public int height= 50;
  • public float xoffset = 0f;
  • public float yoffset = 0f;
  • public bool update = TRUE;
  • +
  • The private int hsize, vsize, Hloc, Vloc;
  • When the game object is initialized, call this function
  • void Start ()
  • % {
  • Adjustcamera ();
  • + }
  • When the game is running, this function is called every frame
  • void Update ()
  • {
  • if (update)
  • Adjustcamera ();
  • + }
  • + void Adjustcamera ()
  • {
  • if (dimensionsin = = screendimensions.screen_percentage)//Adjust the view to a specified percentage size
  • {
  • hsize = mathf.roundtoint (Width * 0.01f * screen.width);
  • PNS vsize = mathf.roundtoint (Height * 0.01f * screen.height);
  • - -}
  • All Else//Adjust the view to the specified pixel size
  • + {
  • hsize = width;
  • vsize = height;
  • + }
  • if (HorizontalAlignment = = HorizontalAlignment.Left)//left aligned horizontally
  • {
  • hloc = Mathf.roundtoint (Xoffset * 0.01f * screen.width);
  • * }
  • - Else if (HorizontalAlignment = = HorizontalAlignment.Right)//Align Right in horizontal direction
  • {
  • Hloc = Mathf.roundtoint ((screen.width-hsize)-(Xoffset * 0.01f * screen.width));
  • Wuyi }
  • on-or-center alignment in the horizontal direction
  • *
  • Hloc = Mathf.roundtoint (((Screen.width * 0.5f)-
  • (hsize * 0.5f))-(Xoffset * 0.01f * screen.height));
  • ( + $}
  • if (verticalalignment = = verticalalignment.top)//Top aligned vertically
  • (
  • Vloc = Mathf.roundtoint ((screen.height-vsize)-(Yoffset * 0.01f * screen.height));
  • *
  • VerticalAlignment else if (= = Verticalalignment.bottom)//Bottom aligned vertically
  • +
  • vloc = Mathf.roundtoint (Yoffset * 0.01f * screen.height);
  • + }
  • All Else//center aligned vertically
  • {
  • Vloc = Mathf.roundtoint (((Screen.height * 0.5f)-
  • (vsize * 0.5f))-(Yoffset * 0.01f * screen.height));
  • A -i}
  • camera.pixelrect = new Rect (hloc,vloc,hsize,vsize);
  • + +}
  • *
The script will determine how to place it, based on user-defined settings, and how large it is to display the view taken by the camera. This article is selected from the "Unity 4.x game development Skills Collection"

Unity 4.x game development Tips in the 2nd Chapter the application of the camera

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.