Exemplo n.º 1
0
    public bool Fix(string path)
    {
        TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
        TextureImporterPlatformSettings textureImporterPlatformSettings = new TextureImporterPlatformSettings();

        textureImporterPlatformSettings.overridden = true;
        textureImporterPlatformSettings.name       = "Android";
        if (textureImporter.DoesSourceTextureHaveAlpha())
        {
            textureImporterPlatformSettings.format = TextureImporterFormat.ASTC_RGBA_6x6;
        }
        else
        {
            textureImporterPlatformSettings.format = TextureImporterFormat.ASTC_RGB_6x6;
        }
        textureImporter.SetPlatformTextureSettings(textureImporterPlatformSettings);
        textureImporterPlatformSettings            = new TextureImporterPlatformSettings();
        textureImporterPlatformSettings.overridden = true;
        textureImporterPlatformSettings.name       = "iPhone";
        if (textureImporter.DoesSourceTextureHaveAlpha())
        {
            textureImporterPlatformSettings.format = TextureImporterFormat.ASTC_RGBA_6x6;
        }
        else
        {
            textureImporterPlatformSettings.format = TextureImporterFormat.ASTC_RGB_6x6;
        }
        textureImporter.SetPlatformTextureSettings(textureImporterPlatformSettings);
        textureImporter.SaveAndReimport();
        return(true);
    }
Exemplo n.º 2
0
        //进行资源格式转换
        private void ChangeTexureFormat(TextureImporter textureImporter)
        {
            //不设置图片的格式
            //textureImporter.textureType = TextureImporterType.Default;
            bool isPowOf2 = IsPowOf2(textureImporter);
            TextureImporterPlatformSettings settings        = textureImporter.GetPlatformTextureSettings("iPhone");
            TextureImporterFormat           defaultAlpha    = isPowOf2 ?TextureImporterFormat.PVRTC_RGBA4 : TextureImporterFormat.ASTC_RGBA_4x4;
            TextureImporterFormat           defaultNotAlpha = isPowOf2? TextureImporterFormat.PVRTC_RGB4 :TextureImporterFormat.ASTC_RGB_6x6;

            settings.overridden     = true;
            settings.maxTextureSize = kTextureMaxSize;
            settings.format         = textureImporter.DoesSourceTextureHaveAlpha() ? defaultAlpha : defaultNotAlpha;
            textureImporter.SetPlatformTextureSettings(settings);

            settings            = textureImporter.GetPlatformTextureSettings("Android");
            settings.overridden = true;
            bool isDivisibleOf4 = IsDivisibleOf4(textureImporter);

            //Android 平台ETC 必须满足被4整除,否则使用ASTC,不带alpha使用4x4 带Alpha 6x6
            defaultAlpha    = isDivisibleOf4? TextureImporterFormat.ETC2_RGBA8 : TextureImporterFormat.ASTC_RGBA_4x4;
            defaultNotAlpha = isDivisibleOf4? TextureImporterFormat.ETC_RGB4 : TextureImporterFormat.ASTC_RGB_6x6;

            settings.maxTextureSize = kTextureMaxSize;
            settings.format         = textureImporter.DoesSourceTextureHaveAlpha() ? defaultAlpha : defaultNotAlpha;
            textureImporter.SetPlatformTextureSettings(settings);

            if (!isPowOf2)
            {
                Debug.LogWarning("Texture is not pow of 2");
            }
            if (!isDivisibleOf4)
            {
                Debug.LogWarning("Texture is not divisible of 4");
            }
        }
Exemplo n.º 3
0
        public void AddTexture(string stringPath, TextureImporter ti)
        {
            var tiAnd = ti.GetPlatformTextureSettings("Android");
            var tiIos = ti.GetPlatformTextureSettings("iPhone");

            foreach (var group in groups)
            {
                if (group.android.overridden == tiAnd.overridden && group.iphone.overridden == tiIos.overridden)
                {
                    if (tiAnd.overridden && tiIos.overridden)
                    {
                        if (@group.android.textureCompression != tiAnd.textureCompression ||
                            @group.android.format != tiAnd.format ||
                            @group.iphone.textureCompression != tiIos.textureCompression ||
                            @group.iphone.format != tiIos.format)
                        {
                            continue;
                        }
                    }
                    else if (tiAnd.overridden)
                    {
                        if (@group.android.textureCompression != tiAnd.textureCompression ||
                            @group.android.format != tiAnd.format)
                        {
                            continue;
                        }
                    }
                    else if (tiIos.overridden)
                    {
                        if (@group.iphone.textureCompression != tiIos.textureCompression ||
                            @group.iphone.format != tiIos.format)
                        {
                            continue;
                        }
                    }
                    else if (@group.normal.textureCompression != ti.textureCompression ||
                             group.hasAlpha != ti.DoesSourceTextureHaveAlpha())
                    {
                        continue;
                    }

                    group.textures.Add(stringPath);
                    return;
                }
            }

            var newAtlas = new Atlas(ti.DoesSourceTextureHaveAlpha());

            newAtlas.textures.Add(stringPath);
            newAtlas.iphone  = tiIos;
            newAtlas.android = tiAnd;
            newAtlas.normal  = ti.GetDefaultPlatformTextureSettings();
            groups.Add(newAtlas);
        }
