Exemple #1
0
        public static void ApplySettings(this SpriteAtlas atlas, SpriteAtlasSettings settings)
        {
            var textureSettings = settings.ImportSettings.GetTextureImporterSettings();

            var atlasTextureSettings = new SpriteAtlasTextureSettings
            {
                generateMipMaps = textureSettings.mipmapEnabled,
                filterMode      = textureSettings.filterMode,
                readable        = textureSettings.readable,
                sRGB            = textureSettings.sRGBTexture
            };

            var atlasPackingSettings = new SpriteAtlasPackingSettings
            {
                padding            = settings.Padding,
                enableRotation     = settings.AllowRotation,
                enableTightPacking = settings.TightPacking
            };

            var platformSettings = settings.ImportSettings.GetTextureImporterPlatformSettings(EditorUserBuildSettings.selectedBuildTargetGroup);

            atlas.SetTextureSettings(atlasTextureSettings);
            atlas.SetPackingSettings(atlasPackingSettings);
            atlas.SetPlatformSettings(platformSettings);

            atlas.SetIncludeInBuild(settings.IncludeInBuild);
            atlas.SetIsVariant(settings.Type == SpriteAtlasType.Variant);
        }
        private void CreateSpriteAtlas(List <Sprite> sprites)
        {
            var path  = directoryName + "/" + fileName + ".spriteatlas";
            var atlas = AssetDatabase.LoadAssetAtPath <SpriteAtlas>(path);

            if (atlas == null)
            {
                atlas = new SpriteAtlas();
                AssetDatabase.CreateAsset(atlas, path);
            }

            var texSetting = new SpriteAtlasTextureSettings();

            texSetting.filterMode      = FilterMode.Point;
            texSetting.generateMipMaps = false;

            var packSetting = new SpriteAtlasPackingSettings();

            packSetting.padding            = 2;
            packSetting.enableRotation     = false;
            packSetting.enableTightPacking = true;

            var platformSetting = new TextureImporterPlatformSettings();

            platformSetting.textureCompression = TextureImporterCompression.Uncompressed;

            atlas.SetTextureSettings(texSetting);
            atlas.SetPackingSettings(packSetting);
            atlas.SetPlatformSettings(platformSetting);
            atlas.Add(sprites.ToArray());

            EditorUtility.SetDirty(atlas);
            AssetDatabase.SaveAssets();
        }
        public static void CreateSpriteAtlas()
        {
            string folder = GetCurrentFolder();
            var    path   = folder + "/" + "SpriteAtlas.spriteAtlas";

            path = AssetDatabase.GenerateUniqueAssetPath(path);

            var atlas = new SpriteAtlas();

            SpriteAtlasPackingSettings packing = new SpriteAtlasPackingSettings();

            packing.enableRotation     = false;
            packing.enableTightPacking = false;
            packing.padding            = 4;
            packing.blockOffset        = 1;
            atlas.SetPackingSettings(packing);

            SpriteAtlasTextureSettings tex = new SpriteAtlasTextureSettings();

            tex.generateMipMaps = false;
            tex.filterMode      = FilterMode.Point;
            tex.sRGB            = true;
            atlas.SetTextureSettings(tex);

            TextureImporterPlatformSettings platform = new TextureImporterPlatformSettings();

            platform.maxTextureSize      = 2048;
            platform.textureCompression  = TextureImporterCompression.Uncompressed;
            platform.crunchedCompression = false;
            atlas.SetPlatformSettings(platform);

            AssetDatabase.CreateAsset(atlas, path);
        }
Exemple #4
0
        //static void OnPostprocessAllAssets(string[] importedAssets, string[] deleteAssets, string[] moveAssets, string[] moveFromAssetPaths)
        //{
        //	if (LsyTextureManager.Instance!=null && LsyTextureImporter.Mode == LsyTextureImporterMode.manual)
        //		return;
        //	for (int i = 0; i < importedAssets.Length; i++)
        //	{
        //		if (importedAssets[i].EndsWith(".spriteatlas")) {
        //			OnPostprocessSpriteAtlas(importedAssets[i]);
        //		}
        //	}
        //}
        static void OnPostprocessSpriteAtlas(string path)
        {
            SpriteAtlas sa = AssetDatabase.LoadAssetAtPath <SpriteAtlas>(path);

            if (sa != null)
            {
                sa.SetIncludeInBuild(false);

                SpriteAtlasPackingSettings saps = new SpriteAtlasPackingSettings();
                saps.enableRotation     = false;
                saps.enableTightPacking = false;
                saps.padding            = 4;
                saps.blockOffset        = 1;
                sa.SetPackingSettings(saps);

                SpriteAtlasTextureSettings sats = new SpriteAtlasTextureSettings();
                sats.readable        = false;
                sats.generateMipMaps = false;
                sats.sRGB            = true;
                sats.filterMode      = FilterMode.Bilinear;
                sa.SetTextureSettings(sats);

                SettingPlat(sa, "iPhone");
                SettingPlat(sa, "Android");
            }
        }
