Legacy Documentation: Version 5.6 (Go to current version)
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.


Suggest a change


Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.


Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.



public var oggVorbis: Object;
public Object oggVorbis;


Load an Ogg Vorbis file into the audio clip.

If the stream has not been downloaded completely, null will be returned. Use isDone or yield to see if the data is available.

See Also: AudioClip, AudioSource.

var path =

function Start () { // Start downloading var download = new WWW (path); // Wait for download to finish yield download; // Create ogg vorbis file var clip : AudioClip = download.oggVorbis; // Play it if (clip != null) { audio.clip = clip; audio.Play(); } else { // Handle error Debug.Log("Ogg vorbis download failed. (Incorrect link?)"); } }

@script RequireComponent (AudioSource)
// A generic stream music player.
// Downloads music successively, then starts playing them randomly.
var downloadPath : String[] = [
private var downloadedClips : AudioClip[];
private var playedSongs = new Array ();

function Start () { downloadedClips = new AudioClip[downloadPath.Length];

DownloadAll ();

PlaySongs (); }

function DownloadAll () { for (var i = 0;i < downloadPath.length; i++) { var path = downloadPath[i]; var download = new WWW (path); yield download; downloadedClips[i] = download.oggVorbis;

if (downloadedClips[i] == null) Debug.Log("Failed audio download " + path); } }

function PickRandomSong () : AudioClip { var possibleSongs = Array ();

// Build a list of all songs that completed their download for (var i = 0; i < downloadedClips.length; i++) { if (downloadedClips[i] != null) possibleSongs.Add(i); }

// No songs downloaded yet if (possibleSongs.length == 0) return null;

// We played all songs, pick from any song now if (possibleSongs.length == playedSongs.length) playedSongs.Clear();

// Remove songs that were played already from the list of songs for (i = 0; i < playedSongs.length; i++) possibleSongs.Remove (playedSongs[i]);

// Pick a random song if (possibleSongs.length != 0) { var index : int= possibleSongs[Random.Range(0, possibleSongs.length)]; playedSongs.Add(index); return downloadedClips[index]; } else return null; }

function PlaySongs () { while (true) { var clip : AudioClip = PickRandomSong (); if (clip != null) { audio.clip = clip; audio.Play(); yield WaitForSeconds(clip.length); } yield; } }

@script RequireComponent(AudioSource)
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。