---
title: 在运行时更改屏幕方向
slug: world-sdk-guide-ch/CaY_ScVIlsITQ1JbV-5Ex
docTags: 
createdAt: 2024-09-02T08:34:13.135Z
---

请在世界设置中设置默认显示的屏幕方向。

![](https://api.archbee.com/api/optimize/fCt3n1oCa8rgNJ8fw9I2N-pa_-ov_CvYaVn_DNHQzGJ-20240904-102413.png)

- 方向：选择屏幕的方向。
  - 垂直
  - 水平



以下是一个示例代码，用于在运行时更改屏幕方向。

仅支持两种屏幕方向：纵向和横向。

:::CodeblockTabs
ChangeOrientation

```typescript
import { ZepetoScriptBehaviour } from 'ZEPETO.Script';
import { Button } from 'UnityEngine.UI';
import { Screen, ScreenOrientation } from "UnityEngine";
  
  
export default class ChangeOrientation extends ZepetoScriptBehaviour {
  
    public orientationBtn: Button;
      
  
    Start() {
  
        // 当按钮被点击时
        this.orientationBtn.onClick.AddListener(() => {
             
            if(Screen.orientation == ScreenOrientation.Landscape)
            {
              	// 设置屏幕方向为纵向
                Screen.orientation = ScreenOrientation.Portrait;
            }
            else
            {
              	// 设置屏幕方向为横向
                Screen.orientation = ScreenOrientation.Landscape;
            } 
        });
  
    }
}
```
:::

:::hint{type="success"}
👍 **提示**

- 默认UI（主页按钮、聊天按钮）和虚拟按键在更改屏幕方向时自动支持横向-纵向切换。
- 如果您在内容中提供其他自定义UI，您需要响应方向的变化。
:::