Exemple #5
0
        // 设置参数
        static void AtlasSetting(SpriteAtlas atlas)
        {
            atlas.SetIncludeInBuild(false);
            SpriteAtlasPackingSettings packSetting = new SpriteAtlasPackingSettings()
            {
                blockOffset        = 2,
                enableRotation     = false,
                enableTightPacking = false,
                padding            = 2,
            };

            atlas.SetPackingSettings(packSetting);

            SpriteAtlasTextureSettings textureSetting = new SpriteAtlasTextureSettings()
            {
                readable        = false,
                generateMipMaps = false,
                sRGB            = true,
                filterMode      = FilterMode.Bilinear,
            };

            atlas.SetTextureSettings(textureSetting);

            TextureImporterPlatformSettings platformSetting = new TextureImporterPlatformSettings()
            {
                maxTextureSize      = DefineSpritePacker.kDefaultMaxSprite,
                format              = TextureImporterFormat.Automatic,
                textureCompression  = TextureImporterCompression.Compressed,
                crunchedCompression = true,
                compressionQuality  = DefineSpritePacker.kDefaultQuality,
            };

            atlas.SetPlatformSettings(platformSetting);
        }
Exemple #6
0
 static void SetDefaultTextureSettings(ref SpriteAtlasTextureSettings textureSettings)
 {
     textureSettings.anisoLevel      = 0;
     textureSettings.filterMode      = FilterMode.Bilinear;
     textureSettings.generateMipMaps = false;
     textureSettings.readable        = false;
     textureSettings.sRGB            = true;
 }
    //private static string _atlasPath = "Assets/Atlas/Atlas.spriteatlas";
    //private static string _texturePath = "Assets/Texture";

    //[MenuItem("Tools/AutoSetAtlas")]
    public static void AutoSetAtlasContents(string _atlasPath, string _texturePath)
    {
        Debug.Log("path:" + _atlasPath + "--" + _texturePath);
        SpriteAtlas atlas = new SpriteAtlas();
        // 设置参数 可根据项目具体情况进行设置
        SpriteAtlasPackingSettings packSetting = new SpriteAtlasPackingSettings()
        {
            blockOffset        = 1,
            enableRotation     = false,
            enableTightPacking = false,
            padding            = 2,
        };

        atlas.SetPackingSettings(packSetting);

        SpriteAtlasTextureSettings textureSetting = new SpriteAtlasTextureSettings()
        {
            readable        = false,
            generateMipMaps = false,
            sRGB            = true,
            filterMode      = FilterMode.Bilinear,
        };

        atlas.SetTextureSettings(textureSetting);

        TextureImporterPlatformSettings platformSetting = new TextureImporterPlatformSettings()
        {
            maxTextureSize      = 2048,
            format              = TextureImporterFormat.Automatic,
            crunchedCompression = true,
            textureCompression  = TextureImporterCompression.Compressed,
            compressionQuality  = 50,
        };

        atlas.SetPlatformSettings(platformSetting);

        AssetDatabase.CreateAsset(atlas, _atlasPath + "\\New Sprite Atlas.spriteatlas");

        // 1、添加文件
        DirectoryInfo dir = new DirectoryInfo(_texturePath);

        // 这里我使用的是png图片,已经生成Sprite精灵了
        FileInfo[] files = dir.GetFiles("*.png");
        foreach (FileInfo file in files)
        {
            Debug.Log("path:" + file.FullName);
            atlas.Add(new[] { AssetDatabase.LoadAssetAtPath <Sprite>($"{_texturePath}/{file.Name}") });
        }

        // 2、添加文件夹
        //Object obj = AssetDatabase.LoadAssetAtPath(_texturePath, typeof(Object));
        //atlas.Add(new[] { obj });

        AssetDatabase.SaveAssets();
    }
Exemple #8
0
        void AutoSetAtlasContents(string texturePath)
        {
            // 设置参数 可根据项目具体情况进行设置
            SpriteAtlasPackingSettings packSetting = new SpriteAtlasPackingSettings()
            {
                blockOffset        = 1,
                enableRotation     = false,
                enableTightPacking = false,
                padding            = 2,
            };
            SpriteAtlasTextureSettings textureSetting = new SpriteAtlasTextureSettings()
            {
                readable        = false,
                generateMipMaps = false,
                sRGB            = true,
                filterMode      = FilterMode.Bilinear,
            };
            TextureImporterPlatformSettings platformSetting = new TextureImporterPlatformSettings()
            {
                maxTextureSize      = 2048,
                format              = TextureImporterFormat.Automatic,
                crunchedCompression = true,
                textureCompression  = TextureImporterCompression.Compressed,
                compressionQuality  = 50,
            };
            DirectoryInfo root = new DirectoryInfo(texturePath);

            DirectoryInfo[] dirs = root.GetDirectories();
            foreach (DirectoryInfo dir in dirs)
            {
                SpriteAtlas atlas = new SpriteAtlas();
                atlas.SetPackingSettings(packSetting);
                atlas.SetTextureSettings(textureSetting);
                atlas.SetPlatformSettings(platformSetting);

                // 这里我使用的是png图片,已经生成Sprite精灵了
                FileInfo[] files = dir.GetFiles("*.png");
                foreach (FileInfo file in files)
                {
                    string dirName = Path.GetFileName(file.DirectoryName);
                    string texPath = $"{texturePath}/{dirName}/{file.Name}";
                    atlas.Add(new[] { AssetDatabase.LoadAssetAtPath <Sprite>(texPath) });
                    //Debug.Log($"Create sprite {texPath}");
                }
                string atlasPath = $"{texturePath}/{dir.Name}.spriteatlas";
                //Debug.Log($"Create sprite {atlasPath}");
                AssetDatabase.CreateAsset(atlas, atlasPath);
                AssetDatabase.SaveAssets();
            }
            // 2、添加文件夹
            //Object obj = AssetDatabase.LoadAssetAtPath(texturePath, typeof(Object));
            //atlas.Add(new[] {obj});
        }
