void SetPlatformSettingForIndex(TextureImporter importer, string name, TextureImporterPlatformSettings original)
    {
        var settings = new TextureImporterPlatformSettings();

        original.CopyTo(settings);
        settings.name = name;
        importer.SetPlatformTextureSettings(settings);
    }
Beispiel #2
0
        bool _SetTextureFormatUnity5(Texture2D tx, TextureFormatInfo toThisFormat, bool addToList, bool setNormalMap, TextureImporter textureImporter)
        {
            bool doImport = false;

            TextureFormatInfo restoreTfi = new TextureFormatInfo(textureImporter.textureCompression,
                                                                 false,
                                                                 toThisFormat.platform,
                                                                 TextureImporterFormat.RGBA32,
                                                                 textureImporter.textureType == TextureImporterType.NormalMap);

            string platform = toThisFormat.platform;

            if (platform != null)
            {
                TextureImporterPlatformSettings tips = textureImporter.GetPlatformTextureSettings(platform);
                if (tips.overridden)
                {
                    restoreTfi.platformFormat             = tips.format;
                    restoreTfi.platformCompressionQuality = tips.compressionQuality;
                    TextureImporterPlatformSettings tipsOverridden = new TextureImporterPlatformSettings();
                    tips.CopyTo(tipsOverridden);
                    tipsOverridden.compressionQuality = toThisFormat.platformCompressionQuality;
                    tipsOverridden.format             = toThisFormat.platformFormat;
                    textureImporter.SetPlatformTextureSettings(tipsOverridden);
                    doImport = true;
                }
            }

            if (textureImporter.textureCompression != toThisFormat.compression)
            {
                textureImporter.textureCompression = toThisFormat.compression;
                doImport = true;
            }

            if (doImport)
            {
                string s;
                if (addToList)
                {
                    s = "Setting texture compression for ";
                }
                else
                {
                    s = "Restoring texture compression for ";
                }
                s += String.Format("{0}  FROM: compression={1} isNormal{2} TO: compression={3} isNormal={4} ", tx, restoreTfi.compression, restoreTfi.isNormalMap, toThisFormat.compression, setNormalMap);
                if (toThisFormat.platform != null)
                {
                    s += String.Format(" setting platform override format for platform {0} to {1} compressionQuality {2}", toThisFormat.platform, toThisFormat.platformFormat, toThisFormat.platformCompressionQuality);
                }
                Debug.Log(s);
            }
            if (doImport && addToList && !_textureFormatMap.ContainsKey(tx))
            {
                _textureFormatMap.Add(tx, restoreTfi);
            }
            return(doImport);
        }
        public static string[] Export(SpriteAtlas atlas, string dirPath)
        {
            string platformName = "Standalone";

            if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android)
            {
                platformName = "Android";
            }
            else if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.iOS)
            {
                platformName = "iPhone";
            }

            TextureImporterPlatformSettings tips       = atlas.GetPlatformSettings(platformName);
            TextureImporterPlatformSettings cachedTips = new TextureImporterPlatformSettings();

            tips.CopyTo(cachedTips);

            tips.overridden = true;
            tips.format     = TextureImporterFormat.RGBA32;
            //tips.maxTextureSize = 1024;
            atlas.SetPlatformSettings(tips);

            List <string> texturePathList = new List <string>();

            SpriteAtlasUtility.PackAtlases(new SpriteAtlas[] { atlas }, EditorUserBuildSettings.activeBuildTarget);
            MethodInfo getPreviewTextureMI = typeof(SpriteAtlasExtensions).GetMethod("GetPreviewTextures", BindingFlags.Static | BindingFlags.NonPublic);

            Texture2D[] atlasTextures = (Texture2D[])getPreviewTextureMI.Invoke(null, new SystemObject[] { atlas });
            if (atlasTextures != null && atlasTextures.Length > 0)
            {
                for (int i = 0; i < atlasTextures.Length; i++)
                {
                    Texture2D packTexture = atlasTextures[i];
                    byte[]    rawBytes    = packTexture.GetRawTextureData();

                    Texture2D nTexture = new Texture2D(packTexture.width, packTexture.height, packTexture.format, false, false);
                    nTexture.LoadRawTextureData(rawBytes);
                    nTexture.Apply();
                    string textPath = string.Format("{0}/{1}_{2}.png", dirPath, atlas.name, i);
                    File.WriteAllBytes(textPath, nTexture.EncodeToPNG());

                    texturePathList.Add(textPath);
                }
            }

            atlas.SetPlatformSettings(cachedTips);

            return(texturePathList.ToArray());
        }
