Unity editor resource import processing function OnPreprocessAudio: in-depth analysis and practical cases

Usage of Unity editor resource import processing function OnPreprocessAudio

https://github.com/AlianBlank/download.unity.com

Click on the cover to jump to the download page

Introduction

In Unity, resource import is a very important link, which determines the use and effect of resources in the project. Unity provides a series of resource import processing functions, one of which is OnPreprocessAudio. This function can customize audio resources during the resource import process, such as modifying import settings, modifying the properties of audio files, etc. This article will introduce OnPreprocessAudiothe usage and sample code of the function to help developers better understand and use this function.

function signature

void OnPreprocessAudio()

function description

OnPreprocessAudiois a callback function that is called during resource import. When Unity imports an audio resource, if there is OnPreprocessAudioa function in the script, Unity will call this function during the import process, and developers can customize the audio resource in this function.

Instructions

To use OnPreprocessAudiothe function, you need to follow these steps:

  1. Create a AssetPostprocessorscript class that inherits from and implements OnPreprocessAudiothe function.
using UnityEditor;
using UnityEngine;

public class MyAudioPostprocessor : AssetPostprocessor
{
    void OnPreprocessAudio()
    {
        // 在这里编写自定义处理逻辑
    }
}
  1. Place the script class in the project's Editorfolder to make sure it only runs in editor mode.

  2. OnPreprocessAudioWrite your custom processing logic in the function . You can modify the import settings of audio resources, modify the properties of audio files, etc. as needed.

void OnPreprocessAudio()
{
    // 获取导入的音频资源
    AudioImporter audioImporter = assetImporter as AudioImporter;

    // 修改导入设置
    audioImporter.loadInBackground = true;
    audioImporter.preloadAudioData = true;

    // 修改音频文件的属性
    AudioImporterSampleSettings sampleSettings = audioImporter.defaultSampleSettings;
    sampleSettings.loadType = AudioClipLoadType.Streaming;
    audioImporter.defaultSampleSettings = sampleSettings;
}

In the sample code above, we have modified the import settings of the audio resource so that it loads in the background and preloads the audio data. At the same time, we also modified the properties of the audio file and set its loading type to Streaming.

  1. Save the script and return to the Unity editor. When you import an audio asset, Unity will automatically call OnPreprocessAudiothe function and execute your custom processing logic. Of course, I can help you completely rewrite an article, including setting different sampling rates, audio length loading methods, compression formats, and audio quality according to different platforms. Here is the full rewritten article:

sample code

using UnityEditor;
using UnityEngine;

public class MyAudioPostprocessor : AssetPostprocessor
{
    void OnPreprocessAudio()
    {
        // 获取导入的音频资源
        AudioImporter audioImporter = assetImporter as AudioImporter;

        // 根据平台进行不同设置
        #if UNITY_ANDROID
            SetAndroidSettings(audioImporter);
        #elif UNITY_IOS
            SetiOSSettings(audioImporter);
        #else
            SetDefaultSettings(audioImporter);
        #endif
    }

    // Android 平台设置
    private void SetAndroidSettings(AudioImporter audioImporter)
    {
        AudioImporterSampleSettings sampleSettings = audioImporter.defaultSampleSettings;
        sampleSettings.loadType = GetAndroidLoadType(); // 根据音频长度获取合适的加载方式
        sampleSettings.compressionFormat = AudioCompressionFormat.Vorbis;
        sampleSettings.quality = GetAndroidQuality(); // 根据音频长度获取合理的码率
        sampleSettings.sampleRateSetting = AudioSampleRateSetting.OverrideSampleRate;
        sampleSettings.sampleRateOverride = (uint) GetAndroidSampleRate(); // 根据音频长度获取合理的采样率
        audioImporter.defaultSampleSettings = sampleSettings;
    }

    // iOS 平台设置
    private void SetiOSSettings(AudioImporter audioImporter)
    {
        AudioImporterSampleSettings sampleSettings = audioImporter.defaultSampleSettings;
        sampleSettings.loadType = GetiOSLoadType(); // 根据音频长度获取合适的加载方式
        sampleSettings.compressionFormat = AudioCompressionFormat.AAC;
        sampleSettings.quality = GetiOSQuality(); // 根据音频长度获取合理的码率
        sampleSettings.sampleRateSetting = AudioSampleRateSetting.OverrideSampleRate;
        sampleSettings.sampleRateOverride = (uint) GetiOSSampleRate(); // 根据音频长度获取合理的采样率
        audioImporter.defaultSampleSettings = sampleSettings;
    }

    // 默认设置
    private void SetDefaultSettings(AudioImporter audioImporter)
    {
        AudioImporterSampleSettings sampleSettings = audioImporter.defaultSampleSettings;
        sampleSettings.loadType = GetDefaultLoadType(); // 根据音频长度获取合适的加载方式
        sampleSettings.compressionFormat = AudioCompressionFormat.MP3;
        sampleSettings.quality = GetDefaultQuality(); // 根据音频长度获取合理的码率
        sampleSettings.sampleRateSetting = AudioSampleRateSetting.OverrideSampleRate;
        sampleSettings.sampleRateOverride = (uint) GetDefaultSampleRate(); // 根据音频长度获取合理的采样率
        audioImporter.defaultSampleSettings = sampleSettings;
    }

