栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > Unity3D

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

Unity3D 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

so就有了下面的代码。

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

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



    
        fileFormatVersion
        1
        variation
        main
        verbose
        
        autoSDSettings
        
        allowRotation
        
        quiet
        
        premultiplyAlpha
        
        shapeDebug
        
        dpi
        72
        dataFormat
        json
        textureFileName
        {-FileName-}.png
        flipPVR
        
        ditherType
        NearestNeighbour
        backgroundColor
        0
        libGdx
        
            filtering
            
                x
                Linear
                y
                Linear
            
        
        shapePadding
        {-shapePadding-}
        jpgQuality
        80
        pngOptimizationLevel
        0
        textureSubPath
        
        textureFormat
        png
        borderPadding
        {-borderPadding-}
        maxTextureSize
        
            width
            {-width-}
            height
            {-height-}
        
        fixedTextureSize
        
            width
            {-width-}
            height
            {-height-}
        
        reduceBorderArtifacts
        
        algorithmSettings
        
            algorithm
            MaxRects
            freeSizeMode
            Best
            sizeConstraints
            AnySize
            forceSquared
            
            forceWordAligned
            
            maxRects
            
                heuristic
                Best
            
            basic
            
                sortBy
                Best
                order
                Ascending
            
        
        andEngine
        
            minFilter
            Linear
            packageName
            Texture
            javaFileName
            {-FileName-}.java
            wrap
            
                s
                Clamp
                t
                Clamp
            
            magFilter
            MagLinear
        
        dataFileName
        {-FileName-}.txt
        mainExtension
        
        forceIdenticalLayout
        
        outputFormat
        RGBA8888
        contentProtection
        
            key
            
        
        autoAliasEnabled
        
        trimSpriteNames
        
        globalSpriteSettings
        
            scale
            1
            scaleMode
            Smooth
            innerPadding
            0
            extrude
            0
            trimThreshold
            1
            trimMode
            {-TrimMode-}
            heuristicMask
            
        
        fileList
        
{-filename-}
        
        ignoreFileList
        
        replaceList
        
        ignoredWarnings
        
        commonDivisorX
        1
        commonDivisorY
        1
    


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("" + filePaths[i] + "");
        }
        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 神一般的狄狄

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/900698.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号