Exemplo n.º 4
0
    private void OnPreprocessTexture()
    {
        TextureImporter textureImporter = assetImporter as TextureImporter;

        if (IsFirstImport(textureImporter))
        {
            TextureImporterSettings textureImportSetting = new TextureImporterSettings();
            textureImporter.ReadTextureSettings(textureImportSetting);

            //textureImportSetting.spriteMeshType = SpriteMeshType.Tight;
            textureImportSetting.spriteExtrude = 1;
            textureImportSetting.spriteGenerateFallbackPhysicsShape = false;
            textureImporter.spritePixelsPerUnit = 100;
            textureImporter.SetTextureSettings(textureImportSetting);

            textureImporter.isReadable = false;

            TextureImporterPlatformSettings settings = textureImporter.GetPlatformTextureSettings("iPhone");
            bool isPowerOfTwo = IsPowerOfTwo(textureImporter);
            TextureImporterFormat defaultAlpha    = isPowerOfTwo ? TextureImporterFormat.PVRTC_RGBA4 : TextureImporterFormat.ASTC_RGBA_4x4;
            TextureImporterFormat defaultNotAlpha = isPowerOfTwo ? TextureImporterFormat.PVRTC_RGB4 : TextureImporterFormat.ASTC_RGB_6x6;
            settings.overridden = true;
            settings.format     = textureImporter.DoesSourceTextureHaveAlpha() ? defaultAlpha : defaultNotAlpha;
            textureImporter.SetPlatformTextureSettings(settings);

            settings                      = textureImporter.GetPlatformTextureSettings("Android");
            settings.overridden           = true;
            settings.allowsAlphaSplitting = false;
            bool divisible4 = IsDivisibleOf4(textureImporter);
            defaultAlpha    = divisible4 ? TextureImporterFormat.ETC2_RGBA8Crunched : TextureImporterFormat.ASTC_RGBA_4x4;
            defaultNotAlpha = isPowerOfTwo ? TextureImporterFormat.ETC_RGB4Crunched : divisible4? TextureImporterFormat.ETC2_RGBA8Crunched:TextureImporterFormat.ASTC_RGB_6x6;
            settings.format = textureImporter.DoesSourceTextureHaveAlpha() ? defaultAlpha : defaultNotAlpha;
            textureImporter.SetPlatformTextureSettings(settings);

            settings                      = textureImporter.GetPlatformTextureSettings("Windows");
            settings.overridden           = true;
            settings.allowsAlphaSplitting = false;
            defaultAlpha                  = TextureImporterFormat.DXT5Crunched;
            defaultNotAlpha               = TextureImporterFormat.DXT1Crunched;
            settings.format               = textureImporter.DoesSourceTextureHaveAlpha() ? defaultAlpha : defaultNotAlpha;
            textureImporter.SetPlatformTextureSettings(settings);

            //Assets/Works/Resources/UI/Sprite/DialogCommon/坐标ICON.png
            if (textureImporter != null && textureImporter.assetPath.StartsWith("Assets/Works/Resources/UI"))
            {
                textureImporter.textureType   = TextureImporterType.Sprite;
                textureImporter.mipmapEnabled = false;
            }

            textureImporter.userData = "exportSuccess";
        }
    }
Exemplo n.º 5
0
    public static void CompressTex(TextureImporter textureImporter, string path, Texture2D texture)
    {
        if (!textureImporter || textureImporter.textureType == TextureImporterType.Lightmap)
        {
            return;
        }

        TextureImporterFormat PCFormat      = TextureImporterFormat.DXT1;
        TextureImporterFormat AndroidFormat = TextureImporterFormat.ETC_RGB4;
        TextureImporterFormat IOSFormat     = TextureImporterFormat.PVRTC_RGB4;

        if (textureImporter.DoesSourceTextureHaveAlpha())
        {
            PCFormat      = TextureImporterFormat.DXT5;
            AndroidFormat = TextureImporterFormat.ETC2_RGBA8;
            IOSFormat     = TextureImporterFormat.PVRTC_RGBA4;
        }
        textureImporter.mipmapEnabled = false;
        textureImporter.isReadable    = false;
        textureImporter.npotScale     = TextureImporterNPOTScale.ToNearest;
        textureImporter.filterMode    = FilterMode.Bilinear;
        //textureImporter.wrapMode = TextureWrapMode.Repeat;
        textureImporter.anisoLevel         = 1;
        textureImporter.textureCompression = TextureImporterCompression.Compressed;
        int size = Mathf.Max(texture.height, texture.width);

        SetPlatformTextureSettings("Standalone", size, textureImporter, PCFormat);
        SetPlatformTextureSettings("Android", size, textureImporter, AndroidFormat);
        SetPlatformTextureSettings("iPhone", size, textureImporter, IOSFormat);
        AssetDatabase.ImportAsset(path);
    }
Exemplo n.º 6
0
    void OnPreprocessTexture()
    {
        if (!assetPath.StartsWith("Assets/Localization/") && !assetPath.StartsWith("Assets/Res/"))
        {
            return;
        }
        TextureImporter         importer       = (TextureImporter)assetImporter;
        TextureImporterSettings defaultSetting = new TextureImporterSettings();

        importer.ReadTextureSettings(defaultSetting);
        defaultSetting.mipmapEnabled = false;
        defaultSetting.npotScale     = TextureImporterNPOTScale.None; //不需要压缩为2的幂次方
        defaultSetting.spriteGenerateFallbackPhysicsShape = false;
        if (importer.textureType == TextureImporterType.Lightmap)     //设置lightmap的属性
        {
            defaultSetting.streamingMipmaps = false;
        }
        importer.SetTextureSettings(defaultSetting);

        //IOS平台特殊的压缩设置
        TextureImporterPlatformSettings settings = importer.GetPlatformTextureSettings("iPhone");

        settings.overridden     = true;
        settings.maxTextureSize = importer.maxTextureSize;
        if (importer.DoesSourceTextureHaveAlpha())
        {
            settings.format = TextureImporterFormat.ASTC_RGBA_6x6;
        }
        else
        {
            settings.format = TextureImporterFormat.ASTC_RGB_6x6;
        }
        importer.SetPlatformTextureSettings(settings);
    }
