Unity2D multi-resolution screen adaptation solution

Source: Internet
Author: User
This article will describe a simple and effective Unity2D multi-resolution screen adaptation solution, which is applicable to native Unity2D games, that is, there is no third-party 2D plug-in, such as Uni2D, 2Dtoolkit and other developed games, the NGUI plug-in is not affected by this solution, and can be used perfectly with this solution. --------------------------------------

This article will describe a simple and effective Unity2D multi-resolution screen adaptation solution, which is applicable to native Unity2D games, that is, there is no third-party 2D plug-in, such as Uni2D, NGUI plug-ins are not affected by this solution for games developed such as 2D toolkit and can be used perfectly with this solution. --------------------------------------

This article will describe a simple and effective Unity2D multi-resolution screen adaptation solution, which is applicable to native Unity2D games, that is, there is no third-party 2D plug-in, such as Uni2D, NGUI plug-ins are not affected by this solution for games developed such as 2D toolkit and can be used perfectly with this solution.

--------------------------------------- Split line -----------------------------------------

First, describe some basic concepts:

1. Aspect Ratio = screen width/screen height

2. The size of the Camera lens in Unity2D determines how much content we actually see the game. In the editor, we can adjust the Camera size by adjusting the orthographicSize attribute value of the Camera. As shown in, when the camera orthographicSize attribute value is equal to half of the current screen height unit, the camera size is exactly the same as the screen size. Note that we mention half of the screen unit height. This value is converted from pixel To unit ratio, that is, Pixels To Units. The default value of this ratio in Unity2D is 100, that is, 100 pixels equals 1 unit. If our game screen is 640 pixels high, the actual unit height is 6.4 units. When our camera's orthographicSize value is 3.2, the camera size is exactly the same as the screen size.

(You can use this option to adjust the pixel unit ratio of each image)

You may be wondering how to determine the width of the camera, because the height of the camera can only be adjusted directly in the Unity editor? The answer is the screen aspect ratio we mentioned above. Unity calculates the width of the camera based on the actual Aspect Ratio of the current screen and the camera's orthographicSize value, namely:

Actual camera width = camera orthographicSize * 2 * screen aspect ratio

That is

Actual camera width = camera height * screen aspect ratio

For example, the screen pixel of iPhone 4 is 640*960, and the aspect ratio is. If Pixels To Units is set To 100, the actual width of the camera is calculated as 6.4, which is the unit width of the screen.

--------------------------------------- The split line of the better environment ------------------------------------------

Now, after talking about the above things, we can see why our games are displayed on different screen-resolution devices.

For different screen resolutions, the same camera orthographicSize value produces different camera sizes. Different camera sizes lead to different actual game content displayed.

Next, I will propose two concepts to facilitate the subsequent description:

1. Effective game content refers to the content that must be completely displayed on the screen in the game;

2. Actual game content refers to all the game content, including valid content and content that is mainly added to adapt to multi-resolution or other unimportant purposes.

We usually choose a fixed design resolution for our development. For example, the design resolution of a commonly used iOS vertical screen game is 640*960. Let's take this design resolution as an example. Generally, the design resolution size is the size of the effective content of our game.

