Unity 同步guid脚本

18 篇文章 2 订阅
2 篇文章 0 订阅

美术和程序分裂两个工程相互需要保持结构一致,且guid一致。以下脚本校验一致性,并且将美术的文件夹中的guid强制同步成程序的资源guid.

# !/usr/bin/env python3
import os
import sys
import yaml
import chardet


def os_is_win32():
    return sys.platform == 'win32'


def folderPathFixEnd(path_str):
    if not os_is_win32():
        if not path_str[-1] == "/":
            return path_str + "/"
        else:
            return path_str
    if path_str.startswith("\\\\?\\"):
        return path_str
    ret = "\\\\?\\" + os.path.abspath(path_str)
    ret = ret.replace("//", "/")
    ret = ret.replace("/", "\\")
    return ret


def gci(folderPath_: str, fileFilter_: list, fileList_: list = None):
    if not os.path.isdir(folderPath_):
        sys.exit(1)
    files = os.listdir(folderPath_)
    for fi in files:
        fi_d = os.path.join(folderPath_, fi)
        if os.path.isdir(fi_d):
            gci(fi_d, fileFilter_, fileList_)
        else:
            _filePath = os.path.join(folderPath_, fi_d)
            _fileSuffix = os.path.splitext(_filePath)[1]
            if fileFilter_:
                if _filePath and not _fileSuffix == "" and (_fileSuffix in fileFilter_):
                    fileList_.append(_filePath)
            else:
                fileList_.append(_filePath)


def getFileListInFolder(folder_: str, filters_: list = None):
    _filePathList = []
    folder_ = folderPathFixEnd(folder_)
    if filters_ and len(filters_) > 0:
        for _i in range(len(filters_)):
            if filters_[_i] == "" or not filters_[_i][0] == ".":
                raise Exception("后缀不对")
                return None
    gci(
        folder_,
        filters_,
        _filePathList
    )
    return _filePathList


def getRelativePathToMetaDict(assetsPath_):
    _filePathList = getFileListInFolder(
        assetsPath_,
        [".png", ".jpg", ".mat", ".prefab", ".controller", ".anim", ".FBX", ".PNG", ".physicsMaterial2D"]
    )
    _relativePathToGuidDict = {}
    for _i in range(len(_filePathList)):
        _filePath = _filePathList[_i]
        _metaFilePath = _filePath + ".meta"
        _fs = open(_metaFilePath, encoding="UTF-8")
        _yamlData = yaml.load(_fs, Loader=yaml.FullLoader)
        _relativePath = _filePath.split(assetsPath_)[1]
        _relativePathToGuidDict[_relativePath] = _yamlData.guid
    return _relativePathToGuidDict


def getAllKnowTypeFileInAssets(assetsPath_):
    return getFileListInFolder(
        assetsPath_,
        [".meta",".mat", ".prefab", ".controller", ".anim", ".renderTexture", ".asset", ".fontsettings", ".physicsMaterial2D"]
    )


def readFromFile(filePath_: str):
    _encodeInfo = None
    try:
        _file = open(filePath_, 'rb')
        try:
            _encodeInfo = chardet.detect(_file.read())
        finally:
            _file.close()
    except Exception as e:
        print(filePath_, e)

    _encodeType = _encodeInfo["encoding"]
    _contentStr = None
    try:
        _file = open(file=filePath_, mode='r', encoding=_encodeType)
        try:
            _contentStr = _file.read()
        finally:
            _file.close()
    except Exception as e:
        print(filePath_, e)
        sys.exit(1)
    return _contentStr, _encodeType


def writeFileWithStr(filePath_: str, str_: str, encodeType_: str):
    if not os.path.exists(os.path.dirname(filePath_)):
        os.makedirs(os.path.dirname(filePath_))
    try:
        _file = open(file=filePath_, mode='w', encoding=encodeType_)
        try:
            _file.write(str_)
        finally:
            _file.close()
    except Exception as e:
        print(filePath_, e)


if __name__ == "__main__":
    # 程序工程 - 美术资源文件的相对路径和guid之间的关系
    _developAssetsPath = "程序Assets路径"
    _developRelativePathToGuidDict = getRelativePathToMetaDict(_developAssetsPath)

    # 美术工程 - 资源文件相对路径和guid之间的关系
    _artAssetsPath = "美术Asstes路径"
    _artRelativePathToGuidDict = getRelativePathToMetaDict(_artAssetsPath)

    # 美术工程,相同的相对路径guid的映射关系
    _inDevelopNotInArtList = []
    _inArtNotInDevelopList = []
    _artGuidToDevelopGuidDict = {}
    for _developRelativePath in _developRelativePathToGuidDict:
        if _developRelativePath in _artRelativePathToGuidDict:
            _guidInDevelop = _developRelativePathToGuidDict[_developRelativePath]
            _guidInArt = _artGuidToDevelopGuidDict[_developRelativePath]
            if not _guidInDevelop == _guidInArt:
                _artGuidToDevelopGuidDict[_guidInArt] = _guidInDevelop
        else:
            _inDevelopNotInArtList.append(_developRelativePath)

    for _artRelativePath in _artRelativePathToGuidDict:
        if not _artRelativePath in _developRelativePathToGuidDict:
            _inArtNotInDevelopList.append(_artRelativePath)

    # 打印两者差异
    print("在 开发 目录,不在 美术 目录 的文件有 : ")
    for _i in range(len(_inDevelopNotInArtList)):
        print("   " + _inDevelopNotInArtList[_i])
    print("在 美术 目录,不在 开发 目录 的文件有 : ")
    for _i in range(len(_inArtNotInDevelopList)):
        print("   " + _inArtNotInDevelopList[_i])

    # 打开美术工程,把所有的guid全部替换掉
    _artFilePathList = getAllKnowTypeFileInAssets(_artAssetsPath)
    for _i in range(len(_artFilePathList)):
        _artFilePath = _artFilePathList[_i]
        _contentStr, _encodeType = readFromFile(_artFilePath)
        for _key in _artGuidToDevelopGuidDict:
            _contentStr.replace(_key, _artGuidToDevelopGuidDict[_key])
        writeFileWithStr(_artFilePath, _contentStr, _encodeType)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值