注意:对于跨平台项目,请使用 UNITY_ANDROID
#define 指令按条件编译 Android 特定的 C# 代码。请参阅有关平台相关的编译的文档以了解更多信息。
应用程序可以通过 Input 和 Handheld 类来访问 Android 设备的大多数功能。有关更多信息,请参阅:
可通过调用 Handheld.Vibrate 来触发振动。不含振动硬件的设备将忽略此调用。
移动操作系统具有内置活动指示器;应用程序可在慢速操作期间使用这些指示器。有关更多信息,请参阅 Handheld.StartActivityIndicator。
要访问设备特有的属性,请使用以下脚本:
脚本 | 设备属性 |
---|---|
SystemInfo.deviceUniqueIdentifier | 始终返回 ANDROID_ID 的 md5。有关更多信息,请参阅 Android 开发者文档的 ANDROID_ID 部分。 |
SystemInfo.deviceName | 返回设备名称。对于 Android 设备,Unity 会尝试从安全系统设置中读取 device_name 和 bluetooth_name 。如果这些字符串没有值,Unity 将返回 <unknown> 。 |
SystemInfo.deviceModel | 返回设备型号。这通常包括制造商名称和型号(例如“LGE Nexus 5”或“SAMSUNG-SM-G900A”)。 |
SystemInfo.operatingSystem | 返回操作系统名称和版本。 |
为了获得有关用户的许可信息并防止盗版,Google 提供了一项称为 Google Play 应用许可 (Google Play Application Licensing) 的服务。请参阅 Asset Store 上的 Google Play 应用许可证验证 (Google Play Application License Verification) 示例插件,查看该示例如何将此功能集成到 Unity 应用程序中。还可以从 GitHub 代码仓库下载插件源代码。
可在 iOS 和 Android 设备上控制应用程序的屏幕方向。检测方向变化或强制使用特定方向对于创建一些取决于用户如何握持设备的游戏行为很有用。
通过访问 Screen.orientation 属性来获取设备方向。允许的方向如下:
方向 | 行为 |
---|---|
Portrait | 设备处于纵向模式,直立握持设备,主屏幕按钮位于底部。 |
PortraitUpsideDown | 设备处于纵向模式,但是上下颠倒,直立握持设备,主屏幕按钮位于顶部。 |
LandscapeLeft | 设备处于横向模式,直立握持设备,主屏幕按钮位于右侧。 |
LandscapeRight | 设备处于横向模式,直立握持设备,主屏幕按钮位于左侧。 |
将 Screen.orientation 设置为上述方向之一,或使用 ScreenOrientation.AutoRotation 来控制屏幕方向。启用自动旋转后,仍可根据具体情况禁用某个方向。
使用以下脚本来控制自动旋转:
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.