Exemplo n.º 7
0
        /**
         * Does two things:
         * 1 - Changes the NPOT strategy to "ToNearest";
         * 2 - Changes the compression to DXT1 or DXT5 if compression is set to something different than DTX1.
         * It only changes the meta file of the texture.
         */
        static void DxtCompressTexture(TextureImporter textureImporter)
        {
            TextureImporterFormat   format;
            TextureImporterSettings textureImporterSettings = new TextureImporterSettings();

            textureImporter.ReadTextureSettings(textureImporterSettings);
            format = textureImporterSettings.textureFormat;

            if (format != TextureImporterFormat.DXT1)
            {
                if (textureImporter.DoesSourceTextureHaveAlpha())
                {
                    format = TextureImporterFormat.DXT5;
                }
                else
                {
                    format = TextureImporterFormat.DXT1;
                }
            }

            textureImporter.npotScale = TextureImporterNPOTScale.ToNearest;

            textureImporterSettings.textureFormat = format;
            textureImporter.SetTextureSettings(textureImporterSettings);
        }
Exemplo n.º 8
0
        //压缩图片
        private static bool _DefaultCompress(Texture2D tex, TextureImporter textureImporter, string path)
        {
            if (path.IndexOf("_A.") >= 0 || path.IndexOf("_NRM.") >= 0)
            {
                return(false);
            }
            int size = tex.width > tex.height ? tex.width : tex.height;
            TextureImporterSettings setting = new TextureImporterSettings();

            setting.textureType = TextureImporterType.Default;

            setting.textureType   = TextureImporterType.Default;
            setting.aniso         = 0;
            setting.mipmapEnabled = size > 256;
            setting.npotScale     = TextureImporterNPOTScale.ToNearest;
            setting.wrapMode      = TextureWrapMode.Repeat;
            setting.filterMode    = FilterMode.Bilinear;

            if (textureImporter.DoesSourceTextureHaveAlpha())
            {
                setting.readable = true;
                AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
                int extIndex = path.LastIndexOf(".");
                if (extIndex >= 0)
                {
                    setting.readable = true;
                    string alphaTexPath = path.Substring(0, extIndex) + "_A.png";
                    MakeAlphaRTex(alphaTexPath, size / 2, tex);
                }
            }
            textureImporter.SetTextureSettings(setting);
            SetPlatformSetting(textureImporter, size);
            textureImporter.isReadable = false;
            return(true);
        }
Exemplo n.º 9
0
    public static void FormatTexture(TextureImporter importer)
    {
        importer.isReadable    = CommonAssetProcessor.ReadWrite(importer.assetPath);
        importer.wrapMode      = TextureWrapMode.Clamp;
        importer.anisoLevel    = 1;
        importer.filterMode    = FilterMode.Bilinear;
        importer.mipmapEnabled = CommonAssetProcessor.HasMipMap(importer.assetPath);
        if (CommonAssetProcessor.IsUI(importer.assetPath))
        {
            importer.mipmapEnabled = false;
            importer.textureType   = TextureImporterType.Sprite;
            importer.npotScale     = TextureImporterNPOTScale.None;
        }
        importer.npotScale = TextureImporterNPOTScale.None;
        TextureImporterPlatformSettings psAndroid = importer.GetPlatformTextureSettings("Android");
        TextureImporterPlatformSettings psIphone  = importer.GetPlatformTextureSettings("iPhone");

        psAndroid.overridden = true;
        psIphone.overridden  = true;
        if (importer.DoesSourceTextureHaveAlpha())
        {
            psAndroid.format = TextureImporterFormat.ASTC_6x6;
            psIphone.format  = TextureImporterFormat.ASTC_6x6;
        }
        else
        {
            psAndroid.format = TextureImporterFormat.ASTC_6x6;
            psIphone.format  = TextureImporterFormat.ASTC_6x6;
        }
        importer.SetPlatformTextureSettings(psAndroid);
        importer.SetPlatformTextureSettings(psIphone);
        importer.SaveAndReimport();
    }
Exemplo n.º 10
0
    static void RemoveAlphaChanel()
    {
        Texture2D tex = Selection.activeObject as Texture2D;

        if (!tex)
        {
            EditorUtility.DisplayDialog("error", "请选择一张图片", "ok");
            return;
        }
        string path = AssetDatabase.GetAssetPath(Selection.activeObject);

        if (!path.EndsWith("png") && !path.EndsWith("PNG"))
        {
            EditorUtility.DisplayDialog("error", "图片不是png或PNG:" + path, "ok");
            return;
        }
        TextureImporter importer = AssetImporter.GetAtPath(path) as TextureImporter;

#if UNITY_5_5_OR_NEWER
        var originFormat = importer.textureCompression;
#else
        TextureImporterFormat originFormat = importer.textureFormat;
#endif
        bool haveAlpha = importer.DoesSourceTextureHaveAlpha();
        if (!haveAlpha)
        {
            Debug.Log("已经没有alpha");
            return;
        }
        bool originIsReadAble = importer.isReadable;
        importer.isReadable = true;
#if UNITY_5_5_OR_NEWER
        importer.textureCompression = TextureImporterCompression.Uncompressed;
#else
        importer.textureFormat = TextureImporterFormat.ARGB32;
#endif
        AssetDatabase.ImportAsset(importer.assetPath);


        Color32[] pix = tex.GetPixels32();
        for (int i = 0; i < pix.Length; i++)
        {
            pix[i].a = 0;
        }

        Texture2D newTex = new Texture2D(tex.width, tex.height, TextureFormat.RGB24, false);
        newTex.SetPixels32(pix);

        var bytes = newTex.EncodeToPNG();
        File.WriteAllBytes(path, bytes);

        AssetDatabase.Refresh();
        importer.isReadable = originIsReadAble;
#if UNITY_5_5_OR_NEWER
        importer.textureCompression = originFormat;
#else
        importer.textureFormat = originFormat;
#endif
        AssetDatabase.ImportAsset(importer.assetPath);
    }
