Version: Unity 6.3 LTS (6000.3)
LanguageEnglish
  • C#

SwapchainPreTransform

enumeration

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

The rotations that Unity can apply during rendering to match the native orientation of the device's display panel.

On devices that support pre-rotation, Unity can render each frame to the backbuffer already rotated to the native orientation of the display panel. This saves the operating system from rotating the image again before it displays it. Use Screen.currentSwapChainPreTransform to find out which rotation Unity applies in the current frame. For more information, refer to Framebuffer orientation.

Each value is relative to the native orientation of the display panel, not to portrait or landscape. For example, a landscape app reports a 90-degree or 270-degree rotation on most phones, but no rotation on most tablets. Don't use these values to detect the screen orientation. Use Screen.orientation instead.

Each value matches a Vulkan surface transform (VkSurfaceTransformFlagBitsKHR). Vulkan defines these rotations as clockwise rotations of the image content.

A 90-degree or 270-degree rotation swaps the width and height of the native backbuffer relative to Screen.width and Screen.height. Unity adjusts the C# API, viewports, and scissor rectangles for you. You only need to handle the rotation yourself if you write directly to the backbuffer, for example from a native rendering plug-in, or in shaders that don't use Unity's projection matrix.

Additional resources: Screen.currentSwapChainPreTransform.

// Attach this script to a GameObject in a scene.
// This example logs the pre-rotation that Unity applies whenever it changes, for example when the user rotates the device.
using UnityEngine;
using UnityEngine.Rendering;

public class PreTransformLogger : MonoBehaviour { SwapchainPreTransform m_LastPreTransform;

void Start() { m_LastPreTransform = Screen.currentSwapChainPreTransform; Debug.Log("Initial pre-transform: " + m_LastPreTransform); }

void Update() { SwapchainPreTransform preTransform = Screen.currentSwapChainPreTransform; if (preTransform == m_LastPreTransform) return;

m_LastPreTransform = preTransform;

// A 90-degree or 270-degree rotation swaps the width and height of the native backbuffer. bool swapsWidthAndHeight = preTransform == SwapchainPreTransform.Rotate90 || preTransform == SwapchainPreTransform.Rotate270;

Debug.Log("Pre-transform changed to " + preTransform + ". Native backbuffer width and height swapped: " + swapsWidthAndHeight); } }

Properties

Property Description
IdentityNo rotation. The rendered image already matches the native orientation of the display panel.
Rotate90Unity rotates rendering by 90 degrees clockwise to match the native orientation of the display panel.
Rotate180Unity rotates rendering by 180 degrees to match the native orientation of the display panel.
Rotate270Unity rotates rendering by 270 degrees clockwise to match the native orientation of the display panel.