Beispiel #1
0
    static public void PackTexture(List <TextureImporter> imps, string atlasName, string atlasPath, string texturePath)
    {
        if (imps == null || imps.Count <= 0)
        {
            Debug.Log("选择打包的小图数量为0");
            return;
        }

        //检查文件夹是否存在
        if (!Directory.Exists(atlasPath))
        {
            Directory.CreateDirectory(atlasPath);
        }

        //检查文件夹是否存在
        if (!Directory.Exists(texturePath))
        {
            Directory.CreateDirectory(texturePath);
        }

        //图集预制相对路径
        string relativePath = atlasPath + atlasName + ".prefab";
        //图集预制绝对路径
        //string absolutePath = Path.GetFullPath(atlasPath) + atlasName + ".png";

        //纹理相对路径
        string relativeTexPath = texturePath + atlasName + ".png";
        //纹理绝对路径
        string absoluteTexPath = Path.GetFullPath(texturePath) + atlasName + ".png";

        //判断图片格式不对了,先进性格式的转换
        CheckFormat(imps);
        //加载图片资源
        Texture2D[] texs = LoadTextures(imps);

        //创建Atlas实例
        UIAtlas atlasInfo = ScriptableObject.CreateInstance <UIAtlas>();

        atlasInfo.name = atlasName;

        //打包小图到大纹理并返回uv信息
        Texture2D atlasTexture = new Texture2D(1, 1);

        Rect[] rs = atlasTexture.PackTextures(texs, (int)padding, (int)matAtlasSize);

        //把图集写入到磁盘文件
        File.WriteAllBytes(absoluteTexPath, atlasTexture.EncodeToPNG());

        //刷新图片
        AssetDatabase.Refresh();
        AssetDatabase.ImportAsset(relativeTexPath);

        //记录图片的名字,只是用于输出日志用;
        StringBuilder names = new StringBuilder();

        //SpriteMetaData结构可以让我们编辑图片的一些信息,想图片的name,包围盒border,在图集中的区域rect等
        SpriteMetaData[] sheet = new SpriteMetaData[rs.Length];
        for (var i = 0; i < sheet.Length; i++)
        {
            SpriteMetaData meta = new SpriteMetaData();
            meta.name = texs[i].name;
            //这里的rect记录的是单个图片在图集中的uv坐标值
            meta.rect = rs[i];
            meta.rect.Set(
                meta.rect.x * atlasTexture.width,
                meta.rect.y * atlasTexture.height,
                meta.rect.width * atlasTexture.width,
                meta.rect.height * atlasTexture.height
                );


            TextureImporter texImp = imps[i];
            //如果图片有包围盒信息的话
            if (texImp != null)
            {
                meta.border = texImp.spriteBorder;
                meta.pivot  = texImp.spritePivot;
            }

            sheet[i] = meta;

            SpriteData sd = new SpriteData();
            sd.name      = meta.name;
            sd.alignment = meta.alignment;
            sd.border    = new Vector4(meta.border.x, meta.border.y, meta.border.z, meta.border.w);
            sd.pivot     = new Vector2(meta.pivot.x, meta.pivot.y);
            sd.rect      = new Rect(meta.rect);

            if (!atlasInfo.HasSpriteData(meta.name))
            {
                atlasInfo._uvs.Add(sd);
            }
            else
            {
                Debug.LogErrorFormat("{0}图集中存在相同名称的图片:{1}", atlasInfo.name, meta.name);
            }

            //打印日志用
            names.Append(meta.name);
            if (i < sheet.Length - 1)
            {
                names.Append(",");
            }
        }

        //设置图集纹理信息
        TextureImporter imp = TextureImporter.GetAtPath(relativeTexPath) as TextureImporter;

        imp.textureType         = TextureImporterType.Sprite;//图集的类型
        imp.textureCompression  = TextureImporterCompression.Uncompressed;
        imp.alphaIsTransparency = true;
        //imp.SetPlatformTextureSettings("iPhone", 2048, TextureImporterFormat.AutomaticTruecolor);
        imp.SetPlatformTextureSettings("Android", 2048, TextureImporterFormat.ETC2_RGBA8);
        imp.ClearPlatformTextureSettings("iPhone");
        imp.textureFormat    = TextureImporterFormat.AutomaticTruecolor; //TextureImporterFormat.AutomaticCompressed;//图集的格式
        imp.spriteImportMode = SpriteImportMode.Multiple;                //Multiple表示我们这个大图片(图集)中包含很多小图片
        imp.mipmapEnabled    = false;                                    //是否开启mipmap
        imp.isReadable       = false;
        imp.spritesheet      = sheet;                                    //设置图集中小图片的信息(每个图片所在的区域rect等)
        // 保存并刷新
        imp.SaveAndReimport();

        //重新加载导出的纹理
        Texture2D texture2d = AssetDatabase.LoadAssetAtPath <Texture2D>(relativeTexPath);

        //填充图集信息
        atlasInfo._tex2d = texture2d;
        AssetDatabase.CreateAsset(atlasInfo, relativePath);
        GameObject.DestroyImmediate(atlasInfo);

        //输出日志
        Debug.Log("Atlas create ok. " + names.ToString());
    }