留个档,Unity下,利用TexturePacker命令行自动化创建图集

需要大量处理优化合并工作的时候,筛选图片后一个一个去点,再处理引用太过于麻烦,于是需要接入一个可以unity里面操作Pack图集的方案。

so就有了下面的代码。

TexturePacker的命令行太复杂了,于是找个简单的办法,我们可以*的操作文本,那么先做好tps配置,让TexturePacker去Publish就好了。

注意一点: tps文件有格式要求需要无bom的UTF8

<?xml version="1.0" encoding="UTF-8"?>
<data version="1.0">
    <struct type="Settings">
        <key>fileFormatVersion</key>
        <int>1</int>
        <key>variation</key>
        <string>main</string>
        <key>verbose</key>
        <false/>
        <key>autoSDSettings</key>
        <array/>
        <key>allowRotation</key>
        <false/>
        <key>quiet</key>
        <false/>
        <key>premultiplyAlpha</key>
        <false/>
        <key>shapeDebug</key>
        <false/>
        <key>dpi</key>
        <uint>72</uint>
        <key>dataFormat</key>
        <string>json</string>
        <key>textureFileName</key>
        <filename>{-FileName-}.png</filename>
        <key>flipPVR</key>
        <false/>
        <key>ditherType</key>
        <enum type="SettingsBase::DitherType">NearestNeighbour</enum>
        <key>backgroundColor</key>
        <uint>0</uint>
        <key>libGdx</key>
        <struct type="LibGDX">
            <key>filtering</key>
            <struct type="LibGDXFiltering">
                <key>x</key>
                <enum type="LibGDXFiltering::Filtering">Linear</enum>
                <key>y</key>
                <enum type="LibGDXFiltering::Filtering">Linear</enum>
            </struct>
        </struct>
        <key>shapePadding</key>
        <uint>{-shapePadding-}</uint>
        <key>jpgQuality</key>
        <uint>80</uint>
        <key>pngOptimizationLevel</key>
        <uint>0</uint>
        <key>textureSubPath</key>
        <string></string>
        <key>textureFormat</key>
        <enum type="SettingsBase::TextureFormat">png</enum>
        <key>borderPadding</key>
        <uint>{-borderPadding-}</uint>
        <key>maxTextureSize</key>
        <QSize>
            <key>width</key>
            <int>{-width-}</int>
            <key>height</key>
            <int>{-height-}</int>
        </QSize>
        <key>fixedTextureSize</key>
        <QSize>
            <key>width</key>
            <int>{-width-}</int>
            <key>height</key>
            <int>{-height-}</int>
        </QSize>
        <key>reduceBorderArtifacts</key>
        <false/>
        <key>algorithmSettings</key>
        <struct type="AlgorithmSettings">
            <key>algorithm</key>
            <enum type="AlgorithmSettings::AlgorithmId">MaxRects</enum>
            <key>freeSizeMode</key>
            <enum type="AlgorithmSettings::AlgorithmFreeSizeMode">Best</enum>
            <key>sizeConstraints</key>
            <enum type="AlgorithmSettings::SizeConstraints">AnySize</enum>
            <key>forceSquared</key>
            <false/>
            <key>forceWordAligned</key>
            <false/>
            <key>maxRects</key>
            <struct type="AlgorithmMaxRectsSettings">
                <key>heuristic</key>
                <enum type="AlgorithmMaxRectsSettings::Heuristic">Best</enum>
            </struct>
            <key>basic</key>
            <struct type="AlgorithmBasicSettings">
                <key>sortBy</key>
                <enum type="AlgorithmBasicSettings::SortBy">Best</enum>
                <key>order</key>
                <enum type="AlgorithmBasicSettings::Order">Ascending</enum>
            </struct>
        </struct>
        <key>andEngine</key>
        <struct type="AndEngine">
            <key>minFilter</key>
            <enum type="AndEngine::MinFilter">Linear</enum>
            <key>packageName</key>
            <string>Texture</string>
            <key>javaFileName</key>
            <filename>{-FileName-}.java</filename>
            <key>wrap</key>
            <struct type="AndEngineWrap">
                <key>s</key>
                <enum type="AndEngineWrap::Wrap">Clamp</enum>
                <key>t</key>
                <enum type="AndEngineWrap::Wrap">Clamp</enum>
            </struct>
            <key>magFilter</key>
            <enum type="AndEngine::MagFilter">MagLinear</enum>
        </struct>
        <key>dataFileName</key>
        <filename>{-FileName-}.txt</filename>
        <key>mainExtension</key>
        <string></string>
        <key>forceIdenticalLayout</key>
        <false/>
        <key>outputFormat</key>
        <enum type="SettingsBase::OutputFormat">RGBA8888</enum>
        <key>contentProtection</key>
        <struct type="ContentProtection">
            <key>key</key>
            <string></string>
        </struct>
        <key>autoAliasEnabled</key>
        <true/>
        <key>trimSpriteNames</key>
        <false/>
        <key>globalSpriteSettings</key>
        <struct type="SpriteSettings">
            <key>scale</key>
            <double>1</double>
            <key>scaleMode</key>
            <enum type="ScaleMode">Smooth</enum>
            <key>innerPadding</key>
            <uint>0</uint>
            <key>extrude</key>
            <uint>0</uint>
            <key>trimThreshold</key>
            <uint>1</uint>
            <key>trimMode</key>
            <enum type="SpriteSettings::TrimMode">{-TrimMode-}</enum>
            <key>heuristicMask</key>
            <false/>
        </struct>
        <key>fileList</key>
        <array>
{-filename-}
        </array>
        <key>ignoreFileList</key>
        <array/>
        <key>replaceList</key>
        <array/>
        <key>ignoredWarnings</key>
        <array/>
        <key>commonDivisorX</key>
        <uint>1</uint>
        <key>commonDivisorY</key>
        <uint>1</uint>
    </struct>
