当从命令行运行 .exe 时,我想指定我的 Unity 游戏在哪个监视器上打开(1?2?...)。
如 Unity Answers 帖子所示 here ,此 C# 代码确实有效:
PlayerPrefs.SetInt("UnitySelectMonitor", 1); // Select monitor 2
但是,该设置只有在您关闭游戏并重新打开后才会生效。 (这个缺陷也在该帖子中得到承认。)
有没有办法在第一次尝试时在所需的显示器上打开游戏?
(注意:我假设解决方案需要在构建步骤中或启动 Unity Player 的 .exe 的命令行参数中。因为如果解决方案在 C# 代码本身中,如上述解决方案,然后游戏已经打开,然后就太晚了。)
最佳答案
从版本 2019.3 开始,使用 -monitor
command line argument .例如:
GameExecutable.exe -screen-fullscreen 1 -monitor 2
Unity > 2019.3.0b12、2020.1.0a12版本修复了同一个应用的多个实例同时运行的相关bug。来自bug tracker :
IMPORTANT: the "adapter" command-line argument has been replaced with "monitor" in 2019.3 (for consistency across Desktop platforms); the behavior is exactly the same.
NOTE: This issue also occurs for other "screen" command-line parameters: screen-width, screen-height, and screen-fullscreen which are also addressed by the fix.
The problem is caused because these command-line arguments will set/update PlayerPrefs, i.e. these values are persisted and applied the next time the app is launched. So, the second instance of the app essentially "inherits" the values from the first process, causing it to ignore it's own command-line arguments due to a race-condition with PlayerPrefs updating logic. However, if the second process is delayed by a few seconds, the race-condition doesn't occur and the command arguments work properly.
The fix address the race-condition by setting "override" values when these arguments are passed in, forcing the runtime to apply the command-line values and ignore those stored in PlayerPrefs.
However, PlayerPrefs is updated by the command arguments, which is still a race-condition, and there's no guarantee as to which values will actually be saved. That is, the values from the first instance of the app may get saved to PlayerPrefs and not the values from the second instance as you might expect. Unity cannot solve this problem as the race occurs externally to the runtime, and so if the final state of PlayerPrefs is important, then you must stagger launching each instance of the app.
关于c# - 第一次尝试时如何选择打开 Unity 游戏的显示器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59430488/