wpf 游戏开发 - 如何将“命令行参数”传递给 Unity 独立构建?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39843039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Game Development - How could I pass 'command line arguments' to a Unity standalone build?
提问by Firedan1176
Standalone/Client/Exe/Player/Build all refer to the executable the end user will use to run my game.
Standalone/Client/Exe/Player/Build 都是指最终用户将用来运行我的游戏的可执行文件。
I'm using Unity as my choice of environment. I'm creating a launcher to start the game. This sounds very redundant, as unity has a "launcher" window, for their standalone builds, but this purpose is to ensure the game is up-to-date by checking for the latest version on a server. I've built the client using a simple WPF application. At this point, I'm trying to think about my various options for making sure the executable for the game is started onlyfrom the launcher. However, Unity's standalone builds only accept a few range of arguments. Here's what I'm considering:
我使用 Unity 作为我的环境选择。我正在创建一个启动器来启动游戏。这听起来很多余,因为 unity 有一个“启动器”窗口,用于它们的独立构建,但这样做的目的是通过检查服务器上的最新版本来确保游戏是最新的。我已经使用一个简单的 WPF 应用程序构建了客户端。在这一点上,我试图考虑我的各种选项,以确保游戏的可执行文件仅从启动器启动。但是,Unity 的独立构建仅接受少数参数范围。这是我正在考虑的:
- Keep the executable hidden as two separate non executablefiles that are temporarily combined using
copy /bin command line, and outputting as an exe (then deleting after startup). The exe's are no more than 17mb, so it shouldn't take too long. - Find some way to modify the .exe to check for a specific command-line argument to be passed (Unity's runtime doesn't let the developer have access to the arguments after being built. 'Security issues'???)
- Create a temp file that stores either a timestamp or some unique identifier that the standalone game will check to make sure it's launched from the launcher (Only problem is throwing an error for Windows to catch that will say 'You must start the game from the launcher.' or something similar)
- 将可执行文件隐藏为两个单独的非可执行文件,
copy /b在命令行中使用临时组合,并作为 exe 输出(然后在启动后删除)。exe 不超过 17 mb,所以它不应该花太长时间。 - 找到某种方法来修改 .exe 以检查要传递的特定命令行参数(Unity 的运行时不允许开发人员在构建后访问这些参数。“安全问题”???)
- 创建一个临时文件,存储独立游戏将检查的时间戳或某些唯一标识符,以确保它是从启动器启动的(唯一的问题是抛出一个错误让 Windows 捕捉到它会说“你必须从启动器启动游戏”) .' 或类似的东西)
I really want to use this launcher because:
我真的很想使用这个启动器,因为:
- It will ensure players' game is updated, so they cannot cheat/exploit
- It will allow the game to be installed using a small 5-10mb launcher instead of downloading the whole game
- It's cool
- 它将确保玩家的游戏更新,因此他们不能作弊/利用
- 它将允许使用 5-10mb 的小型启动器安装游戏,而不是下载整个游戏
- 这个很酷
If you have any other suggestions, or maybe could guide me the right way, let me know. Thank you.
如果您有任何其他建议,或者可以指导我正确的方式,请告诉我。谢谢你。
回答by Geordie
Answered here: http://answers.unity3d.com/questions/366195/parameters-at-startup.html
在这里回答:http: //answers.unity3d.com/questions/366195/parameters-at-startup.html
And from here: https://effectiveunity.com/articles/making-most-of-unitys-command-line/
从这里开始:https: //effectiveunity.com/articles/making-most-of-unitys-command-line/
// Helper function for getting the command line arguments
private static string GetArg(string name)
{
var args = System.Environment.GetCommandLineArgs();
for (int i = 0; i < args.Length; i++)
{
if (args[i] == name && args.Length > i + 1)
{
return args[i + 1];
}
}
return null;
}
You can use System.Environment.GetCommandLineArgs() to access the commandline args in a Unity build.
您可以使用 System.Environment.GetCommandLineArgs() 在 Unity 构建中访问命令行参数。
回答by Bamdad
Not the exact answer, but I needed a launcher for my recent project too.
So I designed my own communication system between the launcher app and the game.
As we open the launcher (a C# Windows Forms Application, looks like a Bethesda game launcher) it begins to listen to a specific port (e.g. 23076 TCP). Then as we open the game, it connects to the launcher server and the launcher will send some data to the game, such as: Is the launcher running? Chosen Resolution, Chosen Quality, Chosen Language, Controls settings, etc.
So we can simply set our options in the launcher and send them to the game trough a TCP connection.
不是确切的答案,但我最近的项目也需要一个启动器。所以我设计了自己的启动器应用程序和游戏之间的通信系统。
当我们打开启动器(C# Windows 窗体应用程序,看起来像 Bethesda 游戏启动器)时,它开始侦听特定端口(例如 23076 TCP)。然后当我们打开游戏时,它会连接到启动器服务器,启动器会向游戏发送一些数据,例如:启动器是否正在运行?选择分辨率、选择质量、选择语言、控制设置等。
因此我们可以简单地在启动器中设置我们的选项,并通过 TCP 连接将它们发送到游戏。
Edit:
If you're looking for more info about this approach, take a look at LitenetLIBlibrary.
It supports Unity and Windows Forms, and I use it to make a connection between my launcher and my game. I also check the validity of the license using my launcher before entering the game.
编辑:
如果您正在寻找有关此方法的更多信息,请查看LitenetLIB库。它支持 Unity 和 Windows Forms,我用它在我的启动器和我的游戏之间建立连接。在进入游戏之前,我还会使用我的启动器检查许可证的有效性。
回答by David
Unity ships with a command linetool.
Unity 附带了一个命令行工具。
Unity is usually launched by double-clicking its icon from the desktop. However, it is also possible to run it from the command line (from the macOS Terminal or the Windows Command Prompt). When launched in this way, Unity can receive commands and information on startup, which can be very useful for test suites, automated builds and other production tasks.
通常通过双击桌面上的图标来启动 Unity。但是,也可以从命令行(从 macOS 终端或 Windows 命令提示符)运行它。当以这种方式启动时,Unity 可以在启动时接收命令和信息,这对于测试套件、自动化构建和其他生产任务非常有用。
One of particular argument which might be of your specific interest is:
您可能特别感兴趣的特定论点之一是:
-batchmode Run Unity in batch mode. This should always be used in conjunction with the other command line arguments, because it ensures no pop-up windows appear and eliminates the need for any human intervention. When an exception occurs during execution of the script code, the Asset server updates fail, or other operations that fail, Unity immediately exits with return code 1.
-batchmode 以批处理模式运行 Unity。这应该始终与其他命令行参数结合使用,因为它确保不会出现弹出窗口并消除任何人为干预的需要。当脚本代码执行过程中发生异常、资产服务器更新失败或其他操作失败时,Unity立即退出并返回代码1。
Note that in batch mode, Unity sends a minimal version of its log output to the console. However, the Log Files still contain the full log information. Opening a project in batch mode while the Editor has the same project open is not supported; only a single instance of Unity can run at a time
请注意,在批处理模式下,Unity 将其日志输出的最小版本发送到控制台。但是,日志文件仍包含完整的日志信息。不支持在编辑器打开相同项目时以批处理模式打开项目;一次只能运行一个 Unity 实例