Exemplo n.º 11
0
        public override void Optimization(string param)
        {
            string[] patams = param.Split(',');
            if (textureImporter)
            {
                var format = textureImporter.DoesSourceTextureHaveAlpha()
                    ? (TextureImporterFormat)Enum.Parse(typeof(TextureImporterFormat), patams[1])
                    : (TextureImporterFormat)Enum.Parse(typeof(TextureImporterFormat), patams[2]);
                TextureImporterPlatformSettings platformSettings = textureImporter.GetPlatformTextureSettings(patams[0]);
                TextureImporterPlatformSettings settings         = new TextureImporterPlatformSettings
                {
                    allowsAlphaSplitting = platformSettings.allowsAlphaSplitting,
                    crunchedCompression  = platformSettings.crunchedCompression,
                    maxTextureSize       = platformSettings.maxTextureSize,
                    name            = platformSettings.name,
                    resizeAlgorithm = platformSettings.resizeAlgorithm,

                    textureCompression = TextureImporterCompression.Compressed,
                    compressionQuality = (int)UnityEditor.TextureCompressionQuality.Normal,
                    overridden         = true
                };
                settings.format            = format;
                settings.overridden        = true;
                textureImporter.isReadable = false;
                textureImporter.SetPlatformTextureSettings(settings);
                textureImporter.SaveAndReimport();
            }
        }
Exemplo n.º 12
0
        //private void CustomizeMaterial(ref Material material, DazMaterialPropertiesInfo info)
        //{
        //    material.SetColor("_BaseColor", info.BaseColor);
        //    material.SetFloat("_SurfaceType", info.Transparent ? 1 : 0); //0 == opaque, 1 == transparent

        //    Texture mainTexture = material.mainTexture;
        //    CustomizeTexture(ref mainTexture, info.Transparent);

        //    var normalMap = material.GetTexture("_NormalMap");
        //    if (!IsValidNormalMap(normalMap))
        //        material.SetTexture("_NormalMap", null);//nuke the invalid normal map, if its a mistake


        //    material.SetFloat("_Metallic", info.Metallic);
        //    material.SetFloat("_Smoothness", info.Smoothness);
        //    material.SetInt("_MaterialID", (int)info.MaterialType);
        //    material.SetFloat("_DoubleSidedEnable", info.DoubleSided ? 0 : 1);
        //}


        void CustomizeTexture(ref Texture texture, bool alphaIsTransparent)
        {
            if (texture != null)
            {
                var             texPath  = AssetDatabase.GetAssetPath(texture);
                TextureImporter importer = (TextureImporter)AssetImporter.GetAtPath(texPath);
                if (importer != null)
                {
                    if (alphaIsTransparent && importer.DoesSourceTextureHaveAlpha())
                    {
                        importer.alphaIsTransparency = true;
                    }

                    //todo twiddle other switches here, before the reimport happens only once
                    importer.SaveAndReimport();
                }
                else
                {
                    Debug.LogWarning("texture " + texture.name + " is not a project asset.");
                }
            }
            else
            {
                Debug.LogWarning("null texture");
            }
        }
Exemplo n.º 13
0
    //纹理导入器, pre-process
    void OnPreprocessTexture()
    {
        //获得importer实例
        TextureImporter tImporter = assetImporter as TextureImporter;

        //设置Read/Write Enabled开关,不勾选
        tImporter.isReadable = false;

        if (tImporter.assetPath.StartsWith("Assets/UITextures"))
        {
            //设置UI纹理Generate Mipmaps开关,不勾选
            tImporter.mipmapEnabled = false;
            //设置UI纹理WrapMode开关,Clamp
            tImporter.wrapMode = TextureWrapMode.Clamp;
        }

        //设置压缩格式
        TextureImporterPlatformSettings psAndroid = tImporter.GetPlatformTextureSettings("Android");
        TextureImporterPlatformSettings psIPhone  = tImporter.GetPlatformTextureSettings("iPhone");

        psAndroid.overridden = true;
        psIPhone.overridden  = true;
        if (tImporter.DoesSourceTextureHaveAlpha())
        {
            psAndroid.format = TextureImporterFormat.ETC2_RGBA8;
            psIPhone.format  = TextureImporterFormat.ASTC_RGBA_4x4;
        }
        else
        {
            psAndroid.format = TextureImporterFormat.ETC2_RGB4;
            psIPhone.format  = TextureImporterFormat.ASTC_RGB_4x4;
        }
        tImporter.SetPlatformTextureSettings(psAndroid);
        tImporter.SetPlatformTextureSettings(psIPhone);
    }
Exemplo n.º 14
0
        /// <summary>
        /// 设置纹理指定平台的格式,默认压缩
        /// </summary>
        /// <param name="importer">Importer.</param>
        /// <param name="platform">Platform.</param>
        private void SetTexturePlatformSetting(TextureImporter importer, string platform)
        {
            TextureImporterPlatformSettings platformSettings = importer.GetPlatformTextureSettings(platform);
            TextureImporterPlatformSettings settings         = new TextureImporterPlatformSettings
            {
                allowsAlphaSplitting = platformSettings.allowsAlphaSplitting,
                crunchedCompression  = platformSettings.crunchedCompression,
                maxTextureSize       = platformSettings.maxTextureSize,
                name            = platformSettings.name,
                resizeAlgorithm = platformSettings.resizeAlgorithm,

                textureCompression = TextureImporterCompression.Compressed,
                compressionQuality = (int)UnityEditor.TextureCompressionQuality.Normal,
                overridden         = true
            };

            bool isAndroid = platform == PLATFORM_ANDROID;

            if (importer.DoesSourceTextureHaveAlpha())
            {
                settings.format = isAndroid ? TextureImporterFormat.ETC2_RGBA8 : TextureImporterFormat.ASTC_RGBA_6x6;
            }
            else
            {
                settings.format = isAndroid ? TextureImporterFormat.ETC2_RGB4 : TextureImporterFormat.ASTC_RGB_6x6;
            }
            importer.SetPlatformTextureSettings(settings);
        }
