Version: 2023.1
Developing for iOS
Input for iOS devices

iOS Scripting

iOS 设备的大多数功能都通过 InputHandheld 类公开。跨平台项目通过定义 UNITY_IPHONE 按条件编译 iOS 特定的 C# 代码。

设备属性

您可以访问设备特有的许多属性。请参阅以下脚本参考:

反盗版检查

破解应用程序的一种常见方法是移除 AppStore DRM 保护,然后进行免费重新分发。Unity 附带反盗版检查功能,用于查明应用程序在提交到 AppStore 之后是否被篡改。

可以使用 Application.genuine 属性检查应用程序是否为正版(未被破解)。如果该属性返回 false,则可警告用户他们正在使用经过破解的应用程序,或者可以禁用该应用程序的某些功能。

注意:使用 Application.genuineCheckAvailable 以及 Application.genuine 可验证应用程序完整性。访问 Application.genuine 属性是一项资源密集型操作,不应在帧更新期间或运行其他时间迫切的代码时执行该操作。

振动支持

可通过调用 Handheld.Vibrate 来触发振动。不含振动硬件的设备将忽略此调用。

活动指示器

移动操作系统具有内置活动指示器;可在慢速操作期间使用这些指示器。有关示例,请参阅 Handheld.StartActivityIndicator 文档

屏幕方向

可在 iOS 和 Android 设备上控制应用程序的屏幕方向。通过检测方向变化或强制使用特定方向,可以创建一些取决于用户如何握持设备的应用程序行为。

要获取设备方向,请访问 Screen.orientation 属性。允许的方向如下:

方向 行为
Portrait 设备处于纵向模式,直立握持设备,主屏幕按钮位于底部。
PortraitUpsideDown 设备处于纵向模式,但是上下颠倒,直立握持设备,主屏幕按钮位于顶部。
LandscapeLeft 设备处于横向模式,直立握持设备,主屏幕按钮位于右侧。
LandscapeRight 设备处于横向模式,直立握持设备,主屏幕按钮位于左侧。

Set Screen.orientation to one of the above orientations or use ScreenOrientation.AutoRotation to control the screen orientation. When you enable autorotation, you can still disable some orientations on a case-by-case basis.

For more information, refer to the following API documentation:

The screen orientation of your application may vary depending on the device orientation set by the user, regardless of your application’s default orientation setting.

The following table shows the default screen orientation set for your application and the actual orientation the application loads in based on the device orientation.

Default screen orientation: Autorotate Default screen orientation: Portrait Default screen orientation: Landscape
Device orientation: Autorotate The application screen loads in portrait and can rotate between portrait, landscape right, and landscape left orientations (excluding portrait upside down orientation).
Note: On an iPad, the application screen loads in portrait or landscape orientation based on the device orientation. The screen can rotate between portrait, portrait upside down, landscape right, and landscape left orientations.
The application screen loads and remains locked in portrait orientation. Although portrait upside down orientation is allowed, the screen doesn’t rotate to that orientation.
Note: On an iPad, the application screen loads in portrait and can rotate between portrait and portrait upside down orientations. The splash screen can load in landscape orientation if the device orientation is landscape on launch.
The splash screen loads in the same orientation as the device orientation. The application screen loads in landscape when the scene loads and can rotate between landscape left or landscape right orientations.
Note: On an iPad, the application screen loads in landscape, and can rotate between landscape left and landscape right orientations. The splash screen can load in portrait if the device orientation is portrait on launch.
Device orientation: Portrait lock The application screen loads and remains locked in portrait orientation.
Note: Whilst in portrait orientation, if you set the ScreenOrientation.AutoRotation property for portrait to false, the application screen rotates to landscape orientation. If you now set the ScreenOrientation.AutoRotation property for landscape to false, the screen orientation remains unchanged.
The application screen loads and remains locked in portrait orientation. The splash screen loads in portrait orientation, but the application screen loads in landscape when the scene loads. By default, the application screen remains locked in landscape left orientation.
Note: On an iPad, the splash screen loads in portrait orientation and the scene loads in landscape orientation. The application screen remains locked in landscape orientation.
Device orientation: Landscape lock Not applicable as iPhones don’t have landscape lock setting. If the user turns off the device autorotation setting whilst the application is in landscape, the application screen rotates to and remains locked in portrait orientation.
Note: On an iPad, the application loads and remains locked in landscape orientation.
Not applicable as iPhones don’t have landscape lock setting. The application remains locked in portrait orientation.
Note: On an iPad, the splash screen loads in landscape and the scene loads in portrait orientation. The application screen remains locked in portrait orientation.
Not applicable as iPhones don’t have landscape lock setting. The application screen loads in landscape and can remain locked in both landscape left or landscape right orientations when the application loads.
Note: On an iPad, the application screen loads in landscape and remains locked in either landscape left or landscape right orientation depending on the device orientation.

确定设备世代

不同的设备世代具有不同的性能并支持不同的功能。可使用 iOS.DeviceGeneration 属性来查询设备的世代。

显示屏凹口

在某些显示屏上,屏幕上的某些区域可能由于其他硬件占用该空间而被遮挡或无法正常工作。使用 Screen.cutouts 可以返回每个凹口周围的包围盒列表。

iOS devices don’t provide a native API to get the display cutout information, so the cutouts are hardcoded in the Xcode project for each available iOS device. You can modify existing data or add additional devices in the Unity Xcode project ReportSafeAreaChangeForView function, which is in the UnityView.mm file.

录制您的游戏回放

可以使用 ReplayKit 录制游戏的音频和视频,以及从设备的麦克风和摄像头捕获的音频和视频评论。

Developing for iOS
Input for iOS devices