Beispiel #4
0
        bool _SetTextureFormat2017(Texture2D tx, TextureFormatInfo toThisFormat, bool addToList, bool setNormalMap, TextureImporter textureImporter)
        {
            bool is2017 = Application.unityVersion.StartsWith("20");

            if (!is2017)
            {
                Debug.LogError("Wrong texture format converter. 2017 Should not be called for Unity Version " + Application.unityVersion);
                return(false);
            }

            bool doImport = false;
            TextureFormatInfo restoreTfi = new TextureFormatInfo(textureImporter.textureCompression,
                                                                 textureImporter.crunchedCompression,
                                                                 toThisFormat.platform,
                                                                 TextureImporterFormat.RGBA32,
                                                                 textureImporter.textureType == TextureImporterType.NormalMap);
            string platform = toThisFormat.platform;

            if (platform != null)
            {
                TextureImporterPlatformSettings tips = textureImporter.GetPlatformTextureSettings(platform);
                if (tips.overridden)
                {
                    restoreTfi.platformFormat             = tips.format;
                    restoreTfi.platformCompressionQuality = tips.compressionQuality;
                    restoreTfi.doCrunchCompression        = tips.crunchedCompression;
                    restoreTfi.isNormalMap = textureImporter.textureType == TextureImporterType.NormalMap;
                    TextureImporterPlatformSettings tipsOverridden = new TextureImporterPlatformSettings();
                    tips.CopyTo(tipsOverridden);
                    tipsOverridden.compressionQuality  = toThisFormat.platformCompressionQuality;
                    tipsOverridden.crunchedCompression = toThisFormat.doCrunchCompression;
                    tipsOverridden.format = toThisFormat.platformFormat;
                    textureImporter.SetPlatformTextureSettings(tipsOverridden);
                    doImport = true;
                }
            }

            if (textureImporter.crunchedCompression != toThisFormat.doCrunchCompression)
            {
                textureImporter.crunchedCompression = toThisFormat.doCrunchCompression;
                doImport = true;
            }
            if (_ChangeNormalMapTypeIfNecessary(textureImporter, setNormalMap))
            {
                doImport = true;
            }

            if (doImport)
            {
                string s;
                if (addToList)
                {
                    s = "Setting texture compression for ";
                }
                else
                {
                    s = "Restoring texture compression for ";
                }
                s += String.Format("{0}  FROM: compression={1} isNormal{2} TO: compression={3} isNormal={4} ", tx, restoreTfi.compression, restoreTfi.isNormalMap, toThisFormat.compression, setNormalMap);
                if (toThisFormat.platform != null)
                {
                    s += String.Format(" setting platform override format for platform {0} to {1} compressionQuality {2}", toThisFormat.platform, toThisFormat.platformFormat, toThisFormat.platformCompressionQuality);
                }
                Debug.Log(s);
                if (doImport && addToList && !_textureFormatMap.ContainsKey(tx))
                {
                    _textureFormatMap.Add(tx, restoreTfi);
                }
            }
            return(doImport);
        }
Beispiel #5
0
        private static void ExportAltas()
        {
            if (Selection.objects == null || Selection.objects.Length == 0)
            {
                return;
            }
            string platformName = "Standalone";

            if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android)
            {
                platformName = "Android";
            }
            else if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.iOS)
            {
                platformName = "iPhone";
            }
            List <SpriteAtlas> atlasList = new List <SpriteAtlas>();
            Dictionary <SpriteAtlas, TextureImporterPlatformSettings> tipsDic = new Dictionary <SpriteAtlas, TextureImporterPlatformSettings>();

            foreach (UnityObject uObj in Selection.objects)
            {
                SpriteAtlas atlas = uObj as SpriteAtlas;
                if (atlas != null)
                {
                    atlasList.Add(atlas);

                    TextureImporterPlatformSettings tips  = atlas.GetPlatformSettings(platformName);
                    TextureImporterPlatformSettings ntips = new TextureImporterPlatformSettings();
                    tips.CopyTo(ntips);
                    tipsDic.Add(atlas, ntips);

                    tips.overridden = true;
                    tips.format     = TextureImporterFormat.RGBA32;
                    atlas.SetPlatformSettings(tips);
                }
            }
            if (atlasList.Count == 0)
            {
                EditorUtility.DisplayDialog("Warning", "Can't Found Sprite Atlas In Selection", "OK");
                return;
            }
            UnityEditor.U2D.SpriteAtlasUtility.PackAtlases(atlasList.ToArray(), EditorUserBuildSettings.activeBuildTarget);
            string outputDirPath = EditorUtility.OpenFolderPanel("Save Atlas To", "D:/", "");

            if (string.IsNullOrEmpty(outputDirPath))
            {
                EditorUtility.DisplayDialog("Warning", "Please Choose a Folder", "OK");
                return;
            }

            MethodInfo getPreviewTextureMI = typeof(SpriteAtlasExtensions).GetMethod("GetPreviewTextures", BindingFlags.Static | BindingFlags.NonPublic);

            foreach (SpriteAtlas atlas in atlasList)
            {
                Texture2D[] atlasTextures = (Texture2D[])getPreviewTextureMI.Invoke(null, new SystemObject[] { atlas });
                if (atlasTextures != null && atlasTextures.Length > 0)
                {
                    for (int i = 0; i < atlasTextures.Length; i++)
                    {
                        Texture2D packTexture = atlasTextures[i];
                        byte[]    rawBytes    = packTexture.GetRawTextureData();

                        Texture2D nTexture = new Texture2D(packTexture.width, packTexture.height, packTexture.format, false, false);
                        nTexture.LoadRawTextureData(rawBytes);
                        nTexture.Apply();
                        string textPath = string.Format("{0}/{1}_{2}.png", outputDirPath, atlas.name, i);
                        File.WriteAllBytes(textPath, nTexture.EncodeToPNG());
                    }
                }
            }

            foreach (var kvp in tipsDic)
            {
                kvp.Key.SetPlatformSettings(kvp.Value);
            }
        }