Exemplo n.º 15
0
    static void SetPicFormat(string path)
    {
        Debug.Log(path);
        TextureImporter importer = AssetImporter.GetAtPath(path) as TextureImporter;

        if (importer.mipmapEnabled == true)
        {
            importer.mipmapEnabled = false;
        }

        //判断图片大小
        Texture2D texture     = AssetDatabase.LoadAssetAtPath <Texture2D>(path);
        int       textureSize = Mathf.Max(texture.height, texture.width);
        //Debug.Log(textureSize);
        int SizeType = FitSize(textureSize);

        //Android   iPhone
        if (importer.DoesSourceTextureHaveAlpha())
        {
            //ios版本
            importer.SetPlatformTextureSettings("iPhone", SizeType, TextureImporterFormat.ASTC_RGBA_6x6);
            //安卓版本
            //importer.SetPlatformTextureSettings("Android", SizeType, TextureImporterFormat.ETC2_RGBA8);
        }
        else
        {
            //ios版本
            importer.SetPlatformTextureSettings("iPhone", SizeType, TextureImporterFormat.ASTC_RGB_6x6);
            //安卓版本
            //importer.SetPlatformTextureSettings("Android", SizeType, TextureImporterFormat.ETC2_RGB4);
        }
    }
Exemplo n.º 16
0
        /// <summary>
        /// 生成一张alpha图
        /// </summary>
        public static Texture2D ConvertTexRtex(Texture2D src)
        {
            string          texPath      = AssetDatabase.GetAssetPath(src);
            int             index        = texPath.LastIndexOf('.');
            string          alphaTexPath = texPath.Substring(0, index) + "_A.png";
            int             size         = src.width > src.height ? src.width : src.height;
            TextureImporter texImporter  = AssetImporter.GetAtPath(texPath) as TextureImporter;

            if (!texImporter.DoesSourceTextureHaveAlpha())
            {
                return(null);
            }
            TextureImporterSettings setting = new TextureImporterSettings();

            setting.textureType   = TextureImporterType.Default;
            setting.aniso         = 0;
            setting.mipmapEnabled = size > 256;
            setting.npotScale     = TextureImporterNPOTScale.ToNearest;
            setting.wrapMode      = TextureWrapMode.Repeat;
            setting.filterMode    = FilterMode.Bilinear;
            setting.readable      = true;
            AssetDatabase.ImportAsset(texPath, ImportAssetOptions.ForceUpdate);
            Texture2D alphaTex = MakeAlphaRTex(alphaTexPath, size / scaleSize, src);

            SetPlatformSetting(texImporter, size);
            texImporter.isReadable = false;
            AssetDatabase.ImportAsset(texPath, ImportAssetOptions.ForceUpdate);
            return(alphaTex);
        }
Exemplo n.º 17
0
 public TextureImporterFormat GetFormatByAlphaMode(TextureImporterFormat format, TextureImporter tImporter)
 {
     if (AlphaMode == TextureAlphaMode.None ||
         tImporter.alphaSource == TextureImporterAlphaSource.None ||
         !tImporter.DoesSourceTextureHaveAlpha())
     {
         return(format);
     }
     else
     {
         if (format == TextureImporterFormat.ETC_RGB4 || format == TextureImporterFormat.ETC2_RGB4)
         {
             return(TextureImporterFormat.ETC2_RGBA8);
         }
         if (format == TextureImporterFormat.PVRTC_RGB4)
         {
             return(TextureImporterFormat.PVRTC_RGBA4);
         }
         if (format == TextureImporterFormat.RGB24)
         {
             return(TextureImporterFormat.RGBA32);
         }
         return(format);
     }
 }
Exemplo n.º 18
0
        protected virtual TextureImporterFormat GetPlatformCompressionType(TextureImporter textureImporter, BuildTargetGroup platform)
        {
            var format = TextureImporterFormat.RGBA32;

            var hasAlpha = textureImporter.DoesSourceTextureHaveAlpha();

            switch (platform)
            {
            case BuildTargetGroup.iOS:
                format = TextureImporterFormat.ASTC_4x4;
                break;

            case BuildTargetGroup.Android:
                format = TextureImporterFormat.ASTC_4x4;
                break;

            case BuildTargetGroup.Standalone:
            {
                if (textureImporter.textureType == TextureImporterType.NormalMap)
                {
                    format = TextureImporterFormat.DXT5;
                }
                else
                {
                    format = hasAlpha ? TextureImporterFormat.DXT5 : TextureImporterFormat.DXT1;
                }
            }
            break;
            }

            return(format);
        }
    // texture
    void OnPreprocessTexture()
    {
        if (assetPath.Contains("Textures"))
        {
            TextureImporter textureImporter = (TextureImporter)assetImporter;
            textureImporter.textureType = TextureImporterType.Default;
            textureImporter.isReadable  = false;

            TextureImporterPlatformSettings androidTexFMT = textureImporter.GetPlatformTextureSettings("Android");
            androidTexFMT.overridden = true;
            if (textureImporter.DoesSourceTextureHaveAlpha())
            {
                androidTexFMT.format = TextureImporterFormat.ETC2_RGBA8;
            }
            else
            {
                androidTexFMT.format = TextureImporterFormat.ETC2_RGB4;
            }
            textureImporter.SetPlatformTextureSettings(androidTexFMT);

            TextureImporterPlatformSettings iosTexFMT = textureImporter.GetPlatformTextureSettings("iPhone");
            iosTexFMT.overridden = true;
            if (textureImporter.DoesSourceTextureHaveAlpha())
            {
                iosTexFMT.format = TextureImporterFormat.ASTC_RGBA_4x4;
            }
            else
            {
                iosTexFMT.format = TextureImporterFormat.ASTC_RGB_4x4;
            }
            textureImporter.SetPlatformTextureSettings(iosTexFMT);

            TextureImporterPlatformSettings pcTexFMT = textureImporter.GetPlatformTextureSettings("Standalone");
            pcTexFMT.overridden = true;
            if (textureImporter.DoesSourceTextureHaveAlpha())
            {
                pcTexFMT.format = TextureImporterFormat.DXT5;
            }
            else
            {
                pcTexFMT.format = TextureImporterFormat.DXT1;
            }
            textureImporter.SetPlatformTextureSettings(pcTexFMT);
        }
    }
