Unity 命令行通过python实时打印unity输出日志

unity的命令行只提供了将日志输出到文件中,不能够实时的看到日志的输出情况,也不知道进行到哪一步了,整个就是一盲盒了。为了解决这个问题,我使用python来实时输出日志,通过输入参数、执行子程序和多线程来实现,下面是python代码

# coding=utf-8
#命令行运行unity并实时在命令行窗口打印日志,根据参数执行不同的方法
import sys
import subprocess
import time
import threading
import os
import argparse

parser = argparse.ArgumentParser(description="Unity log realtime print")
parser.add_argument('-unity', required=True, help=u'Unity executable file path')
parser.add_argument('-project', required=True, help=u'Unity project path')
parser.add_argument('-method', required=True, help=u'Unity method to call')
parser.add_argument('-methodArg', required=True, help=u'Unity method arg to call')
parser.add_argument('-logFile',required=True,help=u'Unity Log path')
args = parser.parse_args()

exePath:str = args.unity # "\"C:/Program Files/Unity 2019.4.40f1/Editor/Unity.exe\""
methodPath:str = args.method # "StaticTest.Launch"
methodArg:str = args.methodArg #"Android"
projectPath:str = args.project # "D:/UnityPro/LifeGame"
logOutPath:str = args.logFile # "D:/UnityPro/LifeGame/test.log"
cmd  ="{0} -nographics -quit -batchmode -executeMethod {1} {2} -projectPath {3} -logfile {4}".format(exePath,methodPath,methodArg,projectPath,logOutPath)
# linkpath = os.path.dirname(projectPath) +"/{}".format(methodArg.split(" ")[0])
# mklineCmd = "mklink /D \"{0}\" \"{1}\"".format(linkpath,projectPath) 
# print(mklineCmd)
# if os.path.exists(linkpath):
#     os.remove(linkpath)
# process = subprocess.call(mklineCmd,shell=True,stdout = subprocess.PIPE)
if os.path.exists(logOutPath):
    os.remove(logOutPath)
process = subprocess.Popen(cmd,shell=True,stdout = subprocess.PIPE)

def tail_thread(proce):
    while True:
        if os.path.exists(logOutPath):
            break
    file = open(logOutPath,"r",encoding="utf-8")
    isFinish = False
    while True:
        pos = file.tell() #当前文件读取的位置
        lines = file.readlines()
        if pos != file.tell():#如果相等了表示读到结束了,需要等待新的内容写入
            for it in lines:
                print(it)
        if isFinish:#操作完成了
            break
        if proce.poll() != None:#poll 返回值不是None的时候表示cmd操作完成了,并返回错误码
            isFinish = True #完成了不直接关闭再等待一个循环免得内容没有读取全
        time.sleep(1)

newThread = threading.Thread(target = tail_thread,args=(process,))
newThread.start()


为了方便启动pyhon 使用bat 批处理命令执行,参数和上面python设置输入参数一致,main.py就是上面python代码的文件

@echo off
python main.py -unity "\"C:/Program Files/Unity 2019.4.40f1/Editor/Unity.exe\"" -project "D:/UnityPro/LifeGame" -logFile "D:/UnityPro/LifeGame/test.log" -method "StaticTest.Launch" -methodArg "Android"
pause

unity 端的代码,通过Environment.GetCommandLineArgs方式获取输入的参数,然后得到方法的参数,这个样就可以根据参数来执行不同的操作而不是通过方法名来区分

public static class StaticTest
{
    public static void Launch(){
        var infos = Environment.GetCommandLineArgs();
         Debug.Log($"auto cmd runing ");
         bool isArg = false;
         List args = new List();
         for(int i =0;i param)
    {
        if(param != null)
        {
            foreach(var it in param)
            {
                Debug.Log($"params is  {it}");
            }
        }
        
    }
}

这种方式不局限于python,c# ,c++ ,java等语言都可以,本人使用python仅仅是学习python的使用方式

你可能感兴趣的:(unity,游戏引擎,python)