If your application’s framebuffer orientation doesn’t match the device’s native display orientation (portrait, for most devices), Android rotates the application’s framebuffer to match the device’s display every frame. Depending on the device’s hardware capabilities, this additional rotation can negatively affect performance. If your application uses the Vulkan Graphics API and the device supports Vulkan, Unity can apply this rotation during rendering which reduces the performance impact of the rotation. This is called pre-rotation.
To make Unity apply pre-rotation, you can use C# scripts or the Unity editor:
Through C# scripts: Set PlayerSettings.vulkanEnablePreTransform to true
.
Through the Unity Editor:
In the Project settings window, select the Player tab, then open Android Player Settings:
In the Other Settings section, enable Apply display rotation during rendering.
Unity applies pre-rotation when it renders directly to the device’s backbuffer, not when it renders to a Render Texture. To apply the rotation, Unity modifies the projection matrix which affects the UNITY_MATRIX_MVP
and UNITY_MATRIX_P
Built-in shader variables. This means that Unity applies the rotation in the vertex shader.
Using pre-rotation doesn’t affect the behavior of Unity’s C# API. For example, you can still use Screen.width
to access the width of the screen. The same applies to viewports and scissor rects. Unity adjusts these as needed, and also handles readback operations from the backbuffer such as Grab Pass, ReadPixels, or Screenshot.
Unity provides utility macros to handle special cases in shaders (for more information, see the Limitations section below).
仅当以下所有条件都为真时才定义宏 UNITY_PRETRANSFORM_TO_DISPLAY_ORIENTATION(否则,未定义):
preTransform
在播放器设置中启用UNITY_DISPLAY_ORIENTATION_PRETRANSFORM 是一个常量,设置为当前 preTransform
旋转。它的值是以下之一:
UNITY_DISPLAY_ORIENTATION_PRETRANSFORM_0
UNITY_DISPLAY_ORIENTATION_PRETRANSFORM_90
UNITY_DISPLAY_ORIENTATION_PRETRANSFORM_180
UNITY_DISPLAY_ORIENTATION_PRETRANSFORM_270
如果 UNITY_PRETRANSFORM_TO_DISPLAY_ORIENTATION 未定义,或者当渲染到渲染纹理时,UNITY_DISPLAY_ORIENTATION_PRETRANSFORM 的值是 UNITY_DISPLAY_ORIENTATION_0。
UNITY_DISPLAY_ORIENTATION_PRETRANSFORM is translated into a Vulkan specialization constant, which makes it efficient to use in if or switch statements.
在以下情况下,如果启用 preTransform,则 Unity 项目可能需要额外修改,然后才能使用:
SV_Position
)这些情况仅适用于直接渲染到后备缓冲区时。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.