Exemplo n.º 20
0
    //是否有透明
    static bool IsHaveAlpha(TextureImporter ti)
    {
        if (ti.DoesSourceTextureHaveAlpha())
        {
            return(true);
        }

        return(false);
    }
Exemplo n.º 21
0
 private static void ImportSprite(TextureImporter importer)
 {
     importer.textureType         = TextureImporterType.Sprite;
     importer.spriteImportMode    = SpriteImportMode.Single;
     importer.textureShape        = TextureImporterShape.Texture2D;
     importer.wrapMode            = TextureWrapMode.Clamp;
     importer.mipmapEnabled       = false;
     importer.alphaIsTransparency = importer.DoesSourceTextureHaveAlpha();
 }
Exemplo n.º 22
0
    private static void CreateAlphaPNG()
    {
        Texture2D tex = Selection.activeObject as Texture2D;

        if (!tex)
        {
            EditorUtility.DisplayDialog("error", "请选择一张图片", "ok");
            return;
        }
        string path = AssetDatabase.GetAssetPath(Selection.activeObject);

        if (!path.EndsWith("png") && !path.EndsWith("PNG"))
        {
            EditorUtility.DisplayDialog("error", "图片不是png或PNG:" + path, "ok");
            return;
        }
        TextureImporter       importer     = AssetImporter.GetAtPath(path) as TextureImporter;
        TextureImporterFormat originFormat = importer.textureFormat;
        bool haveAlpha = importer.DoesSourceTextureHaveAlpha();

        if (!haveAlpha)
        {
            Debug.Log("已经没有alpha");
            return;
        }
        bool originIsReadAble = importer.isReadable;

        importer.isReadable    = true;
        importer.textureFormat = TextureImporterFormat.ARGB32;
        AssetDatabase.ImportAsset(importer.assetPath);


        Color32[] pix    = tex.GetPixels32();
        Color32[] newPix = new Color32[pix.Length];
        for (int i = 0; i < pix.Length; i++)
        {
            newPix[i] = new Color32(pix[i].a, 0, 0, 0);
        }


        Texture2D newTex = new Texture2D(tex.width, tex.height, TextureFormat.RGB24, false);

        newTex.SetPixels32(newPix);


        var bytes = newTex.EncodeToPNG();

        path += "-alpha.png";
        File.WriteAllBytes(path, bytes);


        AssetDatabase.Refresh();
        importer.isReadable    = originIsReadAble;
        importer.textureFormat = originFormat;
        AssetDatabase.ImportAsset(importer.assetPath);
    }
Exemplo n.º 23
0
    /// <summary>
    /// 更改导入图片的格式
    /// </summary>
    void OnPreprocessTexture()
    {
        if (IsAssetProcessed)
        {
            return;
        }
        IsAssetProcessed = true;
        TextureImporter textureImporter = (TextureImporter)assetImporter;

        if (textureImporter == null)
        {
            return;
        }
        textureImporter.textureType = TextureImporterType.Default;
        textureImporter.wrapMode    = TextureWrapMode.Clamp;
        textureImporter.isReadable  = true;

        int width  = 0;
        int height = 0;

        GetOriginalSize(textureImporter, out width, out height);
        if (!GetIsMultipleOf4(width) || !GetIsMultipleOf4(height))
        {
            //Debug.Log("4---" + assetPath);
        }
        bool IsPowerOfTwo = GetIsPowerOfTwo(width) && GetIsPowerOfTwo(height);

        if (!IsPowerOfTwo)
        {
            textureImporter.npotScale = TextureImporterNPOTScale.None;
        }

        textureImporter.mipmapEnabled  = false;
        textureImporter.maxTextureSize = GetMaxSize(Mathf.Max(width, height));

        if (assetPath.EndsWith(".jpg"))
        {
            textureImporter.textureFormat = TextureImporterFormat.ETC2_RGB4;
        }
        else if (assetPath.EndsWith(".png"))
        {
            if (!textureImporter.DoesSourceTextureHaveAlpha())
            {
                textureImporter.grayscaleToAlpha = true;
            }
            textureImporter.alphaIsTransparency = true;
            textureImporter.textureFormat       = TextureImporterFormat.ETC2_RGBA8;
            textureImporter.textureType         = TextureImporterType.Sprite;
            textureImporter.spriteImportMode    = SpriteImportMode.Single;
        }
        else
        {
            Debug.Log("图片格式---" + assetPath);
        }
    }