Beispiel #6
0
        void SetTextureFormat(Texture2D tx, TextureFormatInfo toThisFormat, bool addToList, bool setNormalMap)
        {
            AssetImporter ai = AssetImporter.GetAtPath(AssetDatabase.GetAssetOrScenePath(tx));

            if (ai != null && ai is UnityEditor.TextureImporter)
            {
                TextureImporter textureImporter = (TextureImporter)ai;
                bool            doImport        = false;

                TextureFormatInfo restoreTfi = new TextureFormatInfo(textureImporter.textureCompression,
                                                                     toThisFormat.platform,
                                                                     TextureImporterFormat.RGBA32,
                                                                     textureImporter.textureType == TextureImporterType.NormalMap);

                bool is2017 = Application.unityVersion.StartsWith("2017");
                if (is2017)
                {
                    //don't do any of this for 2017
                }
                else
                {
                    string s;
                    if (addToList)
                    {
                        s = "Setting texture compression for ";
                    }
                    else
                    {
                        s = "Restoring texture compression for ";
                    }
                    s += String.Format("{0}  to compression={1} isNormal={2} ", tx, toThisFormat.compression, setNormalMap);
                    if (toThisFormat.platform != null)
                    {
                        s += String.Format(" setting platform override format for platform {0} to {1} compressionQuality {2}", toThisFormat.platform, toThisFormat.platformFormat, toThisFormat.platformCompressionQuality);
                    }
                    Debug.Log(s);
                    string platform = toThisFormat.platform;
                    if (platform != null)
                    {
                        TextureImporterPlatformSettings tips = textureImporter.GetPlatformTextureSettings(platform);
                        if (tips.overridden)
                        {
                            restoreTfi.platformFormat             = tips.format;
                            restoreTfi.platformCompressionQuality = tips.compressionQuality;
                            TextureImporterPlatformSettings tipsOverridden = new TextureImporterPlatformSettings();
                            tips.CopyTo(tipsOverridden);
                            tipsOverridden.compressionQuality = toThisFormat.platformCompressionQuality;
                            tipsOverridden.format             = toThisFormat.platformFormat;
                            textureImporter.SetPlatformTextureSettings(tipsOverridden);
                            doImport = true;
                        }
                    }
                    if (textureImporter.textureCompression != toThisFormat.compression)
                    {
                        textureImporter.textureCompression = toThisFormat.compression;
                        doImport = true;
                    }
                }
                if (textureImporter.textureType == TextureImporterType.NormalMap && !setNormalMap)
                {
                    textureImporter.textureType = TextureImporterType.Default;
                    doImport = true;
                }

                if (textureImporter.textureType != TextureImporterType.NormalMap && setNormalMap)
                {
                    textureImporter.textureType = TextureImporterType.NormalMap;
                    doImport = true;
                }
                if (addToList && !_textureFormatMap.ContainsKey(tx))
                {
                    _textureFormatMap.Add(tx, restoreTfi);
                }
                if (doImport)
                {
                    AssetDatabase.ImportAsset(AssetDatabase.GetAssetOrScenePath(tx), ImportAssetOptions.ForceUpdate);
                }
            }
        }