Example #1
0
        protected virtual void OnFirstImport(SpriteAtlas spriteAtlas)
        {
            spriteAtlas.SetIncludeInBuild(false);

            //------- PackingSettings -------

            var packingSettings = spriteAtlas.GetPackingSettings();

            SetPackingSettings(ref packingSettings);

            spriteAtlas.SetPackingSettings(packingSettings);

            //------- TextureSettings -------

            var textureSettings = spriteAtlas.GetTextureSettings();

            SetTextureSettings(ref textureSettings);

            spriteAtlas.SetTextureSettings(textureSettings);

            //------ PlatformSettings ------

            foreach (var platformName in DefaultTargetPlatforms)
            {
                var platformSetting = spriteAtlas.GetPlatformSettings(platformName.ToString());

                SetTexturePlatformSettings(ref platformSetting);

                spriteAtlas.SetPlatformSettings(platformSetting);
            }
        }
        public Dictionary <string, string> GetDefaultFields()
        {
            var defaultSetting  = spriteAtlas.GetTextureSettings();
            var packingSetting  = spriteAtlas.GetPackingSettings();
            var platformSetting = spriteAtlas.GetPlatformSettings("Default");

            var fieldMap = new Dictionary <string, string>
            {
                // Basic
                ["Path/Name"]  = Importer.assetPath,
                ["Atlas Type"] = spriteAtlas.isVariant ? "Variant" : "Master",

                // Packing
                ["Allow Rotation"] = packingSetting.enableRotation.ToString(),
                ["Tight Packing"]  = packingSetting.enableTightPacking.ToString(),
                ["Padding"]        = packingSetting.padding.ToString(),

                // Texture
                ["Read/Write Enabled"]   = defaultSetting.readable.ToString(),
                ["Generate Mip Maps"]    = defaultSetting.generateMipMaps.ToString(),
                ["sRGB (Color Texture)"] = defaultSetting.sRGB.ToString(),
                ["Max size"]             = platformSetting.maxTextureSize.ToString(),
            };

            if (Editor.EnableLog)
            {
                UnityEngine.Debug.Log(fieldMap.ToJson());
            }

            return(fieldMap);
        }
        public static void SetPackingSettings(SpriteAtlas spriteAtlas)
        {
            var packSettings = spriteAtlas.GetPackingSettings();

            packSettings.enableRotation     = false;
            packSettings.enableTightPacking = false;
            packSettings.padding            = 2;
            spriteAtlas.SetPackingSettings(packSettings);
        }
    public static void CopySpriteAtlasSetting(SpriteAtlas target, SpriteAtlas template)
    {
        // 注意:默认设置为False
        target.SetIncludeInBuild(false);

        // 通用属性
        target.SetPackingSettings(template.GetPackingSettings());
        target.SetTextureSettings(template.GetTextureSettings());

        // 平台设置
        target.SetPlatformSettings(template.GetPlatformSettings("Standalone"));
        target.SetPlatformSettings(template.GetPlatformSettings("iPhone"));
        target.SetPlatformSettings(template.GetPlatformSettings("Android"));
    }