    // 根据音频长度获取 Android 平台的合适加载方式
    private AudioClipLoadType GetAndroidLoadType()
    {
        // 在这里编写根据音频长度获取合适加载方式的逻辑
        // 示例代码:
        float audioLength = GetAudioLength(); // 获取音频长度
        if (audioLength > 10f)
        {
            return AudioClipLoadType.Streaming;
        }
        else
        {
            return AudioClipLoadType.DecompressOnLoad;
        }
    }

    // 根据音频长度获取 iOS 平台的合适加载方式
    private AudioClipLoadType GetiOSLoadType()
    {
        // 在这里编写根据音频长度获取合适加载方式的逻辑
        // 示例代码:
        float audioLength = GetAudioLength(); // 获取音频长度
        if (audioLength > 10f)
        {
            return AudioClipLoadType.Streaming;
        }
        else
        {
            return AudioClipLoadType.DecompressOnLoad;
        }
    }

    // 根据音频长度获取默认平台的合适加载方式
    private AudioClipLoadType GetDefaultLoadType()
    {
        // 在这里编写根据音频长度获取合适加载方式的逻辑
        // 示例代码:
        float audioLength = GetAudioLength(); // 获取音频长度
        if (audioLength > 10f)
        {
            return AudioClipLoadType.Streaming;
        }
        else
        {
            return AudioClipLoadType.DecompressOnLoad;
        }
    }

    // 根据音频长度获取 Android 平台的合理码率
    private float GetAndroidQuality()
    {
        // 在这里编写根据音频长度获取合理码率的逻辑
        // 示例代码:
        float audioLength = GetAudioLength(); // 获取音频长度
        if (audioLength < 30f)
        {
            return 0.3f;
        }
        else
        {
            return 0.2f;
        }
    }

    // 根据音频长度获取 iOS 平台的合理码率
    private float GetiOSQuality()
    {
        // 在这里编写根据音频长度获取合理码率的逻辑
        // 示例代码:
        float audioLength = GetAudioLength(); // 获取音频长度
        if (audioLength < 30f)
        {
            return 0.5f;
        }
        else
        {
            return 0.4f;
        }
    }

    // 根据音频长度获取默认平台的合理码率
    private float GetDefaultQuality()
    {
        // 在这里编写根据音频长度获取合理码率的逻辑
        // 示例代码:
        float audioLength = GetAudioLength(); // 获取音频长度
        if (audioLength < 30f)
        {
            return 0.6f;
        }
        else
        {
            return 0.5f;
        }
    }

    // 根据音频长度获取 Android 平台的合理采样率
    private int GetAndroidSampleRate()
    {
        // 在这里编写根据音频长度获取合理采样率的逻辑
        // 示例代码:
        float audioLength = GetAudioLength(); // 获取音频长度
        if (audioLength < 30f)
        {
            return 44100;
        }
        else
        {
            return 22050;
        }
    }

    // 根据音频长度获取 iOS 平台的合理采样率
    private int GetiOSSampleRate()
    {
        // 在这里编写根据音频长度获取合理采样率的逻辑
        // 示例代码:
        float audioLength = GetAudioLength(); // 获取音频长度
        if (audioLength < 30f)
        {
            return 48000;
        }
        else
        {
            return 24000;
        }
    }

    // 根据音频长度获取默认平台的合理采样率
    private int GetDefaultSampleRate()
    {
        // 在这里编写根据音频长度获取合理采样率的逻辑
        // 示例代码:
        float audioLength = GetAudioLength(); // 获取音频长度
        if (audioLength < 30f)
        {
            return 44100;
        }
        else
        {
            return 22050;
        }
    }

    // 获取音频长度
    private float GetAudioLength()
    {
        // 在这里编写获取音频长度的逻辑
        // 示例代码:
        return 60f; // 假设音频长度为 60 秒
    }
}

In the above code, we set different import settings according to different platforms to ensure the best effect and performance of audio resources on different platforms. We obtain the appropriate loading method, reasonable bit rate and reasonable sampling rate according to the audio length, and apply these settings to the default sampling settings of the audio importer.

Please note that the judgment logic in the sample code is a simple example, you can modify and expand it according to your actual needs.

Through the above sample codes, you can customize audio resources according to your own needs to achieve better import effects and user experience.

Summarize

OnPreprocessAudioThe function is a resource import processing function provided by Unity, which can be customized during the audio resource import process. By writing custom logic, developers can modify the import settings, modify the properties of audio files, etc. to meet the needs of the project. Hope this article can help you better understand and use OnPreprocessAudiothe function.


My sincerest apologies for possible errors in my technical articles. Efforts have been made to ensure that information is accurate and reliable, but due to the ever-changing technological landscape, mistakes are inevitable. If you find a bug or have any questions, please contact me. I will do my best to correct mistakes and provide more accurate information.

My sincerest apologies again, I will review and update the article with more care to provide a better reading experience and accurate technical information.

Thank you for your understanding and support.

Guess you like

Origin blog.csdn.net/alianhome/article/details/132156896