UGUI与NGUI的区别汇总

 

图集打包Atlas

制作

NGUI是必须先打出图集然后才能开始做界面。因为始终都要去考虑你的UI图集。比如图集会不会超1024 ,图集该如何来规划等等。

 

UGUI 让开发者彻底模糊图集的概念,让开发者不要去关心自己的图集。做界面的时候只用小图,而在最终打包的时候unity才会把你的小图和并在一张大的图集里面。然而这一切都是自动完成的,开发者不需要去care它。

 

NGUI在打包图集的时候图集的默认格式是RGBA32,也就是支持带透明通道的图片,这样一张1024的图集也就是4M内存。为了优化图集,我们可以选择把带透明通道图片 不带透明通道的图片分开打图集,这样可以减少内存的占用量。然而着一切的一切在NGUI上都需要手动操作,而SpritePacker则全自动完成。Sprite上的Packing Tag 同一标识的图片。

UGUI会把相同图片格式的图片打包成同一图集。MomoAtalsRUORUOAtlas就是Packing Tag的标识符,那么此时根据这两个标识符SpritePacker将打出两个图集出来。 因为MomoAtlas这些图片中,一部分是RGBA32格式,还有一部分是ETC 4bits格式,那么MomoAtlas将被在分成两个图集,就是尾缀带Group的。

 

 

打包Sprite Packer有两个打包模式

DefaultPackerPolicy:是默认的打包方式,也是矩形打包方式。他会把所有的小图按照矩形的方式来排列,如果宽高不一样的图片,它们会自动补起。

TightPackerPolicy:是紧密打包方式,也就是尽可能的把图片都打包在图集上,这种方式要比DefaultPackerPolicy打包的图片更多一些,也就是更省空间。

 

UGUI与NGUI的区别汇总_第1张图片

 

图中[TIGHT]开头的就表示单独这张图采用TightPackerPolicy打包模式。

 

自定义打包

假如我想自定义打包方式咋办?比如我想设置图片打包格式,或者图集大小等等怎么办?把如下代码放在Editor文件夹下, 在代码里面就可以设置图集的属性了。

 

using System;

using System.Linq;

using UnityEngine;

using UnityEditor;

using UnityEditor.Sprites;

using System.Collections.Generic;

   

// DefaultPackerPolicy will pack rectangles no matter what Sprite mesh type is unless their packing tag contains "[TIGHT]".

class DefaultPackerPolicySample : UnityEditor.Sprites.IPackerPolicy

{

    protected class Entry

    {

        public Sprite            sprite;

        public AtlasSettings     settings;

        public string            atlasName;

        public SpritePackingMode packingMode;

    }

    

    public virtual int GetVersion() { return 1; }

    

    protected virtual string TagPrefix { get { return "[TIGHT]"; } }

    protected virtual bool AllowTightWhenTagged { get { return true; } }

    

    public void OnGroupAtlases(BuildTarget target, PackerJob job, int[] textureImporterInstanceIDs)

    {

        List entries = new List();

        

        foreach (int instanceID in textureImporterInstanceIDs)

        {

            TextureImporter ti = EditorUtility.InstanceIDToObject(instanceID) as TextureImporter;

            

            TextureImportInstructions ins = new TextureImportInstructions();

            ti.ReadTextureImportInstructions(ins, target);

            

            TextureImporterSettings tis = new TextureImporterSettings();

            ti.ReadTextureSettings(tis);

            

            Sprite[] sprites = AssetDatabase.LoadAllAssetRepresentationsAtPath(ti.assetPath).Select(x => x as Sprite).Where(x => x != null).ToArray();

            foreach (Sprite sprite in sprites)

            {

                                //在这里设置每个图集的参数

                Entry entry = new Entry();

                entry.sprite = sprite;

                entry.settings.format = ins.desiredFormat;

                entry.settings.usageMode = ins.usageMode;

                entry.settings.colorSpace = ins.colorSpace;

                entry.settings.compressionQuality = ins.compressionQuality;

                entry.settings.filterMode = Enum.IsDefined(typeof(FilterMode), ti.filterMode) ? ti.filterMode : FilterMode.Bilinear;

                entry.settings.maxWidth = 1024;

                entry.settings.maxHeight = 1024;

                entry.atlasName = ParseAtlasName(ti.spritePackingTag);

                entry.packingMode = GetPackingMode(ti.spritePackingTag, tis.spriteMeshType);

                

                entries.Add(entry);

            }

            

            Resources.UnloadAsset(ti);

        }

        

        // First split sprites into groups based on atlas name

        var atlasGroups =

            from e in entries

                group e by e.atlasName;

        foreach (var atlasGroup in atlasGroups)

        {

            int page = 0;

            // Then split those groups into smaller groups based on texture settings

            var settingsGroups =

                from t in atlasGroup

                    group t by t.settings;

            foreach (var settingsGroup in settingsGroups)

            {

                string atlasName = atlasGroup.Key;

                if (settingsGroups.Count() > 1)

                    atlasName += string.Format(" (Group {0})", page);

                

                job.AddAtlas(atlasName, settingsGroup.Key);

                foreach (Entry entry in settingsGroup)

                {

                    job.AssignToAtlas(atlasName, entry.sprite, entry.packingMode, SpritePackingRotation.None);

                }

                

                ++page;

            }

        }

    }

    

    protected bool IsTagPrefixed(string packingTag)

    {

        packingTag = packingTag.Trim();

        if (packingTag.Length < TagPrefix.Length)

            return false;

        return (packingTag.Substring(0, TagPrefix.Length) == TagPrefix);

    }

    

    private string ParseAtlasName(string packingTag)

    {

        string name = packingTag.Trim();

        if (IsTagPrefixed(name))

            name = name.Substring(TagPrefix.Length).Trim();

        return (name.Length == 0) ? "(unnamed)" : name;

    }

    

    private SpritePackingMode GetPackingMode(string packingTag, SpriteMeshType meshType)

    {

        if (meshType == SpriteMeshType.Tight)

            if (IsTagPrefixed(packingTag) == AllowTightWhenTagged)

                return SpritePackingMode.Tight;

        return SpritePackingMode.Rectangle;

    }

}

 

虽然可以全选在设置图片的pack tag,但是我觉得最好全自动完成,比如我们把图片放在不同的文件夹下,那么文件夹的名子就可以用做Atals的名子。最后在分享一条这样的脚本。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

using UnityEngine;

using System.Collections;

using UnityEditor;

using System.IO;

   

public class  Post : AssetPostprocessor

{

   

    void OnPostprocessTexture (Texture2D texture)

    {

        string AtlasName =  new DirectoryInfo(Path.GetDirectoryName(assetPath)).Name;

        TextureImporter textureImporter  = assetImporter as TextureImporter;

        textureImporter.textureType = TextureImporterType.Sprite;

        textureImporter.spritePackingTag = AtlasName;

        textureImporter.mipmapEnabled = false;

    }

   

}

 

你可能感兴趣的:(UGUI与NGUI的区别汇总)