Example #5
0
        /// <summary>
        /// 重新设置对应目录下的SpriteAtlas
        /// </summary>
        /// <param name="assetPath"></param>
        static private void CheckSpriteAtlas(string assetPath)
        {
            if (!assetPath.StartsWith(PathTool.ImagesDir) || assetPath.Contains(AssetBundleDefined.NO_PACK_PATH_NAME))
            {
                return;
            }
            DirectoryInfo dirInfo       = new DirectoryInfo(assetPath); // 资源路径
            DirectoryInfo parentDirInfo = dirInfo.Parent;               // 资源父路径

            if (!parentDirInfo.Exists)
            {
                return;
            }
            // 检查当前目录图集是否已存在
            foreach (var item in parentDirInfo.GetFileSystemInfos())
            {
                if (item.Name == "SpriteAtlas.spriteatlas")
                {
                    //unity下的操作的路径
                    string unityPath = item.FullName.Replace('\\', '/');          //反斜杠替换成斜杠
                    AssetDatabase.DeleteAsset(unityPath.Substring(unityPath.IndexOf("Assets")));
                }
            }

            SpriteAtlas atlas = new SpriteAtlas();

            // 图集设置
            SpriteAtlasPackingSettings packingSettings = atlas.GetPackingSettings();

            packingSettings.padding            = 4; //图片之间的间距
            packingSettings.enableTightPacking = false;
            atlas.SetPackingSettings(packingSettings);

            FileInfo fileInfo = new FileInfo(assetPath);

            // 这里我使用的是png图片,已经生成Sprite精灵了
            foreach (var item in parentDirInfo.GetFileSystemInfos())
            {
                FileInfo info = item as FileInfo;
                if (info is FileInfo && info.Extension == ".png")
                {
                    //unity下的操作的路径
                    string unityPath = item.FullName.Replace('\\', '/');          //反斜杠替换成斜杠
                    Object obj       = AssetDatabase.LoadAssetAtPath <Sprite>(unityPath.Substring(unityPath.IndexOf("Assets")));
                    atlas.Add(new[] { obj });
                }
            }
            AssetDatabase.CreateAsset(atlas, assetPath.Substring(0, assetPath.Length - fileInfo.Name.Length) + AssetBundleDefined.SPRITE_ATLAS_NAME);
        }
Example #6
0
    /// <summary>
    /// 复制图集设置
    /// </summary>
    public static void CopySpriteAtlasSetting(SpriteAtlas target, SpriteAtlas template)
    {
#if UNITY_2018_4_OR_NEWER
        // 注意:默认设置为False
        target.SetIncludeInBuild(false);

        // 通用属性
        target.SetPackingSettings(template.GetPackingSettings());
        target.SetTextureSettings(template.GetTextureSettings());

        // 平台设置
        target.SetPlatformSettings(template.GetPlatformSettings("Standalone"));
        target.SetPlatformSettings(template.GetPlatformSettings("iPhone"));
        target.SetPlatformSettings(template.GetPlatformSettings("Android"));
#else
        Debug.LogWarning($"{Application.unityVersion} is not support copy sprite atlas setting. Please upgrade to unity2018.4 or newer.");
#endif
    }
    /// <summary>
    /// 生成一个SpriteAtlas
    /// </summary>
    /// <param name="dictory"></param>
    private static void BuildSpriteAtlas(DirectoryInfo dictory)
    {
        var         spriteAtlasName = SpriteAtlasPath + "/" + dictory.Name + "_Atlas.spriteAtlas";
        SpriteAtlas spriteAtlas     = AssetDatabase.LoadAssetAtPath <SpriteAtlas>(spriteAtlasName);

        if (null == spriteAtlas)
        {
            spriteAtlas = new SpriteAtlas();
            spriteAtlas.SetIncludeInBuild(false);
            var packingSettings = spriteAtlas.GetPackingSettings();
            packingSettings.enableRotation     = false;
            packingSettings.enableTightPacking = false;
            spriteAtlas.SetPackingSettings(packingSettings);
            var path      = DataPathToAssetPath(dictory.FullName);
            var folderObj = AssetDatabase.LoadMainAssetAtPath(path);
            var objectArr = new Object[] { folderObj };
            spriteAtlas.Add(objectArr);
            AssetDatabase.CreateAsset(spriteAtlas, spriteAtlasName);
        }
    }