</data>

using UnityEngine;
using System.IO;
using System.Text;
using System.Diagnostics;
public class TexturePackerCommandBuild
{
    private const string KEY_WIDTH = "{-width-}";
    private const string KEY_HEIGHT = "{-height-}";
    private const string KEY_FILE_PATH = "{-filename-}";
    private const string KEY_SHAPE_PADDING = "{-shapePadding-}";
    private const string KEY_BORDER_PADDING = "{-borderPadding-}";
    private const string KEY_TRIM_MODE = "{-TrimMode-}";

    private const string KEY_FILE_NAME = "{-FileName-}";

    private static string m_strTemplateTPS = "Assets/Editor/Helper/template.tps";
    private static string m_strTexturePackerExe = "E:/TexturePacker/bin/TexturePacker.exe";
    public enum TrimMode
    {
        None,
        Trim,
    }
    public static void MakeTPS(string strPath, int nWidth, int nHeight, string[] filePaths,
        int shapePadding = 2, int borderPadding = 2, TrimMode trimMode = TrimMode.Trim)
    {
        string strTargetPath = m_strTemplateTPS.Replace("Assets", Application.dataPath);
        if (!File.Exists(strTargetPath))
        {
            UnityEngine.Debug.Log("TexturePackerCommandBuild MakeTPS, not exit file : " + strTargetPath);
            return;
        }

        string strData = File.ReadAllText(strTargetPath, Encoding.UTF8);
        strData = strData.Replace(KEY_WIDTH, nWidth.ToString());
        strData = strData.Replace(KEY_HEIGHT, nHeight.ToString());

        StringBuilder strBuilder = new StringBuilder();
		for (int i = 0; i < filePaths.Length; i++)
        {
            strBuilder.AppendLine("<filename>" + filePaths[i] + "</filename>");
        }
        strData = strData.Replace(KEY_FILE_PATH, strBuilder.ToString());

        strData = strData.Replace(KEY_SHAPE_PADDING, shapePadding.ToString());
        strData = strData.Replace(KEY_BORDER_PADDING, borderPadding.ToString());
        strData = strData.Replace(KEY_TRIM_MODE, trimMode.ToString());

        string strFileNameKey = Path.GetFileNameWithoutExtension(strPath);
        strData = strData.Replace(KEY_FILE_NAME, strFileNameKey);

        Encoding eutf8 = new UTF8Encoding(false);   // 不带bom
        File.WriteAllText(strPath, strData, eutf8);
    }

    public static void BuildByTPS(string strTPSPath)
    {
        RunCMD(m_strTexturePackerExe, strTPSPath);
    }

    public static void RunCMD(string strExe, params string[] strParams)
    {
        string strInput = strExe;
		for (int i = 0; i < strParams.Length; i++)
		{
            strInput += " " + strParams[i];
        }

        using (Process p = new Process())
        {
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.FileName = "cmd.exe";
            p.Start();
            p.StandardInput.WriteLine(strInput);
            p.StandardInput.WriteLine("exit");
            p.WaitForExit();

            string s = p.StandardError.ReadToEnd();
            if (!string.IsNullOrEmpty(s))
            {
                UnityEngine.Debug.Log(s);
            }
        }
    }
}

程序学无止尽。
欢迎大家沟通,有啥不明确的,或者不对的,也可以和我私聊
我的QQ 334524067 神一般的狄狄

上一篇:Unity中使用全局变量


下一篇:Unity截屏或Render Texture渲染后的图片透明物体出错的问题及解决方法