1、首先看unity怎么接收命令行的参数
使用System.Environment.GetCommandLineArgs()
接受命令行参数,这是C#的方法,官方文档:
Environment.GetCommandLineArgs Method
2、如何传递参数
实际上传进去的参数都是字符串,用空格
分开,然后接收到的为一个string数组,看如下项目:
C#脚本,绑定在一个unity的物体上
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class StartGame : MonoBehaviour
{
//untiy界面上显示的Text
public Text text;
//接受参数
private string[] args = System.Environment.GetCommandLineArgs();
void Awake()
{
text.text = "";
}
// Start is called before the first frame update
void Start()
{
Debug.Log("参数");
foreach (string s in args)
{
Debug.Log(s);
//分行打印
text.text += s + "\n";
}
}
// Update is called once per frame
void Update()
{
}
}
然后build,在cmd中执行你的执行文件.exe username:test1 password:123
执行效果如下,参数都拿到了,干什么不行
3、如何启动多个
直接在cmd中执行XXX.exe
要等到这个exe文件执行完毕才能执行下一句,进行测试的时候一个脚本开启多个客户端,这样写不行,要在前面加一个start
就行了,cmd不会等到执行结束再执行下一句,如下所示
start XXX.exe 参数1 参数2 ...