Exemple #1
0
        private static void SetTextureImporterPlatformSetting(
            TexturePreprocessorConfig config,
            TextureImporter textureImporter,
            Texture texture,
            string textureName,
            int textureSize,
            TextureImporterFormat format
            )
        {
            Debug.Log($"Setting: {textureSize} | Format: {format} | {textureName}", texture);

            config.PlatformsRegexList.ForEach(platformRegexString =>
            {
                textureImporter.SetPlatformTextureSettings(new TextureImporterPlatformSettings
                {
                    overridden           = true,
                    name                 = platformRegexString,
                    maxTextureSize       = textureSize,
                    format               = format,
                    compressionQuality   = (int)config.TextureCompressionQuality,
                    allowsAlphaSplitting = false
                });
            });

            // Be sure to set the platform override for the current platform string, in case the current platform was
            // NOT a perfect match to one of the platform regex strings.
            textureImporter.SetPlatformTextureSettings(new TextureImporterPlatformSettings
            {
                overridden           = true,
                name                 = EditorUserBuildSettings.activeBuildTarget.ToString(),
                maxTextureSize       = textureSize,
                format               = format,
                compressionQuality   = (int)config.TextureCompressionQuality,
                allowsAlphaSplitting = false
            });

            textureImporter.npotScale = config.NPOTScale;
        }
Exemple #2
0
        /// <summary>
        /// https://docs.unity3d.com/ScriptReference/AssetPostprocessor.OnPreprocessTexture.html
        /// </summary>
        private void OnPreprocessTexture()
        {
            var textureImporter = (TextureImporter)assetImporter;

            var assetPath    = textureImporter.assetPath;
            var textureName  = AssetPreprocessorUtils.GetAssetNameFromPath(textureImporter.assetPath);
            var texture      = AssetDatabase.LoadAssetAtPath <Texture2D>(assetPath);
            var platformName = EditorUserBuildSettings.activeBuildTarget.ToString();

            var configs = AssetPreprocessorUtils.GetScriptableObjectsOfType <TexturePreprocessorConfig>();

            if (configs.Count == 0)
            {
                Debug.Log($"Could not find a {nameof(TexturePreprocessorConfig)} in project.");

                return;
            }

            configs = configs
                      .Where(conf => conf.ShouldUseConfigForAssetImporter(assetImporter))
                      .ToList();

            configs.Sort((config1, config2) => config1.ConfigSortOrder.CompareTo(config2.ConfigSortOrder));

            TexturePreprocessorConfig config = null;

            for (var i = 0; i < configs.Count; i++)
            {
                var configToTest = configs[i];

                if (!AssetPreprocessorUtils.DoesRegexStringListMatchString(configToTest.PlatformsRegexList, platformName))
                {
                    continue;
                }

                // Found matching config.
                config = configToTest;

                break;
            }

            // If could not find a matching config, don't process the texture.
            if (config == null)
            {
                return;
            }

            var currentPlatform         = EditorUserBuildSettings.activeBuildTarget.ToString();
            var currentPlatformSettings = textureImporter.GetPlatformTextureSettings(currentPlatform);

            var hasAlpha          = textureImporter.DoesSourceTextureHaveAlpha();
            var nativeTextureSize = GetOriginalTextureSize(textureImporter);
            var nativeSize        = Mathf.NextPowerOfTwo(Mathf.Max(nativeTextureSize.width, nativeTextureSize.height));
            var currentFormat     = currentPlatformSettings.format.ToString();

            // Handle when native size for texture is too small. Happens for baked light maps and reflection maps.
            // Need to reimport asset once to get correct native texture size.
            if (nativeSize <= 4)
            {
                AssetDatabase.Refresh();
                return;
            }

            Debug.Log($"Processing: {textureName} | Native size: {nativeSize} | Current format: {currentFormat}", texture);
            Debug.Log($"Using: {config.name}", config);

            // If already contains correct texture format, skip adjusting import settings.
            var matchingSkipRegex     = config.SkipIfCurrentTextureFormatMatchesRegexList.Find(regexString => new Regex(regexString).IsMatch(currentFormat));
            var alreadyContainsFormat = matchingSkipRegex != null;

            if (!config.ForcePreprocess && alreadyContainsFormat)
            {
                Debug.Log($"Skipping preprocess. Current format matching skip regex: '{matchingSkipRegex}'", texture);
                return;
            }

            if (config.EnableReadWrite && !textureImporter.isReadable)
            {
                Debug.Log("Enabling Read/Write.", texture);
                textureImporter.isReadable = true;
            }

            var maxTextureSize      = config.MaxTextureSize;
            var multipliedNativeRes = Mathf.RoundToInt(nativeSize * config.NativeTextureSizeMultiplier);
            var textureSize         = Mathf.Min(multipliedNativeRes, maxTextureSize);

            var format = hasAlpha ? config.RGBAFormat : config.RGBFormat;

            if (config.ForceLinear)
            {
                textureImporter.sRGBTexture = false;
            }

            if (config.ForceFilterMode)
            {
                textureImporter.anisoLevel = config.AnisoLevel;
                textureImporter.filterMode = config.FilterMode;
            }

            textureImporter.mipmapEnabled    = config.GenerateMipMaps;
            textureImporter.streamingMipmaps = config.EnableMipMapStreaming;

            SetTextureImporterPlatformSetting(config, textureImporter, texture, textureName, textureSize, format);
        }