由于项目需求,需要接收Linux命令行参数
接下来我们来看看怎么实现
首先我们打包一个linux执行程序,打包出来大致是这样
然后将程序放到linux中
Linux 部分:
正常情况运行程序:./Metavers_Server.x86_64
传参运行程序:./Metavers_Server.x86_64 -customPort 8080
传递了一个 -customPort 的参数,值为8080
Unity部分:
string[] commandLineArgs = System.Environment.GetCommandLineArgs();//获取命令行参数
for (int i = 0; i < commandLineArgs.Length; i++)
{
if (commandLineArgs[i] == "-customPort" && i + 1 < commandLineArgs.Length)//查找 -customPort 参数值
{
int customPort = 0;
if (int.TryParse(commandLineArgs[i + 1], out customPort))
{
Debug.Log(customPort);
//对customPort值进行操作
}
}
}
我这边是做服务器设置端口用的,效果如下: