SoFunction
Updated on 2025-03-06

Unity editor resource import processing function OnPreprocessAudio usage example

Unity editor resource import processing function OnPreprocessAudio usage

Click to jump to the code page

In Unity, resource import is a very important link, which determines how and effect resources are used in the project. Unity provides a series of resource import processing functions, one of which isOnPreprocessAudio. This function can customize audio resources during resource import, such as modifying import settings, modifying the properties of audio files, etc.

This article will introduceOnPreprocessAudioFunction usage and sample code help developers better understand and use this function.

Function signature

void OnPreprocessAudio()

Function description

OnPreprocessAudioIt is a callback function called during resource import. When Unity imports an audio resource, if it exists in the scriptOnPreprocessAudioFunction, Unity will call this function during the import process, and developers can customize audio resources in this function.

How to use

To useOnPreprocessAudioFunctions, you need to follow the following steps:

  • Create an inherited fromAssetPostprocessorscript class and implementOnPreprocessAudioFunction.
using UnityEditor;
using UnityEngine;
public class MyAudioPostprocessor : AssetPostprocessor
{
    void OnPreprocessAudio()
    {
        // Write custom processing logic here    }
}
  • Place the script class in the projectEditorUnder the folder, make sure it runs only in editor mode.
  • existOnPreprocessAudioWrite 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()
{
    // Get imported audio resources    AudioImporter audioImporter = assetImporter as AudioImporter;
    // Modify import settings     = true;
     = true;
    // Modify the properties of the audio file    AudioImporterSampleSettings sampleSettings = ;
     = ;
     = sampleSettings;
}

In the above example code, we modified the import settings of the audio resource to load and preload the audio data in the background. At the same time, we have also modified the properties of the audio file and set its load type to Streaming.

Save the script and return to the Unity editor. When you import an audio resource, Unity will be called automaticallyOnPreprocessAudiofunction and execute your custom processing logic.

Of course, I can help you rewrite an article completely, including setting different sampling rates, audio length loading methods, compression formats, and audio quality according to different platforms. Here is a complete rewritten article:

Sample code

using UnityEditor;
using UnityEngine;
public class MyAudioPostprocessor : AssetPostprocessor
{
    void OnPreprocessAudio()
    {
        // Get imported audio resources        AudioImporter audioImporter = assetImporter as AudioImporter;
        // Different settings are made according to the platform        #if UNITY_ANDROID
            SetAndroidSettings(audioImporter);
        #elif UNITY_IOS
            SetiOSSettings(audioImporter);
        #else
            SetDefaultSettings(audioImporter);
        #endif
    }
    // Android platform settings    private void SetAndroidSettings(AudioImporter audioImporter)
    {
        AudioImporterSampleSettings sampleSettings = ;
         = GetAndroidLoadType(); // Get the appropriate loading method according to the audio length         = ;
         = GetAndroidQuality(); // Get a reasonable code rate based on the audio length         = ;
         = (uint) GetAndroidSampleRate(); // Get a reasonable sampling rate based on the audio length         = sampleSettings;
    }
    // iOS platform settings    private void SetiOSSettings(AudioImporter audioImporter)
    {
        AudioImporterSampleSettings sampleSettings = ;
         = GetiOSLoadType(); // Get the appropriate loading method according to the audio length         = ;
         = GetiOSQuality(); // Get a reasonable code rate based on the audio length         = ;
         = (uint) GetiOSSampleRate(); // Get a reasonable sampling rate based on the audio length         = sampleSettings;
    }
    // Default settings    private void SetDefaultSettings(AudioImporter audioImporter)
    {
        AudioImporterSampleSettings sampleSettings = ;
         = GetDefaultLoadType(); // Get the appropriate loading method according to the audio length         = AudioCompressionFormat.MP3;
         = GetDefaultQuality(); // Get a reasonable code rate based on the audio length         = ;
         = (uint) GetDefaultSampleRate(); // Get a reasonable sampling rate based on the audio length         = sampleSettings;
    }
    // Obtain the appropriate loading method for Android platform based on the audio length    private AudioClipLoadType GetAndroidLoadType()
    {
        // Write the logic here to get the appropriate loading method based on the audio length        // Sample code:        float audioLength = GetAudioLength(); // Get audio length        if (audioLength > 10f)
        {
            return ;
        }
        else
        {
            return ;
        }
    }
    // Get the appropriate loading method for iOS platform based on audio length    private AudioClipLoadType GetiOSLoadType()
    {
        // Write the logic here to get the appropriate loading method based on the audio length        // Sample code:        float audioLength = GetAudioLength(); // Get audio length        if (audioLength > 10f)
        {
            return ;
        }
        else
        {
            return ;
        }
    }
    // Get the appropriate loading method for the default platform based on the audio length    private AudioClipLoadType GetDefaultLoadType()
    {
        // Write the logic here to get the appropriate loading method based on the audio length        // Sample code:        float audioLength = GetAudioLength(); // Get audio length        if (audioLength > 10f)
        {
            return ;
        }
        else
        {
            return ;
        }
    }
    // Obtain the reasonable code rate of the Android platform based on the audio length    private float GetAndroidQuality()
    {
        // Write the logic here to obtain a reasonable bit rate based on the audio length        // Sample code:        float audioLength = GetAudioLength(); // Get audio length        if (audioLength < 30f)
        {
            return 0.3f;
        }
        else
        {
            return 0.2f;
        }
    }
    // Obtain the reasonable code rate of iOS platform based on the audio length    private float GetiOSQuality()
    {
        // Write the logic here to obtain a reasonable bit rate based on the audio length        // Sample code:        float audioLength = GetAudioLength(); // Get audio length        if (audioLength < 30f)
        {
            return 0.5f;
        }
        else
        {
            return 0.4f;
        }
    }
    // Get the reasonable code rate of the default platform based on the audio length    private float GetDefaultQuality()
    {
        // Write the logic here to obtain a reasonable bit rate based on the audio length        // Sample code:        float audioLength = GetAudioLength(); // Get audio length        if (audioLength < 30f)
        {
            return 0.6f;
        }
        else
        {
            return 0.5f;
        }
    }
    // Obtain the reasonable sampling rate of the Android platform based on the audio length    private int GetAndroidSampleRate()
    {
        // Write the logic here to obtain reasonable sampling rates based on audio length        // Sample code:        float audioLength = GetAudioLength(); // Get audio length        if (audioLength < 30f)
        {
            return 44100;
        }
        else
        {
            return 22050;
        }
    }
    // Obtain the reasonable sampling rate of iOS platform based on the audio length    private int GetiOSSampleRate()
    {
        // Write the logic here to obtain reasonable sampling rates based on audio length        // Sample code:        float audioLength = GetAudioLength(); // Get audio length        if (audioLength < 30f)
        {
            return 48000;
        }
        else
        {
            return 24000;
        }
    }
    // Get the reasonable sampling rate of the default platform based on the audio length    private int GetDefaultSampleRate()
    {
        // Write the logic here to obtain reasonable sampling rates based on audio length        // Sample code:        float audioLength = GetAudioLength(); // Get audio length        if (audioLength < 30f)
        {
            return 44100;
        }
        else
        {
            return 22050;
        }
    }
    // Get audio length    private float GetAudioLength()
    {
        // Write the logic to get the audio length here        // Sample code:        return 60f; // Assume that the audio length is 60 seconds    }
}

In the above code, we set different import settings according to different platforms to ensure the best results and performance of audio resources on different platforms. We obtain the appropriate loading method, reasonable bit rate, and reasonable sampling rate based on 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 that you can modify and extend according to actual needs.

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

Summarize

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

I sincerely apologize to you for possible errors in my technical articles. I strive to ensure accurate and reliable information is provided, but errors are inevitable due to constant changes in the technical field. If you find an error or have any questions, please contact me. I will do my best to correct errors and provide more accurate information.

Again, I sincerely apologize to you and I will review and update the article more carefully to provide a better reading experience and accurate technical information.

The above is the detailed content of the Unity editor resource import processing function OnPreprocessAudio usage. For more information about the usage of Unity editor resource import processing function, please pay attention to my other related articles!