- Home /
AudioClip newClip.name is null in OnPostProcessAudio( AudioClip newClip), how to fix/get name?
Problem solved: I don't own the pro version of Unity3d and thus cannot access the asset pipeline via script, as per the License Comparison of the editor: http://unity3d.com/unity/licenses
I've created an editor script that creates a prefab for an AudioClip whenever it is imported (or reimported).
The problem I have is that in the OnPostProcessAudio() function call I need to access the name of the recently added/edited AudioClip but the name variable of the AudioClip is always null/empty.
Here's the code:
using UnityEngine;
using System.Collections;
using UnityEditor;
public class AudioPrefabGenerater : AssetPostprocessor
{
public void OnPostprocessAudio (AudioClip newClip)
{
//This gets the audio prefab directory
string audioPrefabDirPath = AudioManager.GetAudioPrefabDirFullPath();
//This creates the file name from the newClip's name and its extension (prefab)
string fileName = newClip.name+".prefab";
//This creates an empty prefab in the audioPrefab directory
Object emptyPrefab = PrefabUtility.CreateEmptyPrefab(audioPrefabDirPath+fileName);
//This creates a new GameObject with newClip's name
GameObject newAudioGo = new GameObject(newClip.name);
//Add the AudioSource component and set the clip to be newClip
newAudioGo.AddComponent<AudioSource>().clip = newClip;
PrefabUtility.ReplacePrefab(newAudioGo.gameObject, emptyPrefab, ReplacePrefabOptions.ConnectToPrefab);
Editor.DestroyImmediate(newAudioGo);
}
}
Answer by Paulius-Liekis · May 30, 2012 at 09:04 AM
I don't know why it's empty, but you can try using AssetPostprocessor.assetPath instead.
Thanks for the reply, but I've figured it out (I don't own Unity3d Pro and thus cannot access the asset pipeline via script).
Answer by radiantSil · Jan 15, 2015 at 01:38 PM
Use This:
void OnPostprocessAudio(AudioClip nullClip)
{
var audioClip = AssetDatabase.LoadAssetAtPath(assetPath, typeof(AudioClip)) as AudioClip;
//use audioClip instead of nullClip
}
Your answer
Follow this Question
Related Questions
Where does streamed audio clip stored and removed? 0 Answers
Microphone Position goes larger than predetermined number of samples 0 Answers
Extract audio clip from VideoPlayer for real-time processing 0 Answers
Microphone capture different channels 0 Answers
How to manage player and enemies shooting sounds at the same time? 1 Answer