Exemple #9
0
        private static SpriteAtlas PackSpritesToAtlas(string packName, Sprite[] sprites)
        {
            string      atlasAssetPath = UIAtlasRootPath + "/" + packName + "_atlas" + ".spriteatlas";
            SpriteAtlas packAtlas      = AssetDatabase.LoadAssetAtPath <SpriteAtlas>(atlasAssetPath);

            if (packAtlas == null)
            {
                packAtlas = new SpriteAtlas();
                AssetDatabase.CreateAsset(packAtlas, atlasAssetPath);
            }
            packAtlas.Remove(packAtlas.GetPackables());
            packAtlas.Add(sprites);

            SpriteAtlasTextureSettings sats = packAtlas.GetTextureSettings();

            sats.readable        = false;
            sats.sRGB            = true;
            sats.generateMipMaps = false;
            sats.filterMode      = FilterMode.Bilinear;
            packAtlas.SetTextureSettings(sats);

            SpriteAtlasPackingSettings saps = packAtlas.GetPackingSettings();

            saps.enableRotation     = false;
            saps.padding            = 2;
            saps.enableTightPacking = false;
            packAtlas.SetPackingSettings(saps);

            TextureImporterPlatformSettings winTips = packAtlas.GetPlatformSettings("Standalone");

            winTips.overridden     = true;
            winTips.maxTextureSize = 2048;
            winTips.format         = TextureImporterFormat.DXT5;
            packAtlas.SetPlatformSettings(winTips);

            TextureImporterPlatformSettings androidTips = packAtlas.GetPlatformSettings("Android");

            androidTips.maxTextureSize = 2048;
            androidTips.overridden     = true;
            androidTips.format         = TextureImporterFormat.ETC2_RGBA8;
            packAtlas.SetPlatformSettings(androidTips);

            TextureImporterPlatformSettings iOSTips = packAtlas.GetPlatformSettings("iPhone");

            iOSTips.maxTextureSize = 2048;
            iOSTips.overridden     = true;
            iOSTips.format         = TextureImporterFormat.ASTC_4x4;
            packAtlas.SetPlatformSettings(iOSTips);

            AssetDatabase.ImportAsset(atlasAssetPath);
            return(packAtlas);
        }
Exemple #10
0
    /// <summary>
    /// 设置SpriteAtlas 的配置参数  Uncompressed 目录的图集不压
    /// </summary>
    /// <param name="atlas"></param>
    /// <param name="_atlasPath"></param>
    private static void SetSpriteAtlas(SpriteAtlas atlas, string _atlasPath)
    {
        TextureImporterFormat _format = TextureImporterFormat.ASTC_6x6;

        if (_atlasPath.Contains("Uncompressed"))
        {
            _format = TextureImporterFormat.RGBA32;
        }
        // 设置参数 可根据项目具体情况进行设置
        SpriteAtlasPackingSettings packSetting = new SpriteAtlasPackingSettings()
        {
            blockOffset        = 1,
            enableRotation     = false,
            enableTightPacking = false,
            padding            = 2,
        };

        atlas.SetPackingSettings(packSetting);

        SpriteAtlasTextureSettings textureSetting = new SpriteAtlasTextureSettings()
        {
            readable        = false,
            generateMipMaps = false,
            sRGB            = true,
            filterMode      = FilterMode.Bilinear,
        };

        atlas.SetTextureSettings(textureSetting);

        TextureImporterPlatformSettings platformSetting = new TextureImporterPlatformSettings()
        {
            name           = "Android",
            maxTextureSize = 2048,
            format         = _format,
            overridden     = true,
        };

        atlas.SetPlatformSettings(platformSetting);

        platformSetting = new TextureImporterPlatformSettings()
        {
            name           = "iPhone",
            maxTextureSize = 2048,
            format         = _format,
            overridden     = true,
        };

        atlas.SetPlatformSettings(platformSetting);
    }
