ファイルを残し、TexturePacker コマンド ラインを使用して、Unity でアトラスを自動的に作成します。

大量の最適化やマージ作業を処理する必要がある場合、写真を1枚ずつフィルタリングして参照を扱うのは面倒なので、Unity上でPack atlasを操作できるソリューションを接続する必要があります。

したがって、次のコードがあります。

TexturePacker のコマンドラインは複雑すぎるので、テキストを自由に操作できる簡単な方法を見つけて、最初に tps を設定して TexturePacker を公開します。

注: 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);
            }
        }
    }
}

プログラミングには終わりがありません。
誰でも気軽にコミュニケーションできます。不明な点や間違いがある場合は、個人的にチャットすることもできます。My
QQ 334524067 God-like Didi

おすすめ

転載: blog.csdn.net/qq_37776196/article/details/122091649