Exemple #1
0
    /// <summary>
    /// 是否设置过Android图片
    /// </summary>
    /// <param name="ti"></param>
    /// <param name="tx"></param>
    /// <returns></returns>
    static bool IsSetAndroidTexture(TextureImporter ti, Texture tx)
    {
        bool isSet        = false;
        var  overridden   = ti.GetPlatformTextureSettings(Android).overridden;         //IOS 平台是否被设置过
        var  isBigTexture = IsBigTexture(tx);
        var  isHaveAlpha  = IsHaveAlpha(ti);

        if (overridden)
        {
            if (isHaveAlpha)
            {
                if (isBigTexture)
                {
                    if (ti.textureType == TextureImporterType.GUI &&
                        ti.alphaSource == TextureImporterAlphaSource.FromInput &&
                        ti.npotScale == TextureImporterNPOTScale.ToNearest &&
                        ti.GetAutomaticFormat(Android) == _androidBigAlphaFormat)
                    {
                        isSet = true;
                    }
                }
                else
                {
                    if (ti.textureType == TextureImporterType.GUI &&
                        ti.alphaSource == TextureImporterAlphaSource.FromInput &&
                        ti.npotScale == TextureImporterNPOTScale.ToNearest &&
                        ti.GetAutomaticFormat(Android) == _androidSmallAlphaFormat)
                    {
                        isSet = true;
                    }
                }
            }
            else
            {
                if (isBigTexture)
                {
                    if (ti.textureType == TextureImporterType.GUI &&
                        ti.alphaSource == TextureImporterAlphaSource.None &&
                        ti.npotScale == TextureImporterNPOTScale.ToNearest &&
                        ti.GetAutomaticFormat(Android) == _androidBigFormat)
                    {
                        isSet = true;
                    }
                }
                else
                {
                    if (ti.textureType == TextureImporterType.GUI &&
                        ti.alphaSource == TextureImporterAlphaSource.None &&
                        ti.npotScale == TextureImporterNPOTScale.ToNearest &&
                        ti.GetAutomaticFormat(Android) == _androidSmallFormat)
                    {
                        isSet = true;
                    }
                }
            }
        }
        return(isSet);
    }
Exemple #2
0
    private static void Process(TextureImporter texImp, Texture2D texture, string rootPath, string altasPath)
    {
        altasPath = altasPath.Substring(altasPath.IndexOf("/") + 1);
        altasPath = altasPath.Replace(Path.GetExtension(altasPath), "");
        string filesDirectory = Path.Combine(rootPath, altasPath);
        bool   isRead         = texImp.isReadable;
        bool   isRGB32        = texImp.GetAutomaticFormat(platform) == TextureImporterFormat.RGBA32;

        System.Action callBack = null;
        if (!isRead)
        {
            texImp.isReadable = true;
            AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(texImp));
            callBack += () => texImp.isReadable = isRead;
        }
        if (!isRGB32)
        {
            TextureImporterPlatformSettings textureImporterPlatformSettings = GetOldTextureImporterPlatformSettings(texImp);
            SetTexturePlatformSettings(!isRGB32, texImp, TextureImporterFormat.RGBA32);
            callBack += () => texImp.SetPlatformTextureSettings(textureImporterPlatformSettings);
        }

        if (!isRead || !isRGB32)
        {
            callBack += () => AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(texImp));
        }

        if (Directory.Exists(filesDirectory))
        {
            Directory.Delete(filesDirectory, true);
        }
        Directory.CreateDirectory(filesDirectory);

        foreach (SpriteMetaData metaData in texImp.spritesheet)
        {
            Texture2D myimage = new Texture2D((int)metaData.rect.width, (int)metaData.rect.height);
            for (int y = (int)metaData.rect.y; y < metaData.rect.y + metaData.rect.height; y++)
            {
                for (int x = (int)metaData.rect.x; x < metaData.rect.x + metaData.rect.width; x++)
                {
                    myimage.SetPixel(x - (int)metaData.rect.x, y - (int)metaData.rect.y, texture.GetPixel(x, y));
                }
            }
            var    pngData  = myimage.EncodeToPNG();
            string fileName = Path.Combine(filesDirectory, metaData.name + ".PNG");
            fileName = fileName.Replace("\\", "/");
            File.WriteAllBytes(fileName, pngData);
        }
        if (callBack != null)
        {
            callBack();
        }
        callBack = null;
    }
Exemple #3
0
        public static TextureImporterFormat GetPlatformTextureSettings(TextureImporter textureImporter, string platform)
        {
            if (textureImporter == null)
            {
                Debug.LogError("Invalid texture importer.");
                return(TextureImporterFormat.Automatic);
            }

            if (platform == string.Empty)
            {
                Debug.LogError("Invlid platform.");
                return(TextureImporterFormat.Automatic);
            }

            TextureImporterFormat textureImporterFormat = textureImporter.GetPlatformTextureSettings(platform).format;

            return((textureImporterFormat == TextureImporterFormat.Automatic) ? textureImporter.GetAutomaticFormat(platform) : textureImporterFormat);
        }
Exemple #4
0
    /// <summary>
    /// 是否设置Android,live2d
    /// </summary>
    /// <param name="ti"></param>
    /// <returns></returns>
    static bool IsSetAndroidSpriteTexture(TextureImporter ti)
    {
        bool isSet      = false;
        var  overridden = ti.GetPlatformTextureSettings(Android).overridden;

        if (overridden)
        {
            if (ti.GetAutomaticFormat(Android) == TextureImporterFormat.ETC2_RGBA8 &&
                ti.textureType == TextureImporterType.Sprite &&
                ti.spriteImportMode == SpriteImportMode.Single &&
                ti.alphaSource == TextureImporterAlphaSource.FromInput &&
                ti.alphaIsTransparency && ti.sRGBTexture)
            {
                return(true);
            }
        }

        return(isSet);
    }
