Unity3D removes the black borders of the screen when the screen is full, unity3d removes the full screen
After the screen is displayed, a friend who does not care about stretching and deformation and still wants to fill the screen, searched the internet for a morning. There is really no relevant information, and he can only think about it by himself.
While Unity will automatically stretch the UI for us after using Canvas Scaler in full screen mode, the aspect ratio we set in Unity remains unchanged after stretching. Black borders in the screen need to be stretched and filled with code.
The following is my test environment:
Unity3D 5.3.4
Win 10 64bit
In the test, I set Unity to automatically stretch Based on the screen width. In this way, I need to adjust the UI height to fill in the black edges on the screen.
The resolution used in development is 16: 9, but after the actual release, it will be able to run on the, or even display, so that according to the above settings, unity automatically scales horizontally, but black edges appear on the upper and lower sides of the screen.
Place the following code in the Start function of the script on Camera or Canvas, and the UI will automatically stretch up and down to fill out the black edges in the upper and lower parts of the screen.
StandarRatio = new Vector3[RechangeForms.Length]; float referenceRatio = 16f/9f; float currentRatio =( (float)Screen.width / (float)Screen.height); for (int i = 0; i < RechangeForms.Length; i++) { StandarRatio[i] = RechangeForms[i].transform.localScale; float yFactor = StandarRatio[i].y * (referenceRatio / currentRatio); float posYFactor = RechangeForms[i].transform.position.y * (referenceRatio / currentRatio); RechangeForms[i].transform.DOScaleY(yFactor, 0.0f); RechangeForms[i].transform.DOMoveY(posYFactor, 0.0f); }
StandarRatio is an array of Vector3 used to store the Scale value of elements before stretching. RechangeForms is a set of elements to be stretched and a gameobject array.
The Code fills the Black edge by adjusting the value of Y in the Scale of the UI element.
Mathematical formula used: New YScale = original YScale * (reference resolution ratio/actual resolution ratio ).
There is an extra problem here, that is, after the Scale Y value of the UI element is adjusted, the center of the element is changed on the left, so the corresponding adjustment is also required. The formula is the same as above. (This may also be caused by my own project)
After completing the preceding steps, you can switch to various Billy monitors in the Unity preview window for testing, we can see that the original Black edge has been filled with the UI elements after being stretched (although the UI is changed, the Black edge is gone ).