private static void OnPostprocessAllAssets(
     string[] importedAssets,
     string[] deletedAssets,
     string[] movedAssets,
     string[] movedFromAssetPaths
     )
 {
     foreach (var v in movedAssets)
     {
         var ex = Path.GetExtension(v);
         if (TextureToolUtil.IsTexture(ex))
         //https://docs.unity3d.com/2019.1/Documentation/ScriptReference/AssetPostprocessor.OnPostprocessTexture.html
         //如果在Postprocess事件里修改ImportSetting的话需要等待下次imported才会生效
         //所以在这儿import一下
         //todo 重命名的时候也会走这里,目前没什么好办法解决
         {
             AssetDatabase.ImportAsset(v);
         }
     }
 }
        private void CheckPreset(TexImporterSetting.Settings setting)
        {
            switch (setting.PresetSetting)
            {
            case PresetSettings.Texture_Scene:
                setting.TextureType         = TextureImporterType.Default;
                setting.WrapMode            = TextureWrapMode.Repeat;
                setting.MipMapEnable        = true;
                setting.TextureShape        = TextureImporterShape.Texture2D;
                setting.AlphaIsTransparency = false;
                break;

            case PresetSettings.Texture_UI:
                setting.TextureType         = TextureImporterType.Default;
                setting.WrapMode            = TextureWrapMode.Clamp;
                setting.MipMapEnable        = false;
                setting.TextureShape        = TextureImporterShape.Texture2D;
                setting.AlphaIsTransparency = true;
                break;

            case PresetSettings.Sprite_UI:
                setting.TextureType         = TextureImporterType.Sprite;
                setting.WrapMode            = TextureWrapMode.Clamp;
                setting.MipMapEnable        = false;
                setting.TextureShape        = TextureImporterShape.Texture2D;
                setting.AlphaIsTransparency = true;
                break;
            }


            if (setting.AlphaMode != AlphaMode.Auto)
            {
                var hasAlpha = setting.AlphaMode == AlphaMode.Alpha;
                setting.OverrideAndroidFormat = TextureToolUtil.GetFormat(RuntimePlatform.Android, hasAlpha);
                setting.OverrideIOSFormat     = TextureToolUtil.GetFormat(RuntimePlatform.IPhonePlayer, hasAlpha);
                setting.OverridePCFormat      = TextureToolUtil.GetFormat(RuntimePlatform.WindowsPlayer, hasAlpha);
            }
        }