Example #8
0
        public static bool CheckSettings(this SpriteAtlas atlas, SpriteAtlasSettings settings)
        {
            var atlasTextureSettings = atlas.GetTextureSettings();
            var packingSettings      = atlas.GetPackingSettings();
            var isVariant            = atlas.isVariant;

            var textureSettings = settings.ImportSettings.GetTextureImporterSettings();
            var sameSettings    =
                atlasTextureSettings.generateMipMaps == textureSettings.mipmapEnabled &&
                atlasTextureSettings.filterMode == textureSettings.filterMode &&
                atlasTextureSettings.readable == textureSettings.readable &&
                atlasTextureSettings.sRGB == textureSettings.sRGBTexture &&
                packingSettings.padding == settings.Padding &&
                packingSettings.enableRotation == settings.AllowRotation &&
                packingSettings.enableTightPacking == settings.TightPacking &&
                isVariant == (settings.Type == SpriteAtlasType.Variant);

            return(sameSettings && CheckPlatformSettings(atlas, settings, BuildTargetGroup.Unknown) &&
                   CheckPlatformSettings(atlas, settings, BuildTargetGroup.Standalone) &&
                   CheckPlatformSettings(atlas, settings, BuildTargetGroup.Android));
        }
        protected virtual void OnFirstImport(SpriteAtlas spriteAtlas)
        {
            spriteAtlas.SetIncludeInBuild(false);

            //------- PackingSettings -------

            var packingSettings = spriteAtlas.GetPackingSettings();

            packingSettings.padding = 2;

            spriteAtlas.SetPackingSettings(packingSettings);

            //------- TextureSettings -------

            var textureSettings = spriteAtlas.GetTextureSettings();

            textureSettings.readable        = false;
            textureSettings.generateMipMaps = false;

            spriteAtlas.SetTextureSettings(textureSettings);
        }
        protected virtual void OnFirstImport(SpriteAtlas spriteAtlas)
        {
            spriteAtlas.SetIncludeInBuild(false);

            //------- PackingSettings -------

            var packingSettings = spriteAtlas.GetPackingSettings();

            packingSettings.padding            = 2;
            packingSettings.enableTightPacking = false;
            packingSettings.enableRotation     = false;

            spriteAtlas.SetPackingSettings(packingSettings);

            //------- TextureSettings -------

            var textureSettings = spriteAtlas.GetTextureSettings();

            textureSettings.readable        = false;
            textureSettings.generateMipMaps = false;

            spriteAtlas.SetTextureSettings(textureSettings);

            //------ PlatformSettings ------

            foreach (var platformName in DefaultTargetPlatforms)
            {
                var platformSetting = spriteAtlas.GetPlatformSettings(platformName.ToString());

                platformSetting.overridden     = true;
                platformSetting.format         = TextureImporterFormat.ASTC_RGBA_4x4;
                platformSetting.maxTextureSize = 1024;

                spriteAtlas.SetPlatformSettings(platformSetting);
            }
        }
Example #11
0
    static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
    {
        for (int i = 0; i < importedAssets.Length; i++)
        {
            string assetPath = importedAssets[i].Replace("\\", "/");
            if (!assetPath.StartsWith("Assets/Localization/") && !assetPath.StartsWith("Assets/Res/"))
            {
                continue;
            }
            if (!AssetDatabase.IsValidFolder(assetPath))
            {
                continue;                                          //非文件夹文件
            }
            DirectoryInfo dirAssetInfo = new DirectoryInfo(assetPath);
            for (int j = 0; j < _atlasRuleDir.Length; j++)
            {
                AtlasInfo atlasInfo_Rule = _atlasRuleDir[j];
                if (!assetPath.Equals(atlasInfo_Rule.dir) && !assetPath.StartsWith(atlasInfo_Rule.dir + "/"))
                {
                    continue;
                }
                //命中目标文件夹
                if (dirAssetInfo.Parent.FullName.Equals(atlasInfo_Rule.fullName))
                {
                    FileInfo[] atlasFiles  = dirAssetInfo.GetFiles("*.spriteatlas", SearchOption.AllDirectories);
                    FileInfo   targetAtlas = null; //文件夹下直属Atlas
                    for (int k = 0; k < atlasFiles.Length; k++)
                    {
                        FileInfo file = atlasFiles[k];
                        if (targetAtlas == null)
                        {
                            if (file.Directory.FullName.Equals(dirAssetInfo.FullName))
                            {
                                targetAtlas = file;
                            }
                        }
                        else
                        {
                            file.Delete(); //删除其他的Atlas
                        }
                    }

                    string targetName = atlasInfo_Rule.prefix_name + "_" + dirAssetInfo.Name + "_Atlas.spriteatlas";
                    if (targetAtlas == null) //没有找到直属Atlas
                    {
                        AssetDatabase.CreateAsset(new SpriteAtlas(), assetPath + "/" + targetName);
                        targetAtlas = new FileInfo(dirAssetInfo.FullName + "/" + targetName);
                    }
                    else
                    {
                        AssetDatabase.RenameAsset(assetPath + "/" + targetAtlas.Name, targetName); //改名
                        targetAtlas = new FileInfo(dirAssetInfo.FullName + "/" + targetName);
                    }
                    //对Atlas进行设置
                    SpriteAtlas atlas = AssetDatabase.LoadAssetAtPath <SpriteAtlas>(assetPath + "/" + targetAtlas.Name);
                    SpriteAtlasPackingSettings packingSettings = atlas.GetPackingSettings();
                    packingSettings.enableRotation     = atlasInfo_Rule.isRotation;
                    packingSettings.enableTightPacking = atlasInfo_Rule.isTight;
                    packingSettings.padding            = 2;
                    atlas.SetPackingSettings(packingSettings);

                    TextureImporterPlatformSettings platformSettings = atlas.GetPlatformSettings("iPhone");
                    platformSettings.overridden = true;
                    if (platformSettings.maxTextureSize > 2048)
                    {
                        platformSettings.maxTextureSize = 2048;
                    }
                    platformSettings.format = TextureImporterFormat.ASTC_RGBA_6x6;
                    atlas.SetPlatformSettings(platformSettings);

                    atlas.Remove(atlas.GetPackables());
                    atlas.Add(new [] { AssetDatabase.LoadAssetAtPath <Object>(assetPath) });
                }
                break;
            }
        }
    }
