Unity 入门操作 之 快速学会游戏运行不自动锁屏。游戏运行时由于手机设置,手机会自动锁屏,手机的自动锁屏影响了游戏的体验度,本节讲解如何在自己游戏运行时,不自动锁屏的操作,具体如下
工具/原料
Unity
一、基本概念
1、Screen.sleepTimeout:A power saving setting, allowing t茑霁酌绡he screen to dim some time after the last active user interaction.Most useful for handheld devices, allowing OS to preserve battery life in most efficient ways. Does nothing on non-handheld devices.sleepTimeout is measured in seconds. The default value varies from platform to platform, generally being non-zero.On mobile devices it would be useful to set sleepTimeout to [SleepTimeout.NeverSleep] for games using accelerometer as the main source of input. However, such games should allow screen dimming while in menu or paused. Currently you will only be able to set this property to one of the values predefined in [SleepTimeout] class. A get will return either one of the predefined values, or the actual number of seconds until screen gets dimmed, as specified in system preferences of the device.
二、入门操作 之 快速学会游戏运行不自动锁屏
1、打开Unity,新建一个空工程,然后Unity界面如下图
2、在工程中新建一个脚本,脚本可以命名为“SleepTest”,具体如下图
3、选中“SleepTest”脚本,双击脚本或者右键“Open C# Project”,打开脚本,具体如下图
4、在打开的脚本上进行代码编辑,首先设置一个cube变量,然后赋值,并设置屏幕neversleep,最后设置cube自转,具体代码及代码说明如下图
5、脚本编译正确后,回到Unity界面,在场景中新建一个“Cube”,然后把脚本“SleepTest”赋给“Cube”,具体如下图
6、保存场景,把工程Build安装到手机上,运行即可看到游戏不再自动黑屏了,具体如下图
7、到此,《Unity 入门操作 之 快速学会游戏运行不自动锁屏》讲解结束,谢谢