Exemple #11
0
        public SpriteAtlas Create(string fullPath)
        {
            if (string.IsNullOrEmpty(fullPath))
            {
                return(null);
            }

            var atlas = new SpriteAtlas();

            var atlasTextureSettings = new SpriteAtlasTextureSettings();

            atlasTextureSettings.generateMipMaps = _atlasSettings.GenerateMipMaps;
            atlasTextureSettings.filterMode      = _atlasSettings.FilterMode;
            atlasTextureSettings.readable        = _atlasSettings.ReadWriteEnabled;
            atlasTextureSettings.sRGB            = _atlasSettings.SRgb;

            var atlasPackingSettings = new SpriteAtlasPackingSettings();

            atlasPackingSettings.padding            = _atlasSettings.Padding;
            atlasPackingSettings.enableRotation     = _atlasSettings.AllowRotation;
            atlasPackingSettings.enableTightPacking = _atlasSettings.TightPacking;

            var platformSettings = new TextureImporterPlatformSettings();

            platformSettings.textureCompression  = _atlasSettings.Compression;
            platformSettings.maxTextureSize      = _atlasSettings.MaxTextureSize;
            platformSettings.format              = (TextureImporterFormat)_atlasSettings.Format;
            platformSettings.crunchedCompression = _atlasSettings.UseCrunchCompression;
            platformSettings.compressionQuality  = _atlasSettings.CompressionQuality;

            atlas.SetTextureSettings(atlasTextureSettings);
            atlas.SetPackingSettings(atlasPackingSettings);
            atlas.SetPlatformSettings(platformSettings);

            atlas.SetIncludeInBuild(_atlasSettings.IncludeInBuild);
            atlas.SetIsVariant(_atlasSettings.Type == SpriteAtlasType.Variant);

            EditorFileUtils.CreateDirectories(fullPath);
            AssetDatabase.CreateAsset(atlas, fullPath);

            AssetDatabase.SaveAssets();

            AssetDatabase.WriteImportSettingsIfDirty(fullPath);

            return(atlas);
        }
        public override void Execute()
        {
            atlas = null;

            if (assetPaths != null && assetPaths.Length > 0)
            {
                string atlasName = AtlasName;
                if (string.IsNullOrEmpty(atlasName))
                {
                    atlasName = Path.GetFileName(filter.Folder);
                }

                atlasName = atlasName.ToLower();
                string atlasAssetPath = $"{TargetFolder}/{atlasName}_atlas.spriteatlas";

                SpriteAtlas packAtlas = AssetDatabase.LoadAssetAtPath <SpriteAtlas>(atlasAssetPath);
                if (packAtlas == null)
                {
                    packAtlas = new SpriteAtlas();
                    AssetDatabase.CreateAsset(packAtlas, atlasAssetPath);
                }

                Sprite[] sprites = (from assetPath in assetPaths select AssetDatabase.LoadAssetAtPath <Sprite>(assetPath)).ToArray();

                packAtlas.Remove(packAtlas.GetPackables());
                packAtlas.Add(sprites);

                SpriteAtlasTextureSettings sats = packAtlas.GetTextureSettings();
                sats.readable        = false;
                sats.sRGB            = true;
                sats.generateMipMaps = false;
                sats.filterMode      = FilterMode.Bilinear;
                packAtlas.SetTextureSettings(sats);

                SpriteAtlasPackingSettings saps = packAtlas.GetPackingSettings();
                saps.enableRotation     = AllowRotation;
                saps.padding            = Padding;
                saps.enableTightPacking = TightPacking;
                packAtlas.SetPackingSettings(saps);

                atlas = packAtlas;
            }
        }
Exemple #13
0
        private static void CreateAtlas(string name, List <string> importPaths)
        {
            var filename = Path.Combine(EditorUtil.GetBaumAtlasPath(), name + ".spriteatlas");

            var atlas    = new SpriteAtlas();
            var settings = new SpriteAtlasPackingSettings()
            {
                padding            = 8,
                enableTightPacking = false
            };

            atlas.SetPackingSettings(settings);
            var textureSettings = new SpriteAtlasTextureSettings
            {
                filterMode      = FilterMode.Point,
                generateMipMaps = false,
                sRGB            = true
            };

            atlas.SetTextureSettings(textureSettings);

            var textureImporterPlatformSettings = new TextureImporterPlatformSettings {
                maxTextureSize = 8192
            };

            atlas.SetPlatformSettings(textureImporterPlatformSettings);

            // iOS用テクスチャ設定
            // ASTCに固定してしまいっている これらの設定を記述できるようにしたい
            textureImporterPlatformSettings.overridden = true;
            textureImporterPlatformSettings.name       = "iPhone";
            textureImporterPlatformSettings.format     = TextureImporterFormat.ASTC_4x4;
            atlas.SetPlatformSettings(textureImporterPlatformSettings);

            // アセットの生成
            AssetDatabase.CreateAsset(atlas, EditorUtil.ToUnityPath(filename));

            // ディレクトリを登録する場合
            // var iconsDirectory = AssetDatabase.LoadAssetAtPath<Object>("Assets/ExternalAssets/Baum2/CreatedSprites/UIESMessenger");
            // atlas.Add(new Object[]{iconsDirectory});
        }
Exemple #14
0
    /// <summary>
    /// 设置图集
    /// </summary>
    /// <param name="directoryName"></param>
    /// <param name="relativePath"></param>
    private static void SetSpriteAtlas(string relativePath)
    {
        string      spriteAtlasPath = relativePath + ".spriteatlas";
        SpriteAtlas spriteAtlas     = AssetDatabase.LoadAssetAtPath <SpriteAtlas>(spriteAtlasPath);

        if (spriteAtlas == null)
        {
            spriteAtlas = new SpriteAtlas();
            AssetDatabase.CreateAsset(spriteAtlas, spriteAtlasPath);

            SpriteAtlasPackingSettings spriteAtlasPackingSettings = SpriteAtlasExtensions.GetPackingSettings(spriteAtlas);
            spriteAtlasPackingSettings.enableTightPacking = false;
            spriteAtlasPackingSettings.padding            = 2;
            SpriteAtlasTextureSettings spriteAtlasTextureSettings = SpriteAtlasExtensions.GetTextureSettings(spriteAtlas);
            spriteAtlasTextureSettings.sRGB = true;

            var obj = AssetDatabase.LoadMainAssetAtPath(relativePath);
            UnityEngine.Object[] objects = new UnityEngine.Object[] { obj };
            SpriteAtlasExtensions.Add(spriteAtlas, objects);
        }
    }
