0
\$\begingroup\$

I am limited in the number of audio sources I can add. Is it possible to use a single audio source and then rotate between a collection of audio files. This is for an avatar for VRChat so scripting is not an option as scripts will be removed at build time. It would need to be a feature built into Unity that maybe I am just missing.

\$\endgroup\$
3
  • 2
    \$\begingroup\$ If you are not allowed custom scipts, how are you going to determinate which audio file should be played? \$\endgroup\$
    – Zibelas
    Jul 17, 2023 at 12:00
  • \$\begingroup\$ about the feature built into Unity - you have an editor scripting create the one you need. \$\endgroup\$
    – Bogdan
    Jul 17, 2023 at 15:47
  • \$\begingroup\$ Correct, cannot use scripts. I recognize that it just may not be possible, but I have been wrong in the past and there have been secret tricky loop holes to get things to work in VRC in the past. \$\endgroup\$
    – Atomiklan
    Oct 20, 2023 at 5:40

2 Answers 2

0
\$\begingroup\$

Put this script into the folder named Editor and you will get list with audioClips in your inspector. Set slider to count of elements and empty slots appear.

using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;


[CustomEditor(typeof(AudioSource))]
public class AudioSourceEditor : Editor
{
    public List<AudioClip> audioClips;

    [Obsolete]
    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();
        EditorGUILayout.LabelField("Audio Clips:");
        EditorGUILayout.BeginHorizontal();
        {
            int count = EditorGUILayout.IntSlider("Count", audioClips.Count, 0, 10);
            if (count != audioClips.Count)
            {
                audioClips.Clear();
                for (int i = 0; i < count; i++)
                {
                    audioClips.Add(null);
                }
            }
        }
        EditorGUILayout.EndHorizontal();
        foreach (AudioClip audioClip in audioClips)
        {
            EditorGUILayout.ObjectField(audioClip, typeof(AudioClip));
        }
    }
}



enter image description here

\$\endgroup\$
3
  • \$\begingroup\$ This code adds a list in the inspector where you can assign multiple audio clips, but those clips are not serialized as part of the AudioSource, and do not survive into a built game or exported asset. \$\endgroup\$
    – DMGregory
    Jul 18, 2023 at 17:46
  • \$\begingroup\$ OP wrote they are using VRChat. They have the requirements listed here: creators.vrchat.com/avatars/whitelisted-avatar-components Since AudioSourceEditor is not AudioSource, it will be stripped and the audioclip is missing in the end \$\endgroup\$
    – Zibelas
    Jul 18, 2023 at 20:03
  • \$\begingroup\$ Correct, cannot use scripts. I recognize that it just may not be possible, but I have been wrong in the past and there have been secret tricky loop holes to get things to work in VRC in the past. \$\endgroup\$
    – Atomiklan
    Oct 20, 2023 at 5:39
-1
\$\begingroup\$

Yes you can add to your script collection of AudioClips and then play them via audiosource.

[SerializeField] List<AudioClip> clips;
IEnumerator Start()
{
 AudioSource audio = GetComponent<AudioSource>();
 for(int i = 0;iclips.Count();i++)
    {
    audio.clip = clips[i];
    audio.Play();
    yield return new WaitForSeconds(audio.clip.length);
    i++;
    }
}
\$\endgroup\$
4
  • \$\begingroup\$ Except you can't use scripts \$\endgroup\$
    – Zibelas
    Jul 17, 2023 at 16:14
  • \$\begingroup\$ Please ensure you've read and understood the whole question, including "scripting is not an option as scripts will be removed at build time" \$\endgroup\$
    – DMGregory
    Jul 17, 2023 at 16:18
  • \$\begingroup\$ I understud AudioSource it is a script. It for some reason you cant use Monobehaviour directtly then you can modify it as EditorExtension script and add same logic I described. \$\endgroup\$
    – Bogdan
    Jul 17, 2023 at 16:50
  • \$\begingroup\$ Correct, cannot use scripts. I recognize that it just may not be possible, but I have been wrong in the past and there have been secret tricky loop holes to get things to work in VRC in the past. \$\endgroup\$
    – Atomiklan
    Oct 20, 2023 at 5:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .