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.
\$\begingroup\$
\$\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– Zibelas2023-07-17 12:00:28 +00:00Commented 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– Bogdan2023-07-17 15:47:32 +00:00Commented 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– Atomiklan2023-10-20 05:40:20 +00:00Commented Oct 20, 2023 at 5:40
Add a comment
|
2 Answers
\$\begingroup\$
\$\endgroup\$
3
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));
}
}
}
-
\$\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\$2023-07-18 17:46:41 +00:00Commented 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– Zibelas2023-07-18 20:03:11 +00:00Commented 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– Atomiklan2023-10-20 05:39:52 +00:00Commented Oct 20, 2023 at 5:39
\$\begingroup\$
\$\endgroup\$
4
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++;
}
}
-
\$\begingroup\$ Except you can't use scripts \$\endgroup\$Zibelas– Zibelas2023-07-17 16:14:46 +00:00Commented 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\$2023-07-17 16:18:13 +00:00Commented 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– Bogdan2023-07-17 16:50:04 +00:00Commented 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– Atomiklan2023-10-20 05:39:44 +00:00Commented Oct 20, 2023 at 5:39
