Unity3d 如何从脚本在unity中播放音频剪辑?

Unity3d 如何从脚本在unity中播放音频剪辑?,unity3d,audio-source,audioclip,Unity3d,Audio Source,Audioclip,所以我尝试用唱歌的方法来播放我在上面制作的一段音频剪辑。我知道我需要一个音频源,我只是不知道它如何适合音频剪辑。我目前没有将音频资源分配给该脚本分配给的对象,因此可能会产生影响。谢谢你的帮助 using System.Collections; using System.Collections.Generic; using UnityEngine; public class Piano : MonoBehaviour { private List<AudioClip> notes =

所以我尝试用唱歌的方法来播放我在上面制作的一段音频剪辑。我知道我需要一个音频源,我只是不知道它如何适合音频剪辑。我目前没有将音频资源分配给该脚本分配给的对象,因此可能会产生影响。谢谢你的帮助

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

public class Piano : MonoBehaviour {
private List<AudioClip> notes = new List<AudioClip>();

public string voice; 

public AudioClip ash1, a1, b1, csh1, c1, dsh1, d1, e1, f1, fsh1, g1, gsh1;

// Use this for initialization
void Start () {
    //octave1
    notes.Add(a1); notes.Add(b1); notes.Add(ash1); notes.Add(c1); notes.Add(csh1);notes.Add(d1); notes.Add(dsh1);
    notes.Add(e1); notes.Add(f1); notes.Add(g1); notes.Add(gsh1);

    WarmUp(voice);
}
//consider adding countertenor, true alto (i am using mezzo soprano), and baritone.
void WarmUp(string vp)
{
    //high and low end of voice parts 
    // 30 = c4 for example
    int high;
    int low;

    ArrayList range = new ArrayList();
    //c4 to c6 
    if (vp == "Soprano")
    {
        high = 0; //this is just a range to make the code shorter
        low = 7;

        for(int i = low; i < high; i++)
        {
            range.Add(notes[i]);
        }

        Sing(range);
    }
}

/**
 * @Param: range. the arraylist of range of notes. 
 * shift the pitch UP one half step if the up arrow key is pressed
 * shift the pitch down one half step if the down arrow key is pressed
 * shift the pitch up a third (4 half steps) if the left arrow key is pressed
 * shift the pitch up a fifth (7 half steps) if the right arrow key is pressed
 */
void Sing(ArrayList range)
{
    //how to play one of the audioclips in range here
}

// Update is called once per frame
void Update () {

}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共课钢琴:独奏{
私有列表注释=新列表();
公共弦乐;
公共音频剪辑ash1、a1、b1、csh1、c1、dsh1、d1、e1、f1、fsh1、g1、gsh1;
//用于初始化
无效开始(){
//八度音阶1
注释添加(a1);注释添加(b1);注释添加(ash1);注释添加(c1);注释添加(csh1);注释添加(d1);注释添加(dsh1);
notes.Add(e1);notes.Add(f1);notes.Add(g1);notes.Add(gsh1);
热身(声音);
}
//考虑加入反男高音、真正的中音(我用的是女中音)和男中音。
无效预热(字符串vp)
{
//语音部分的高端和低端
//例如,30=c4
int高;
int低;
ArrayList范围=新的ArrayList();
//c4至c6
如果(vp=“女高音”)
{
high=0;//这只是一个使代码更短的范围
低=7;
对于(int i=低;i<高;i++)
{
范围。添加(注释[i]);
}
歌唱(范围);
}
}
/**
*@Param:range。音符范围的数组列表。
*如果按下向上箭头键,将俯仰上移半步
*如果按下向下箭头键,将俯仰向下移动半步
*如果按下左箭头键,将俯仰向上移动三分之一(4个半步)
*如果按下向右箭头键,将俯仰向上移动五分之一(7个半步)
*/
空单(ArrayList范围)
{
//如何在此处播放范围内的音频剪辑之一
}
//每帧调用一次更新
无效更新(){
}
}

如果将
AudioSource
组件连接到
GameObject
,则可以为该组件设置
AudioSource
变量,然后调用setter函数
clip(AudioClip x)
来设置源应播放的剪辑。此时,您可以调用
Play()
并等待音频片段的长度


代码应该不会太难理解,但如果您还有任何问题,请毫不犹豫地提问。祝你好运

获取AudioSource和AudioClip的变量,并将音频源游戏对象分配给inspector上的AudioSource和clip,并将其分配给inspector上的audio aClip,在哪里播放声音,只需将其写入AudioSource.PlayOneShot(一个剪辑)。

AudioSource
组件附加到游戏对象

歌唱中

yourAudioSource.clip = (AudioClip)range[Random.Range(0, range.Count)];
yourAudioSource.Play()

你也可以考虑将<代码> ARAYLIST范围改为<代码>列表范围< /C>或类似,以避免Case

我认为你正在寻找:或者可能,但是,就像TopRamen所说的,你将需要一个音频源。然后简单地创建AudioSource类型的变量,使用GetComponent填充该变量,并简单地调用variableName.PlayOneShot()或variableName.Play()。