Unity 集成 Python 2.7 环境

    xiaoxiao2022-07-13  157

    原因

    Unity 使用 protobuf 来做网络传输和数据配置,一直使用 bat 的方式来调用生成数据,但是在 mac上无法使用,非常不便。跨平台的工具语言能够更方便进行进行不同平台的操作。使用 bat 的方式,代码难写,且难以复用。但是 Windows 下没有提供 Python 环境。Mac 平台已经自带了 Python 环境,所以只需在 Windows 环境集成 Python 环境即可。

    方案

    安装WinPython-64bit-2.7.13.1Zero.exe,将文件夹拷到工程目录下。 注意,调用 Python 的时候,要带参数-E,以免跟系统安装的 Python 进行混淆。

    使用举例

    安装模块命令行,例如:

    tools\WinPython\python>Scripts\pip.exe install protobuf==2.6.1

    将 protobuf 的生成修改成 Python 方式,注意不同平台的调用有所不同:

    #! /usr/bin/env python #coding=utf-8 import sys import os import traceback import subprocess class ProtogenOperator: def __init__(self): self.isWin = False toolPath = "../../tools/Protobuf/" self.monoPath = "/Library/Frameworks/Mono.framework/Versions/Current/bin/mono" if sys.platform == "win32" : toolPath= "..\\..\\tools\\Protobuf\\" self.isWin = True self.protogenPath = toolPath def DoProtogen(self, argValue): try : if self.isWin: command = self.protogenPath + "protogen " + argValue os.system(command) else: p = subprocess.Popen(self.monoPath + " " + toolPath + "protogen.exe " + argValue, shell = False) p.wait() except BaseException, e : print "protogen failed!" return False return True
    最新回复(0)