Exemplo n.º 3
0
        private void checkPreset(TexFormatController control)
        {
            switch (control.preset)
            {
            case PresetSettings.Texture_Scene:
                control.textureType         = TextureImporterType.Default;
                control.wrapMode            = TextureWrapMode.Repeat;
                control.mipMapEnable        = true;
                control.textureShape        = TextureImporterShape.Texture2D;
                control.alphaIsTransparency = false;
                break;

            case PresetSettings.Texture_UI:
                control.textureType         = TextureImporterType.Default;
                control.wrapMode            = TextureWrapMode.Clamp;
                control.mipMapEnable        = false;
                control.textureShape        = TextureImporterShape.Texture2D;
                control.alphaIsTransparency = true;
                break;

            case PresetSettings.Sprite_UI:
                control.textureType         = TextureImporterType.Sprite;
                control.wrapMode            = TextureWrapMode.Clamp;
                control.mipMapEnable        = false;
                control.textureShape        = TextureImporterShape.Texture2D;
                control.alphaIsTransparency = true;
                break;
            }

            if (control.alphaMode != AlphaMode.Auto)
            {
                var hasAlpha = control.alphaMode == AlphaMode.Alpha;
                control.overrideAndroidFormat = TextureToolUtil.GetFormat(RuntimePlatform.Android, hasAlpha);
                control.overrideIOSFormat     = TextureToolUtil.GetFormat(RuntimePlatform.IPhonePlayer, hasAlpha);
                control.overridePCFormat      = TextureToolUtil.GetFormat(RuntimePlatform.WindowsPlayer, hasAlpha);
            }
        }
        private static void ImportTexture(string assetPath)
        {
            var importer = AssetImporter.GetAtPath(assetPath) as TextureImporter;

            var(importImporterSetting, settingDir) = GetImporterSetting(assetPath);
            if (!importImporterSetting || !importer)
            {
                return;
            }

            var setting = GetSettings(importImporterSetting, assetPath);

            if (setting == null)
            {
                return;
            }

            importer.textureType = setting.TextureType;
            TrySetPackingTag(
                importer,
                setting.AtlasMode,
                setting.PackingTag,
                assetPath,
                settingDir
                );
            if (importer.textureType == TextureImporterType.Sprite)
            {
                if (importer.spriteImportMode != SpriteImportMode.Single &&
                    importer.spriteImportMode != SpriteImportMode.Multiple)
                {
                    importer.spriteImportMode = SpriteImportMode.Single;
                }
            }

            importer.npotScale           = setting.NPOTScale;
            importer.alphaIsTransparency = setting.AlphaIsTransparency;
            importer.mipmapEnabled       = setting.MipMapEnable;
            importer.isReadable          = setting.ReadWriteEnable;
            importer.sRGBTexture         = setting.SRGB;
            importer.wrapMode            = setting.WrapMode;
            importer.filterMode          = setting.FilterMode;
            importer.textureShape        = setting.TextureShape;

            TextureImporterFormat iosFormat = 0, androidFormat = 0, pcFormat = 0;

            if (setting.AlphaMode != AlphaMode.Auto)
            {
                iosFormat     = setting.OverrideIOSFormat;
                androidFormat = setting.OverrideAndroidFormat;
                pcFormat      = setting.OverridePCFormat;
                importer.alphaIsTransparency =
                    setting.AlphaIsTransparency &&
                    setting.AlphaMode == AlphaMode.Alpha;
                if ((int)iosFormat == 51)
                {
                    iosFormat = TextureImporterFormat.ASTC_8x8;
                }
                else if ((int)iosFormat == 56)
                {
                    iosFormat = TextureImporterFormat.ASTC_6x6;
                }
                else if ((int)iosFormat == 50)
                {
                    iosFormat = TextureImporterFormat.ASTC_6x6;
                }
                else if ((int)iosFormat == 54)
                {
                    iosFormat = TextureImporterFormat.ASTC_4x4;
                }
                else if ((int)iosFormat == 57)
                {
                    iosFormat = TextureImporterFormat.ASTC_8x8;
                }
            }
            else
            {
                var hasAlpha = importer.DoesSourceTextureHaveAlpha();
                importer.alphaIsTransparency = setting.AlphaIsTransparency && hasAlpha;
                iosFormat     = TextureToolUtil.GetFormat(RuntimePlatform.IPhonePlayer, hasAlpha);
                androidFormat = TextureToolUtil.GetFormat(RuntimePlatform.Android, hasAlpha);
                pcFormat      = TextureToolUtil.GetFormat(RuntimePlatform.WindowsPlayer, hasAlpha);
            }

            var iphoneSettings =
                GetPlatformSettings(
                    "iPhone",
                    iosFormat,
                    setting.MaxSize,
                    setting.Quality
                    );

            var androidSettings =
                GetPlatformSettings(
                    "Android",
                    androidFormat,
                    setting.MaxSize,
                    setting.Quality
                    );

            var pcSettings =
                GetPlatformSettings(
                    "Standalone",
                    pcFormat,
                    setting.MaxSize,
                    setting.Quality
                    );

            if (setting.MaxSize == 0)
            {
                iphoneSettings.maxTextureSize =
                    importer.GetPlatformTextureSettings("iPhone").maxTextureSize;
                androidSettings.maxTextureSize =
                    importer.GetPlatformTextureSettings("Android").maxTextureSize;
                pcSettings.maxTextureSize =
                    importer.GetPlatformTextureSettings("Standalone").maxTextureSize;
            }

            importer.SetPlatformTextureSettings(iphoneSettings);
            importer.SetPlatformTextureSettings(androidSettings);
            importer.SetPlatformTextureSettings(pcSettings);
        }