With this resolution, we set the camera's orthographicSize to 4.8. Assume that we do not adapt to any multi-resolution processing, so that the size of the effective content area of our game is the same as that of the actual content area, which is 6.4*9.6 (the conversion from pixels to units has been done, the same below ), let's see what happens when we run this game on an iPhone 5 device (that is, the screen is 640*1136.

For better description, we first set the variable:

Valid game content size: gameValidContentSize

The actual game content size is gameContentSize. The camera size is cameraSize. The actual screen size is screenSize. The screen aspect ratio is aspectRatio.

Start computing:

OrthographicSize = 4.8 aspectRatio = 640/1136 = 9/16cameraSize. height = camera orthographicSize * 2 = 4.8*2 = 9.6cameraSize.width = cameraSize. height * aspectRatio = 9.6*9/16 = 5.4

According to the calculation, the actual camera width is 5.4, while the effective content width of the game is 6.4, and the camera width is less than the effective content width of the game, that is, cameraSize. width <gameValidContentSize. width = gameContentSize. width. The game content is cut by the camera!

The following is an example of a small game I have developed. We can see this problem more clearly:

The first one is the effect of running on 640*960 devices. Everything works normally. The second figure shows the effect of running on 640*1136 devices. You can see that the game content is obviously cut, and the buttons in the upper right corner are almost lost.

How can this problem be solved? The most direct idea is that if our game is on a 640*1136 screen device and the camera width is still 6.4, it will definitely not be cut. To do this, we must adjust the orthographicSize value of camera at runtime. The method is very simple. Use the formula described above:

AspectRatio = 9/16 to make cameraSize. width = 6.4, we calculate cameraSize. height = cameraSize. width/aspectRatio = 6.4*16/9. height/2 = 5.69

When we run the game again, the value of orthographicSize of camera is modified to 5.69 dynamically. We can see that:

The aspect of width is completely displayed, but the "Black edge" appears in both the upper and lower sides (here is the blue edge, huh, huh ). This is because the camera height is greater than the height of the game content, so there will naturally be NO content area, that is, "Black edge ". To solve this problem, we need to add up and down edges for the game. It is a method to directly share the same size as the black edges, but there is also a simpler method, directly enlarge the game background to cover the Black edge! This game is relatively simple. We use this simple method to enlarge the game background to 1.3 times, for example:

OK! Now our game looks quite normal and we have adapted the iPhone 5.

Note that the effective content area of our game is not equal to the actual content area at this time. If we enlarge the background image, it actually adds an external edge to the game ., The effective content area is inside the white box, And the invalid content area is outside the white box. The actual game content area is larger than the valid content area.

----------------------------------------- Split line of the final conclusion ------------------------------------------

According to the above process of solving the resolution problem, we can conclude that the actual resolution adaptation problem is related to three dimensions: Camera size, game content size (including valid content size and Invalid Content size) and actual screen size. To display the valid content we need without displaying black borders, we must ensure that:

The camera size must be smaller than or equal to the actual content size of the game and greater than or equal to the valid content size of the game. For example, a blue box indicates the camera size. We only need to ensure that the Blue Box is outside the white box, and the game content is correctly displayed in the figure.

As long as we can ensure this, our game will be able to cope with almost all screen resolutions.

According to this solution, the solution to screen resolution adaptation is actually to solve the problem of how to limit the size of the game camera to a given range.

To sum up, the steps are as follows: First, determine the effective content area and actual content area of the game. Then, when the game starts, adjust your camera size to the appropriate size for your game based on the actual screen aspect ratio.

----------------------------------------- The last split line ------------------------------------------

Recently I have written many games. This solution solves the problem of multi-resolution adaptation, especially for Android devices. The advantage of this method is that, as long as you understand it, the operation is very simple. On the other hand, it is different from the solution for zooming the game content. This method ensures the original flavor of the game content. Of course, there may also be some disadvantages. For the moment, it may be possible that changing the camera size may affect some games that require camera effects or operations. At present, there is little experience in this area, and we hope this solution can be continuously improved in the future.

Finally, I wrote a simple script to adjust the orthographicSize value, which is used to ensure that the width of camera is not smaller than the effective content width of the game. The valid content size is 6.4*9.6. This script takes effect as long as it is appended to the game's Camera.

 1 using UnityEngine; 2 using System.Collections; 3  4 public class GameCamera : MonoBehaviour { 5  6     float devHeight = 9.6f; 7     float devWidth = 6.4f; 8  9     // Use this for initialization10     void Start () {11     12         float screenHeight = Screen.height;13 14         Debug.Log ("screenHeight = " + screenHeight);15 16         //this.GetComponent
 
  ().orthographicSize = screenHeight / 200.0f;17 18         float orthographicSize = this.GetComponent
  
   ().orthographicSize;19 20         float aspectRatio = Screen.width * 1.0f / Screen.height;21 22         float cameraWidth = orthographicSize * 2 * aspectRatio;23 24         Debug.Log ("cameraWidth = " + cameraWidth);25 26         if (cameraWidth < devWidth)27         {28             orthographicSize = devWidth / (2 * aspectRatio);29             Debug.Log ("new orthographicSize = " + orthographicSize);30             this.GetComponent
   
    ().orthographicSize = orthographicSize;31         }32 33     }34     35     // Update is called once per frame36     void Update () {37     38     }39 }
   
  
 

Well, let's write so much. If you have any questions, you can leave a message or send a text message to me.

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.