播放长音频源/音频片段 [英] playing a long audiosource/audioclip

查看:89
本文介绍了播放长音频源/音频片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试播放一个音频剪辑,太大了,无法使用PlayClipAtPoint(),因此我需要稍后将其停止.

I am trying to play an audio clip, too big to use PlayClipAtPoint() and I will need to stop it later.

public class GridPower: MonoBehaviour {

    private AudioSource audiosource;
    public AudioClip poweroff;
    int k;

    void Start () 
        {
        audiosource= GetComponent<AudioSource>();
        k = 1; 
        }

    void Update () 
        {
        if (PlayLevel1.percentageLoadProductionint < 100 && StateManager.activeState is PlayState && Time.timeSinceLevelLoad > 1 && k== 1) 
            {
            audiosource.clip = poweroff;
            audiosource.Play ();
            k = 2;
            }
         }
}

我遵循了一些文档,但是团结一致告诉我,脚本所在的游戏对象没有附加AudioSource.

I followed some documentation but unity tells me that there is no AudioSource attached to the game object where the script is located.

你能帮忙吗?

推荐答案

这是完全错误的.删除所有提及"AudioClip"的内容-您永远不要在代码中使用.

this is totally wrong. delete any and all mentions of "AudioClip" - you never, ever use it in code.

(1)制作一个空的游戏对象(称其为测试"). (2)在其上放​​一个AudioSource-任何您喜欢的音频. (3)在您的脚本中,有一个公共检查器变量以在测试中拾取AudioSource. (4)只需播放该音频源.老实说,就是这么简单.说您可能有20首不同的歌曲. 只有20个不同的游戏对象,例如测试" .真的就是这么简单.

(1) make an empty game object (call it say "test"). (2) put an AudioSource on it - whatever audio you like. (3) in your script, have a public inspector variable to pick up the AudioSource on test. (4) just Play that audio source. honestly it's just that simple. say you have maybe 20 different songs. just have 20 different game objects like "test". It is really just that simple.

假设您在一个项目中有500种音效.您仅拥有500个游戏对象(例如示例中的测试"),每个对象上都有不同的声音.

Say you have 500 sound effects in a project. you simply have 500 game objects (like "test" in the example) each one with the different sound on it.

(当然,只有一个空的游戏对象,将它们全部放在下面",以保持其整洁.)

(of course, just have one empty game object, to sit them all "under", to keep it tidy.)

注意-Unity中的一项很棒的功能是,从资产中选择并拖动说100种声音效果(或其他音频)到场景中:请注意,它知道"可以为您将每个效果放置在一个游戏对象上.这是Unity5中的一大新增功能.

Note - an awesome feature in Unity is, select and drag say 100 sound effects (or whatever audio) from assets in to the scene: note that it "knows" to put each one on a game object for you. this was a huge addition in Unity5.

就这么简单.

这篇关于播放长音频源/音频片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