Ejemplo n.º 1
0
        //将一个多图文件夹的所有贴图资源的纹理数据打包填充成一张大贴图
        private static void CreateFolderModeAtlas(string[] paths, string atlasPath)
        {
            Texture2D[] textures     = new Texture2D[paths.Length];                                        //Texture数组,一个多图文件夹中包含的图片个数,每个图片资源对应一个Texture
            string[]    textureNames = new string[textures.Length];                                        //texture的资源名称数组
            for (int i = 0; i < textures.Length; i++)                                                      //遍历
            {
                textures[i]     = AssetDatabase.LoadAssetAtPath(paths[i], typeof(Texture2D)) as Texture2D; //按路径加载贴图资源
                textureNames[i] = textures[i].name;                                                        //命名
                textures[i]     = TextureClamper.Clamp(textures[i]);                                       //填充图集的Texture数据
            }
            Texture2D atlas = new Texture2D(ATLAS_MAX_SIZE, ATLAS_MAX_SIZE);                               //创建图集

            //将多个纹理打包到一个纹理图集中。Rect[] 包含每个输入纹理在图集中的 UV 坐标的矩形数组,如果打包失败,则为 null。
            //rects 为每个texture的矩形框数组
            Rect[] rects = atlas.PackTextures(textures, 0, ATLAS_MAX_SIZE, false); //textures要打包到图集的纹理的数组,0 纹理间的像素填充,ATLAS_MAX_SIZE 纹理最大尺寸,false,不可读?false
            AtlasWriter.Write(atlas, atlasPath);                                   //写入纹理数据
            AssetDatabase.ImportAsset(atlasPath, ImportAssetOptions.ForceUpdate);  //导入atlasPath路径下的资源,由用户发起的资源导入。
            //将每一张sprite导入一个大图集,路径,uv信息,名称,边界vector4精灵的边缘边框大小[以像素为单位]X=左边框/Y=下边框/Z=右边框/W=上边框
            TextureImporterUtil.CreateMultipleSpriteImporter(
                atlasPath,                    //大图的路径
                rects,                        //每张Sprite的矩形UV框
                textureNames,                 //每张Sprite的名称
                new Vector4[textures.Length], //border数组
                atlas.width,                  //图集的宽度
                atlas.height,                 //图集的高度
                ATLAS_MAX_SIZE,               //图集的最大尺寸
                TextureImporterFormat.DXT5    //纹理导入的格式
                );
            //TextureImporterUtil.SetAtlasPackingTag(atlasPath, GetPackingTagFromPath(atlasPath));
            AssetDatabase.ImportAsset(atlasPath, ImportAssetOptions.ForceUpdate);//导入atlasPath路径下的资源,由用户发起的资源导入。
        }
Ejemplo n.º 2
0
        private static Texture2D CreateAtlas(string atlasPath, TextureData[] textureDatas)
        {
            Texture2D atlas = new Texture2D(ATLAS_MAX_SIZE, ATLAS_MAX_SIZE);

            Rect[] rects = atlas.PackTextures(GetPackTextures(textureDatas), 0, ATLAS_MAX_SIZE, false);
            AtlasWriter.Write(atlas, atlasPath);
            int    atlasWidth     = atlas.width;
            int    atlasHeight    = atlas.height;
            string alphaAtlasPath = atlasPath.Replace(".png", "_alpha.png");

            AssetDatabase.ImportAsset(atlasPath, ImportAssetOptions.ForceUpdate);
            //安卓平台图集分离通道
            if (AssetPathHelper.GetBuildTarget() == BuildTarget.Android && !atlasPath.Contains("UI_Base"))
            {
                try
                {
                    ImageChannelSpliterWrapper.Execute(atlasPath);
                    AssetDatabase.ImportAsset(alphaAtlasPath, ImportAssetOptions.ForceUpdate);
                    TextureImporterUtil.CreateAlphaChannelImporter(alphaAtlasPath);
                    AssetDatabase.ImportAsset(alphaAtlasPath, ImportAssetOptions.ForceUpdate);
                }
                catch (Exception e)
                {
                    Debug.LogError("通道分离过程中发生错误: " + e.Message);
                    Debug.LogException(e);
                }
            }
            TextureImporterFormat textureFormat = TextureImporterUtil.GetTextureFormat();

            if (atlasPath.Contains("UI_Base"))
            {
                textureFormat = TextureImporterFormat.ARGB32;
            }
            AssetDatabase.ImportAsset(atlasPath, ImportAssetOptions.ForceUpdate);
            TextureImporterUtil.CreateMultipleSpriteImporter(atlasPath, rects, GetPackTexturesNames(textureDatas), GetSpriteBorders(textureDatas),
                                                             atlasWidth, atlasHeight, textureFormat, ATLAS_MAX_SIZE);
            AssetDatabase.ImportAsset(atlasPath, ImportAssetOptions.ForceUpdate);
            MaterialCreator.Create(atlasPath, alphaAtlasPath);
            return(atlas);
        }
Ejemplo n.º 3
0
 //导入贴图
 private static void ImportReadableTexture(string path)
 {
     TextureImporterUtil.CreateReadableTextureImporter(path);//创建导入器
     AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
 }