Unity的C#參數傳遞
在使用Unity命令行時候用到參數時候,需要將參數從外部傳遞,在C#中讀取。
例如一個shell調用Unity的命令:
# [unity path] -quit -projectpath [project path] -executeMethod [class.method] [platfotm]
其中[platform]
就是傳遞的參數,需要在C#中接收,接收方如下:
string[] args = Environment.GetCommandLineArgs();
string platform = args [args.Length - 1];
args是shell命令以空格分割的數組,最後一個是需要傳遞的參數,所以取index爲args.Length - 1的就是platform參數,如果參數多,就依次類推,取倒數第二個、倒數第三個…
Python腳本的參數傳遞
調用python腳本直接使用shell或bat命令可以:
python xxx/xxx/xxx/test.python
需要傳遞參數就變爲:
python xxx/xxx/xxx/test.python param1 param2
只需要在python文件路徑後面加上參數即可,用空格分割。
在python腳本中接收參數需要用:
import sys
param1 = sys.argv[1]
param2 = sys.argv[2]
sys.argv
是將shell命令中除去‘python’後以空格分割的數組,index=0是python腳本的路徑,index=1之後的是對應爲的參數。