Exemple #15
0
    private void createSpriteAltas(string altasFolderPath_, string altasName_)
    {
        //New altas file.
        SpriteAtlas _atlas = new SpriteAtlas();
        SpriteAtlasPackingSettings _packSetting = new SpriteAtlasPackingSettings()
        {
            blockOffset        = 1,
            enableRotation     = true,
            enableTightPacking = true,
            padding            = 2,
        };

        _atlas.SetPackingSettings(_packSetting);
        SpriteAtlasTextureSettings _textureSetting = new SpriteAtlasTextureSettings()
        {
            readable        = false,
            generateMipMaps = false,
            sRGB            = true,
            filterMode      = FilterMode.Bilinear,
        };

        _atlas.SetTextureSettings(_textureSetting);
        TextureImporterPlatformSettings _platformSetting = new TextureImporterPlatformSettings()
        {
            maxTextureSize      = _maxTextureSize,
            format              = TextureImporterFormat.Automatic,
            crunchedCompression = _crunchedCompression,
            textureCompression  = TextureImporterCompression.Compressed,
            compressionQuality  = _compressionQuality,
        };

        _atlas.SetPlatformSettings(_platformSetting);
        //Create altas file.
        AssetDatabase.CreateAsset(_atlas, altasFolderPath_ + "/" + altasName_);
        //Get folder pack with it.
        Object _folderObj = AssetDatabase.LoadAssetAtPath(altasFolderPath_, typeof(Object));

        _atlas.Add(new [] { _folderObj });
        AssetDatabase.SaveAssets();
    }
Exemple #16
0
    public void CreateDirAtlas(string packDir)
    {
        var         atlasDir  = Path.GetDirectoryName(packDir);
        var         atlasName = "atlas_" + Path.GetFileName(packDir).ToLower();
        var         fullPath  = atlasDir + '/' + atlasName + ".spriteatlas";
        SpriteAtlas atlas;

        if (!File.Exists(fullPath))
        {
            atlas = new SpriteAtlas();
            SpriteAtlasPackingSettings packSetting = new SpriteAtlasPackingSettings()
            {
                blockOffset        = 1,
                enableRotation     = false,
                enableTightPacking = false,
                padding            = 2,
            };
            atlas.SetPackingSettings(packSetting);

            var textureSetting = new SpriteAtlasTextureSettings()
            {
                readable        = false,
                generateMipMaps = false,
                sRGB            = true,
                filterMode      = FilterMode.Bilinear,
            };
            atlas.SetTextureSettings(textureSetting);

            var platformSetting = new TextureImporterPlatformSettings()
            {
                maxTextureSize      = 2048,
                format              = TextureImporterFormat.Automatic,
                crunchedCompression = true,
                textureCompression  = TextureImporterCompression.Compressed,
                compressionQuality  = 50,
            };
            atlas.SetPlatformSettings(platformSetting);
            AssetDatabase.CreateAsset(atlas, fullPath);
        }
        else
        {
            atlas = AssetDatabase.LoadAssetAtPath <SpriteAtlas>(fullPath);
        }

        if (atlas == null)
        {
            Debug.LogError($"{fullPath} is not exist");
            return;
        }

        var packes = atlas.GetPackables();

        atlas.Remove(packes);
        atlas.Add(new[] { AssetDatabase.LoadAssetAtPath <UnityEngine.Object>(packDir) });
        ImportTool.SetAssetAddress(fullPath, atlasName);
        AssetDatabase.SaveAssets();

        // 取消图集图片的Addressable
        var assetes = FileUtil.GetDirAllAssetFullPath(packDir);

        foreach (var asset in assetes)
        {
            ImportTool.RemoveAssetAddress(asset);
        }
    }
Exemple #17
0
        /// <summary>
        /// 刷新所有 同层 的资源 bundle name
        /// </summary>

        /*
         * public void RefreshSetting()
         * {
         *  //Debug.LogError("---------------------------------------------------");
         *  //D:/HFFramework/HFFramework/Assets
         *  //Assets/GameResources/Game/Prefab[A]/AssetConfig.asset
         *  foreach (var assetPath in GetAllAssetInFolder())
         *  {
         *      //Debug.LogError("assetPath " + assetPath);
         *      AssetImporter assetImporter = AssetImporter.GetAtPath(assetPath);
         *      string bundleName = string.Empty;
         *      if (assetPath.Contains("AssetConfig.asset"))
         *      {
         *          bundleName = string.Empty;
         *      }
         *      else
         *      {
         *          bundleName = assetbundleName;
         *      }
         *
         *
         *      //Debug.LogError(assetImporter.assetBundleName);
         *
         *      //if (assetImporter.assetBundleName!= bundleName)
         *      //{
         *      //    assetImporter.assetBundleName = bundleName;
         *      //    assetImporter.SaveAndReimport();
         *      //}
         *
         *
         *      assetImporter.assetBundleName = bundleName;
         *      assetImporter.SaveAndReimport();
         *  }
         *
         *
         *  AssetDatabase.SaveAssets();
         *      AssetDatabase.Refresh();
         * }
         */



        public void RefreshAtlas()
        {
            if (string.IsNullOrEmpty(atlasName))
            {
                return;
            }

            SpriteAtlas atlas = null;

            foreach (var assetPath in GetAllAssetInFolder())
            {
                atlas = AssetDatabase.LoadAssetAtPath <SpriteAtlas>(assetPath);
                if (atlas != null)
                {
                    break;
                }
            }

            if (atlas == null)
            {
                atlas = new SpriteAtlas();
                // 设置参数 可根据项目具体情况进行设置
                SpriteAtlasPackingSettings packSetting = new SpriteAtlasPackingSettings()
                {
                    blockOffset        = 1,
                    enableRotation     = false,
                    enableTightPacking = false,
                    padding            = 2,
                };
                atlas.SetPackingSettings(packSetting);

                SpriteAtlasTextureSettings textureSetting = new SpriteAtlasTextureSettings()
                {
                    readable        = false,
                    generateMipMaps = false,
                    sRGB            = true,
                    filterMode      = FilterMode.Bilinear,
                };
                atlas.SetTextureSettings(textureSetting);

                TextureImporterPlatformSettings platformSetting = new TextureImporterPlatformSettings()
                {
                    maxTextureSize      = 1024,
                    format              = TextureImporterFormat.Automatic,
                    crunchedCompression = true,
                    textureCompression  = TextureImporterCompression.Compressed,
                    compressionQuality  = 50,
                };
                atlas.SetPlatformSettings(platformSetting);
                AssetDatabase.CreateAsset(atlas, FolderPath + "/" + atlasName + ".spriteatlas");
            }

            if (atlas != null)
            {
                UnityEngine.Object obj = AssetDatabase.LoadAssetAtPath <UnityEngine.Object>(FolderPath);
                atlas.Remove(atlas.GetPackables());
                atlas.Add(new[] { obj });
                EditorUtility.SetDirty(atlas);
                AssetDatabase.SaveAssets();
            }

            AssetDatabase.Refresh();
        }