Exemple #5
0
    /// <summary>
    /// 是否设置IOS,live2d
    /// </summary>
    /// <param name="ti"></param>
    /// <returns></returns>
    static bool IsSetIOSSpriteTexture(TextureImporter ti)
    {
        bool isSet      = false;
        var  overridden = ti.GetPlatformTextureSettings(iOS).overridden;           //IOS 平台是否被设置过

        if (overridden)
        {
            if (ti.GetAutomaticFormat(iOS) == TextureImporterFormat.ASTC_RGBA_4x4 &&
                ti.textureType == TextureImporterType.Sprite &&
                ti.spriteImportMode == SpriteImportMode.Single &&
                ti.alphaSource == TextureImporterAlphaSource.FromInput &&
                ti.alphaIsTransparency && ti.sRGBTexture)
            {
                return(true);
            }
        }

        return(isSet);
    }
Exemple #6
0
        private 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;
            bool   isAutoPVRTC = false;

            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;
                }

                isAutoPVRTC = MBVersionEditor.IsAutoPVRTC(tips.format, textureImporter.GetAutomaticFormat(platform));
            }


            if (isAutoPVRTC && textureImporter.textureCompression != toThisFormat.compression)
            {
                textureImporter.textureCompression = toThisFormat.compression;
                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);
        }
Exemple #7
0
        public Dictionary <string, string> GetPlatformFields(string platform)
        {
            if (platformMap.ContainsKey(platform) == false || platformMap[platform] == null)
            {
                return(null);
            }

            var platformImporter = platformMap[platform];
            var format           = platformImporter.format == TextureImporterFormat.Automatic ? textureImporter.GetAutomaticFormat(platform) : platformImporter.format;

            var fieldMap = new Dictionary <string, string>()
            {
                // Basic
                ["Path/Name"] = textureImporter.assetPath,

                // Platform
                ["Overridden"]         = platformImporter.overridden.ToString(),
                ["Max size"]           = platformImporter.maxTextureSize.ToString(),
                ["Resize algorithm"]   = platformImporter.resizeAlgorithm.ToString(),
                ["Format"]             = format.ToString(),
                ["Compressor quality"] = platformImporter.compressionQuality.ToString(),
            };

            fieldMap["Original size"] = $"{texture.width}x{texture.height}";

            if (platform == Platforms[0])
            {
                fieldMap["Android ETC2 Fallback override"] = platformImporter.androidETC2FallbackOverride.ToString();
            }

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

            return(fieldMap);
        }
    void OnPostprocessTexture(Texture2D texture)
    {
        //------------------------------------
        // 除外するパスに指定してある場合は、はじく
        //------------------------------------
        for (int i = 0; i < m_ExcludePaths.Length; i++)
        {
            if (assetPath.StartsWith(m_ExcludePaths[i]))
            {
                Debug.Log("AssetPath:" + assetPath);
                return;
            }
        }

        TextureImporter importer = (assetImporter as TextureImporter);
        string          format   = importer.GetAutomaticFormat(Platform).ToString();

        if (!assetPath.EndsWith(".png"))
        {
            Debug.Log("AssetPath:" + assetPath);
            return;
        }

        if (assetPath.EndsWith("_mask.png"))
        {
            Debug.Log("AssetPath:" + assetPath);
            return;
        }

#if true
        // アルファ抜きの輪郭部分の画質改善
        if (texture.format == TextureFormat.RGBA32 &&
            importer.textureType == TextureImporterType.Sprite &&
            importer.spriteImportMode == SpriteImportMode.Multiple
            )
        {
            Debug.Log("Fill transparent..");
            Texture2D texture2       = _fillTransparent(texture);
            byte[]    texture2_bytes = texture2.EncodeToPNG();
            File.WriteAllBytes(assetPath, texture2_bytes);
            return;
        }
#endif

        if (!format.Equals("Alpha8"))
        {
            Debug.Log("FORMAT:" + format);
            return;
        }

        Debug.Log("Create Mask..");
        Texture2D mask = new Texture2D(texture.width, texture.height, TextureFormat.ARGB32, false);
        mask.wrapMode = TextureWrapMode.Clamp;

        // Convert the source image into a mask.
        var pixels = texture.GetPixels();
        for (int i = 0; i < pixels.Length; i++)
        {
            var a = pixels[i].a;
            pixels[i] = new Color(a, a, a, a);
        }
        mask.SetPixels(pixels);

        byte[] bytes = mask.EncodeToPNG();

        //        File.WriteAllBytes(Application.dataPath + "/../SavedScreen.png", bytes);
        //        EditorUtility.CompressTexture(mask, CompressionFormat, TextureCompressionQuality.Best);

        //        var maskPath = assetPath.Replace(".png", string.Format("_mask_{0}.asset",CompressionFormat.ToString()));
        var maskPath = assetPath.Replace(".png", "_mask.png");
        File.WriteAllBytes(maskPath, bytes);

        AssetDatabase.Refresh();

        //        var maskAsset = AssetDatabase.LoadAssetAtPath(maskPath, typeof(Texture2D)) as Texture2D;
        //        if (maskAsset == null)
        //        {
        //            AssetDatabase.CreateAsset(mask, maskPath);
        //        }
        //        else
        //        {
        //            EditorUtility.CopySerialized(mask, maskAsset);
        //        }
    }