Example #12
0
        //================================================================================
        // 関数
        //================================================================================
        /// <summary>
        /// 指定された TextureImporter に設定を適用します
        /// </summary>
        public void Apply(SpriteAtlas spriteAtlas)
        {
            var packingSettings = spriteAtlas.GetPackingSettings();

            if (m_enableRotation.IsOverride)
            {
                packingSettings.enableRotation = m_enableRotation.Value;
            }

            if (m_enableTightPacking.IsOverride)
            {
                packingSettings.enableTightPacking = m_enableTightPacking.Value;
            }

            if (m_padding.IsOverride)
            {
                packingSettings.padding = m_padding.Value;
            }

            spriteAtlas.SetPackingSettings(packingSettings);

            var textureSettings = spriteAtlas.GetTextureSettings();

            if (m_readable.IsOverride)
            {
                textureSettings.readable = m_readable.Value;
            }

            if (m_generateMipMaps.IsOverride)
            {
                textureSettings.generateMipMaps = m_generateMipMaps.Value;
            }

            if (m_sRGB.IsOverride)
            {
                textureSettings.sRGB = m_sRGB.Value;
            }

            if (m_filterMode.IsOverride)
            {
                textureSettings.filterMode = m_filterMode.Value;
            }

            if (m_anisoLevel.IsOverride)
            {
                textureSettings.anisoLevel = m_anisoLevel.Value;
            }

            spriteAtlas.SetTextureSettings(textureSettings);

            if (m_defaultSettings != null)
            {
                var platformSettings = spriteAtlas.GetPlatformSettings("DefaultTexturePlatform");
                m_defaultSettings.Apply(platformSettings);
                spriteAtlas.SetPlatformSettings(platformSettings);
            }

            if (m_standaloneSettings != null)
            {
                var platformSettings = spriteAtlas.GetPlatformSettings("Standalone");
                m_standaloneSettings.Apply(platformSettings);
                spriteAtlas.SetPlatformSettings(platformSettings);
            }

            if (m_iPhoneSettings != null)
            {
                var platformSettings = spriteAtlas.GetPlatformSettings("iPhone");
                platformSettings.overridden = true;
                m_iPhoneSettings.Apply(platformSettings);
                spriteAtlas.SetPlatformSettings(platformSettings);
            }

            if (m_androidSettings != null)
            {
                var platformSettings = spriteAtlas.GetPlatformSettings("Android");
                m_androidSettings.Apply(platformSettings);
                spriteAtlas.SetPlatformSettings(platformSettings);
            }

            if (m_webGLSettings != null)
            {
                var platformSettings = spriteAtlas.GetPlatformSettings("WebGL");
                m_webGLSettings.Apply(platformSettings);
                spriteAtlas.SetPlatformSettings(platformSettings);
            }
        }