Exemple #18
0
    static SpriteAtlas mGenAtlas()
    {
        SpriteAtlasPackingSettings atlasSetting = new SpriteAtlasPackingSettings()
        {
            blockOffset        = 1,
            enableRotation     = false,
            enableTightPacking = false,
            padding            = 2,
        };
        SpriteAtlasTextureSettings textureSetting = new SpriteAtlasTextureSettings()
        {
            readable        = false,
            generateMipMaps = false,
            sRGB            = true,
            filterMode      = FilterMode.Bilinear,
        };

        //Default
        TextureImporterPlatformSettings compressDefault = new TextureImporterPlatformSettings()
        {
            name                = "DefaultTexturePlatform",
            overridden          = false,
            maxTextureSize      = 2048,
            compressionQuality  = 50,
            crunchedCompression = true,
            format              = TextureImporterFormat.Automatic,
            textureCompression  = TextureImporterCompression.Compressed,
        };
        //IOS
        TextureImporterPlatformSettings compressIOS = new TextureImporterPlatformSettings()
        {
            name                = "iPhone",
            overridden          = true,
            maxTextureSize      = 2048,
            compressionQuality  = 50,
            crunchedCompression = true,
            format              = TextureImporterFormat.RGBA32,
            textureCompression  = TextureImporterCompression.Compressed,
        };
        //Android
        TextureImporterPlatformSettings compressAndroid = new TextureImporterPlatformSettings()
        {
            name                = "Android",
            overridden          = true,
            maxTextureSize      = 2048,
            compressionQuality  = 50,
            crunchedCompression = true,
            format              = TextureImporterFormat.Automatic,
            textureCompression  = TextureImporterCompression.Compressed,
        };

        SpriteAtlas atlas = new SpriteAtlas();

        atlas.SetPackingSettings(atlasSetting);
        atlas.SetTextureSettings(textureSetting);
        atlas.SetPlatformSettings(compressDefault);
        atlas.SetPlatformSettings(compressIOS);
        atlas.SetPlatformSettings(compressAndroid);
        atlas.SetIncludeInBuild(true);
        atlas.SetIsVariant(false);

        return(atlas);
    }
Exemple #19
0
 protected virtual void SetTextureSettings(ref SpriteAtlasTextureSettings textureSettings)
 {
     textureSettings.readable        = false;
     textureSettings.generateMipMaps = false;
     textureSettings.filterMode      = FilterMode.Bilinear;
 }
Exemple #20
0
    static void AutoSetAtlasContents(string dir)
    {
        SpriteAtlas atlas = new SpriteAtlas();

        // 设置参数 可根据项目具体情况进行设置
        SpriteAtlasPackingSettings packSetting = new SpriteAtlasPackingSettings()
        {
            blockOffset        = 1,
            enableRotation     = false,
            enableTightPacking = false,
            padding            = 2,
        };

        atlas.SetPackingSettings(packSetting);

        SpriteAtlasTextureSettings textureSetting = new SpriteAtlasTextureSettings()
        {
            readable        = false,
            generateMipMaps = false,
            sRGB            = true,
            filterMode      = FilterMode.Bilinear,
        };

        atlas.SetTextureSettings(textureSetting);

        TextureImporterPlatformSettings platformSetting = new TextureImporterPlatformSettings()
        {
            maxTextureSize      = 1024,
            format              = TextureImporterFormat.Automatic,
            crunchedCompression = true,
            textureCompression  = TextureImporterCompression.Compressed,
            compressionQuality  = 50,
        };

        atlas.SetPlatformSettings(platformSetting);

        DirectoryInfo dirInfo = new DirectoryInfo(dir);
        string        path    = $"{dir}/{dirInfo.Name}.spriteatlas";

        if (File.Exists(path))
        {
            File.Delete(path);
        }

        AssetDatabase.CreateAsset(atlas, path);

        //1.添加文件
        FileInfo[] files = dirInfo.GetFiles("*.png");
        foreach (FileInfo file in files)
        {
            atlas.Add(new[] { AssetDatabase.LoadAssetAtPath <Sprite>($"{dir}/{file.Name}") });
        }

        //2.添加子文件夹
        DirectoryInfo[] subdirs = dirInfo.GetDirectories();
        foreach (DirectoryInfo subdir in subdirs)
        {
            Object obj = AssetDatabase.LoadAssetAtPath(subdir.FullName, typeof(Object));
            atlas.Add(new[] { obj });
        }

        AssetDatabase.SaveAssets();
        Debug.LogFormat("Generate a <b>{0}.spriteatlas</b> success! path=<b>{1}</b>",
                        dirInfo.Name, path);
    }
