命令行启动多个unity,并且传递参数

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 ...

猜你喜欢

转载自blog.csdn.net/qq_40666620/article/details/107979077