Exemplo n.º 24
0
    //public static void CreateConfigAssetBundle(string Extension, BuildTarget target)
    //{
    //    string[] ds = Directory.GetDirectories("Assets/Scenes/Config");
    //    foreach (string d in ds)
    //    {
    //        string[] files = Directory.GetFiles(d);
    //        foreach (string filePath in files)
    //        {
    //            if (Path.GetExtension(filePath) == ".csv")
    //            {
    //                CSV.CsvStreamReader csv = new CSV.CsvStreamReader(filePath);
    //                if (!csv.LoadCsvFile()) continue;
    //                string FileName = Path.GetFileNameWithoutExtension(filePath);


    //                FileStreamHolder holder = ScriptableObject.CreateInstance<FileStreamHolder>();
    //                holder.Init(csv.GetRowList());

    //                string time = Common.CurrTimeString;
    //                string p = "Assets" + Path.DirectorySeparatorChar + FileName + "_" + time + ".asset";
    //                AssetDatabase.CreateAsset(holder, p);
    //                UnityEngine.Object tmpObject = AssetDatabase.LoadAssetAtPath(p, typeof(FileStreamHolder));

    //                string dest = Common.GetWindowPath(filePath, Extension);
    //                Common.CreatePath(dest);

    //                tmpObject.name = FileName;
    //                if (BuildPipeline.BuildAssetBundle(tmpObject, null, dest, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, target))
    //                {
    //                    byte[] bytes = AssetsEncrypt.ReadFileToByte(dest);
    //                    AssetsEncrypt.EncryptBytes(bytes);
    //                    AssetsEncrypt.WriteByteToFile(bytes, dest);
    //                }

    //                AssetDatabase.DeleteAsset(p);
    //            }
    //        }
    //    }


    //}

    public static void MassSetTextureImporter(string path, BuildTarget target)
    {
        TextureImporterType   TextureType = TextureImporterType.Default;
        TextureImporterFormat tif;

        TextureImporter importer = AssetImporter.GetAtPath(path) as TextureImporter;

        if (importer == null)
        {
            return;
        }
        importer.mipmapEnabled = true;
        importer.npotScale     = TextureImporterNPOTScale.None;
        importer.textureType   = TextureType;
        importer.isReadable    = false;

        if (importer.grayscaleToAlpha)
        {
            tif = TextureImporterFormat.Alpha8;
        }
        else if (importer.DoesSourceTextureHaveAlpha())
        {
            tif = TextureImporterFormat.RGBA32;
        }
        else
        {
            switch (target)
            {
            case BuildTarget.Android:
            {
                tif = TextureImporterFormat.ETC_RGB4;
            }
            break;

            case BuildTarget.iOS:
            {
                tif = TextureImporterFormat.PVRTC_RGB4;
            }
            break;

            default:
            {
                tif = TextureImporterFormat.RGB24;
            }
            break;
            }
        }

        importer.textureFormat = tif;


        importer.maxTextureSize = 512;

        AssetDatabase.ImportAsset(path);
    }
Exemplo n.º 25
0
    };                                                                   //Icon的路径,用于动态图集
    #endregion

    #region 场景贴图

    #endregion

    void OnPreprocessTexture()
    {
        //获得importer实例
        TextureImporter tImporter = assetImporter as TextureImporter;

        TextureType textureType = GetTextureType(tImporter.assetPath);

        bool hasAlpha = false;

        if (tImporter.alphaSource != TextureImporterAlphaSource.None)
        {
            hasAlpha = tImporter.DoesSourceTextureHaveAlpha() || tImporter.alphaSource == TextureImporterAlphaSource.FromGrayScale;
        }


        if (textureType == TextureType.UICommon)
        {
            tImporter.textureType      = TextureImporterType.Sprite;
            tImporter.spriteImportMode = SpriteImportMode.Single;
            string atlasName = new DirectoryInfo(Path.GetDirectoryName(assetPath)).Name.ToLower();
            tImporter.spritePackingTag = atlasName;
            //设置Read/Write Enabled开关,不勾选
            tImporter.isReadable          = false;
            tImporter.alphaIsTransparency = hasAlpha;
            //设置UI纹理Generate Mipmaps开关,不勾选
            tImporter.mipmapEnabled    = false;
            tImporter.streamingMipmaps = false;
            //设置UI纹理WrapMode开关,Clamp
            tImporter.wrapMode = TextureWrapMode.Clamp;
        }
        else if (textureType == TextureType.UITexture || textureType == TextureType.UITextureNoAlpha || textureType == TextureType.UIIcon)
        {
            tImporter.textureType         = TextureImporterType.Default;
            tImporter.alphaIsTransparency = hasAlpha;
            if (textureType == TextureType.UIIcon)
            {
                tImporter.npotScale = TextureImporterNPOTScale.ToNearest;
            }
            tImporter.isReadable = false;
            //设置UI纹理Generate Mipmaps开关,不勾选
            tImporter.mipmapEnabled    = false;
            tImporter.streamingMipmaps = false;
        }

        var standardAndroidImporter = GetAndroidStandardImportSetting(tImporter, textureType, hasAlpha);

        tImporter.SetPlatformTextureSettings(standardAndroidImporter);
        var standardIOSImporter = GetIOSStandardImportSetting(tImporter, textureType, hasAlpha);

        tImporter.SetPlatformTextureSettings(standardIOSImporter);
        var standardWinImporter = GetWinStandardImportSetting(tImporter, textureType, hasAlpha);

        tImporter.SetPlatformTextureSettings(standardWinImporter);
    }