Exemple #21
0
        static void HandleSpriteAtlasFormat(HashSet <string> atlasFiles)
        {
            string[]      atlasFileArray   = new string[ATLAS_TYPE_NUMBER];
            SpriteAtlas[] spriteAtlasArray = new SpriteAtlas[ATLAS_TYPE_NUMBER];
            SpriteAtlasPackingSettings[] packingSettingsArray = new SpriteAtlasPackingSettings[ATLAS_TYPE_NUMBER];
            SpriteAtlasTextureSettings[] textureSettingsArray = new SpriteAtlasTextureSettings[ATLAS_TYPE_NUMBER];
            Dictionary <string, TextureImporterPlatformSettings>[] platformSettingsDictArray = new Dictionary <string, TextureImporterPlatformSettings> [ATLAS_TYPE_NUMBER];
            platformSettingsDictArray[0] = new Dictionary <string, TextureImporterPlatformSettings>();
            platformSettingsDictArray[1] = new Dictionary <string, TextureImporterPlatformSettings>();

            foreach (string file in atlasFiles)
            {
                if (file.EndsWith(LOW_ATLAS_SUFFIX))
                {
                    continue;
                }

                atlasFileArray[0] = file;
                atlasFileArray[1] = file.Replace(EditorResourcePaths.ATLAS_SUFFIX, LOW_ATLAS_SUFFIX);

                for (int i = 0; i < ATLAS_TYPE_NUMBER; i++)
                {
                    spriteAtlasArray[i]     = AssetDatabase.LoadAssetAtPath <SpriteAtlas>(atlasFileArray[i]);
                    packingSettingsArray[i] = SpriteAtlasExtensions.GetPackingSettings(spriteAtlasArray[i]);
                    textureSettingsArray[i] = SpriteAtlasExtensions.GetTextureSettings(spriteAtlasArray[i]);
                    foreach (string platform in PLATFORMS)
                    {
                        platformSettingsDictArray[i][platform] = SpriteAtlasExtensions.GetPlatformSettings(spriteAtlasArray[i], platform);
                    }
                }

                HashSet <string> spriteSet    = EditorResourcePaths.GetDependencies(atlasFileArray, EditorResourcePaths.PNG_REGEX);
                bool             containAlpha = false;
                foreach (string spriteFile in spriteSet)
                {
                    Texture2D texture = EditorCommonUtils.ReadTexture(spriteFile);
                    if (ContainAlpha(texture))
                    {
                        containAlpha = true;
                        break;
                    }
                }

                for (int i = 0; i < ATLAS_TYPE_NUMBER; i++)
                {
                    packingSettingsArray[i].enableRotation     = false;
                    packingSettingsArray[i].enableTightPacking = false;
                    SetDefaultTextureSettings(ref textureSettingsArray[i]);
                }

                if (containAlpha)
                {
                    SetPlatformTextureSettings(platformSettingsDictArray[0]["Default"], "Default", TextureImporterFormat.RGBA32);
                    SetPlatformTextureSettings(platformSettingsDictArray[0]["Standalone"], "Standalone", TextureImporterFormat.DXT5);
                    SetPlatformTextureSettings(platformSettingsDictArray[0]["Android"], "Android", TextureImporterFormat.ETC2_RGBA8);
                    SetPlatformTextureSettings(platformSettingsDictArray[0]["iPhone"], "iPhone", IOS_NEW_RGBA);

                    SetPlatformTextureSettings(platformSettingsDictArray[1]["Default"], "Default", TextureImporterFormat.RGBA32);
                    SetPlatformTextureSettings(platformSettingsDictArray[1]["Standalone"], "Standalone", TextureImporterFormat.DXT5Crunched);
                    SetPlatformTextureSettings(platformSettingsDictArray[1]["Android"], "Android", TextureImporterFormat.ETC2_RGBA8Crunched);
                    SetPlatformTextureSettings(platformSettingsDictArray[1]["iPhone"], "iPhone", TextureImporterFormat.PVRTC_RGBA4);
                }
                else
                {
                    SetPlatformTextureSettings(platformSettingsDictArray[0]["Default"], "Default", TextureImporterFormat.RGB24);
                    SetPlatformTextureSettings(platformSettingsDictArray[0]["Standalone"], "Standalone", TextureImporterFormat.DXT1);
                    SetPlatformTextureSettings(platformSettingsDictArray[0]["Android"], "Android", TextureImporterFormat.ETC2_RGB4);
                    SetPlatformTextureSettings(platformSettingsDictArray[0]["iPhone"], "iPhone", TextureImporterFormat.PVRTC_RGB4);

                    SetPlatformTextureSettings(platformSettingsDictArray[1]["Default"], "Default", TextureImporterFormat.RGB24);
                    SetPlatformTextureSettings(platformSettingsDictArray[1]["Standalone"], "Standalone", TextureImporterFormat.DXT1Crunched);
                    SetPlatformTextureSettings(platformSettingsDictArray[1]["Android"], "Android", TextureImporterFormat.ETC_RGB4Crunched);
                    SetPlatformTextureSettings(platformSettingsDictArray[1]["iPhone"], "iPhone", TextureImporterFormat.PVRTC_RGB4);
                }

                for (int i = 0; i < ATLAS_TYPE_NUMBER; i++)
                {
                    SpriteAtlasExtensions.SetIncludeInBuild(spriteAtlasArray[i], false);
                    SpriteAtlasExtensions.SetPackingSettings(spriteAtlasArray[i], packingSettingsArray[i]);
                    SpriteAtlasExtensions.SetTextureSettings(spriteAtlasArray[i], textureSettingsArray[i]);
                    foreach (string platform in PLATFORMS)
                    {
                        SpriteAtlasExtensions.SetPlatformSettings(spriteAtlasArray[i], platformSettingsDictArray[i][platform]);
                    }
                }
                SpriteAtlasExtensions.SetIsVariant(spriteAtlasArray[1], false);
                SpriteAtlasExtensions.Remove(spriteAtlasArray[1], SpriteAtlasExtensions.GetPackables(spriteAtlasArray[1]));
                SpriteAtlasExtensions.Add(spriteAtlasArray[1], SpriteAtlasExtensions.GetPackables(spriteAtlasArray[0]));
            }
            AssetDatabase.Refresh();
        }
Exemple #22
0
        public static void CreateAtlasAsset()
        {
            var    selection = Selection.objects;
            string path      = string.Empty;

            StringBuilder sb      = new StringBuilder();
            string        tagName = string.Empty;

            foreach (Object s in selection)
            {
                if (s is DefaultAsset && (path = AssetDatabase.GetAssetPath(s)) != null && Directory.Exists(path))
                {
                    var    ragName    = s.name.ToLower() + "_atlas.spriteatlas";
                    string atlas_path = Path.Combine(path, ragName);
                    tagName = s.name.ToLower() + "_atlas";

                    sb.Append("Crate atlas Asset :");
                    sb.Append(ragName);
                    sb.Append("\r\n");

                    var           allchildren = EditorUtils.getAllChildFiles(path, @"\.meta$|\.manifest$|\.DS_Store$|\.u$", null, false);
                    int           count       = 0;
                    List <int>    names       = new List <int>();
                    List <Sprite> allSprites  = new List <Sprite>();
                    foreach (var f in allchildren)
                    {
                        count++;
                        TextureImporter ti = AssetImporter.GetAtPath(f) as TextureImporter;
                        if (ti != null)
                        {
                            if (ti.textureType != TextureImporterType.Sprite)
                            {
                                ti.textureType = TextureImporterType.Sprite;
                            }
                            Object[] objs = AssetDatabase.LoadAllAssetRepresentationsAtPath(f);
                            foreach (var item in objs)
                            {
                                if (item is Sprite)
                                {
                                    sb.AppendLine(item.name);
                                    names.Add(UnityEngine.Animator.StringToHash(item.name));
                                    allSprites.Add((Sprite)item);
                                }
                            }
                            // ti.spritePackingTag = tagName;
                            // ti.assetBundleName = tagName + Common.CHECK_ASSETBUNDLE_SUFFIX;
                            EditorUtility.DisplayProgressBar("Processing...", "生成中... (" + count + " / " + allchildren.Count + ")", count / allchildren.Count);
                        }
                        else
                        {
                            Debug.LogWarningFormat("{0} is not Texture ", f);
                        }
                    }
                    EditorUtility.ClearProgressBar();
                    //生成或者替换资源
                    var atlas = AssetDatabase.LoadAssetAtPath <SpriteAtlas>(atlas_path);
                    if (atlas == null)
                    {
                        atlas = new SpriteAtlas();
                        AssetDatabase.CreateAsset(atlas, atlas_path);
                        SpriteAtlasPackingSettings packSet = new SpriteAtlasPackingSettings()
                        {
                            blockOffset        = 1,
                            enableRotation     = false,
                            enableTightPacking = false,
                            padding            = 2,
                        };
                        atlas.SetPackingSettings(packSet);


                        SpriteAtlasTextureSettings textureSet = new SpriteAtlasTextureSettings()
                        {
                            readable        = false,
                            generateMipMaps = false,
                            sRGB            = false,
                            filterMode      = FilterMode.Bilinear,
                        };
                        atlas.SetTextureSettings(textureSet);
                    }

                    SpriteAtlasExtensions.Add(atlas, allSprites.ToArray());
                    EditorUtility.SetDirty(atlas);

                    var groupAssets = new List <string>();
                    groupAssets.Add(atlas_path);
                    var atlasGroup = AASEditorUtility.FindGroup(tagName, AASEditorUtility.DefaltGroupSchema[0]);
                    AASEditorUtility.SetGroupAddress(AASEditorUtility.LoadAASSetting(), atlasGroup, groupAssets);
                    sb.AppendFormat("build {0} success  count = {1} ", ragName, names.Count);
                    AssetDatabase.SaveAssets();
                }
            }

            sb.AppendLine("\r\nall completed");
            Debug.Log(sb.ToString());
            EditorUtils.WriteToTmpFile(tagName + ".txt", sb.ToString());
        }