Exemplo n.º 26
0
    /// <summary>
    /// 优化纹理
    /// </summary>
    /// <returns></returns>
    public static bool OptimizeTexture()
    {
        List <string> mTexturePathList = new List <string>();
        DirectoryInfo rootDir          = new DirectoryInfo(Application.dataPath);

        foreach (string ext in mTextureExtensionArray)
        {
            FileInfo[] allFiles = rootDir.GetFiles(ext, SearchOption.AllDirectories);
            //             int filecount = allFiles.Length;
            //             int fileindex = 0;
            foreach (FileInfo fi in allFiles)
            {
                string fullName = fi.FullName.Replace('\\', '/');
                fullName = fullName.Remove(0, mAssetPathStartIndex);
                mTexturePathList.Add(fullName);
                //                 float percent = (float)fileindex / (float)filecount;
                //                 EditorUtility.DisplayProgressBar("收集Texture数据" + percent.ToString(" 0.00%").ToString(), fullName, percent);
                //                 fileindex++;
            }
        }

        int texcount = mTexturePathList.Count;

        for (int i = 0; i < texcount; i++)
        {
            string path = mTexturePathList[i];

            float percent = (float)i / (float)texcount;
            EditorUtility.DisplayProgressBar("处理Texture" + percent.ToString(" 0.00%").ToString(), path, percent);

            int MaxSize = 1024;
            TextureImporterFormat TexFormat;
            TextureImporter       textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
            textureImporter.GetPlatformTextureSettings("WP8", out MaxSize, out TexFormat);

            if (true /* && textureResourceUnitIt.mIsPowerOfTwo*/)
            {
                bool hasAlpha = textureImporter.DoesSourceTextureHaveAlpha();
                if (hasAlpha)
                {
                    textureImporter.SetPlatformTextureSettings("WP8", MaxSize, TextureImporterFormat.DXT5);
                }
                else
                {
                    textureImporter.SetPlatformTextureSettings("WP8", MaxSize, TextureImporterFormat.DXT1);
                }
                AssetDatabase.ImportAsset(path);
            }
        }

        GC.Collect();
        return(true);
    }
Exemplo n.º 27
0
        public void OnEnable()
        {
            texImporter = target as TextureImporter;
            texFormat.Clear();
            if (texImporter != null)
            {
                path         = texImporter.assetPath;
                cannotHotfix = path.StartsWith("Assets/Resources/StaticUI");
                isUITex      = (path.StartsWith("Assets/Resources/atlas/UI") || path.StartsWith("Assets/Resources/StaticUI")) && !path.EndsWith("_A.png");
                if (isUITex)
                {
                    GetTexFormat(IsAtlas(path), texImporter.userData, out srcFormat, out alphaSize);
                    for (int i = 0; i < targets.Length; ++i)
                    {
                        TextureImporter ti = targets[i] as TextureImporter;
                        if (ti != null)
                        {
                            needAlpha = ti.DoesSourceTextureHaveAlpha();
                            TexFormat tf = new TexFormat();
                            GetTexFormat(IsAtlas(ti.assetPath), ti.userData, out tf.srcFormat, out tf.alphaSize);
                            texFormat.Add(tf);
                            if (tf.srcFormat != srcFormat)
                            {
                                srcFormat = (ETextureCompress)(-1);
                            }
                            if (tf.alphaSize != alphaSize)
                            {
                                alphaSize = (ETextureSize)(-1);
                            }
                        }
                    }
                }
            }

            SceneView.onSceneGUIDelegate = TargetUpdate;
            Type t = null;

            foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                foreach (Type type in assembly.GetTypes())
                {
                    if (type.Name.ToLower().Contains("textureimporterinspector"))
                    {
                        t = type;
                        break;
                    }
                }
            }
            nativeEditor = Editor.CreateEditor(target, t);
        }
Exemplo n.º 28
0
    public void OnPreprocessTexture()
    {
        if (!assetPath.Contains("Res/UI"))
        {
            return;
        }

        TextureImporterType   type    = TextureImporterType.Sprite;
        TextureImporterFormat pc      = TextureImporterFormat.DXT5;
        TextureImporterFormat android = TextureImporterFormat.ETC2_RGBA8;
        TextureImporterFormat ios     = TextureImporterFormat.PVRTC_RGBA4;
        TextureImporter       ti      = AssetImporter.GetAtPath(assetPath) as TextureImporter;

        //if (ti.textureType != TextureImporterType.Default)
        //{
        //    return;
        //}


        if (assetPath.Contains("BigImage"))
        {
            if (ti.DoesSourceTextureHaveAlpha())
            {
                if (!assetPath.Contains("_a.png"))
                {
                    Debug.LogError(string.Format("{0}带alpha通道,但是并没有加上_a标签,请检查是否需要alpha通道", assetPath));
                }
                pc      = TextureImporterFormat.DXT5;
                android = TextureImporterFormat.ETC2_RGBA8;
                ios     = TextureImporterFormat.PVRTC_RGBA4;
            }
            else
            {
                pc      = TextureImporterFormat.DXT5;
                android = TextureImporterFormat.ETC2_RGB4;
                ios     = TextureImporterFormat.PVRTC_RGB4;
            }
        }

        if (assetPath.Contains("Dynamic") || assetPath.Contains("FrameAnim") || assetPath.Contains("Login"))
        {
            pc      = TextureImporterFormat.DXT5;
            android = TextureImporterFormat.ETC_RGB4;
            ios     = TextureImporterFormat.ASTC_RGBA_4x4;
        }

        string tag = GetTagName(assetPath);

        SetTextureImportFormat(ti, assetPath, type, 1024, false, pc, android, ios, tag);
    }
Exemplo n.º 29
0
 public static void SetDefaultSettings(PostprocessorAssetType assetType, TextureImporter importer)
 {
     importer.alphaIsTransparency = importer.DoesSourceTextureHaveAlpha();
     importer.sRGBTexture         = true;
     importer.isReadable          = false;
     importer.mipmapEnabled       = false;
     importer.streamingMipmaps    = false;
     importer.filterMode          = FilterMode.Bilinear;
     importer.wrapMode            = TextureWrapMode.Clamp;
     if (assetType == PostprocessorAssetType.Texture)
     {
         importer.npotScale = TextureImporterNPOTScale.ToNearest;
     }
 }
Exemplo n.º 30
0
        public static bool hasAlpha(Texture2D tex)
        {
            if (tex == null)
            {
                return(false);
            }
            TextureImporter ti = mset.AssetUtil.getTextureImporter(AssetDatabase.GetAssetPath(tex), "mset.Util.hasAlpha");

            if (ti)
            {
                return(ti.DoesSourceTextureHaveAlpha());
            }
            return(false);
        }