为什么在Unity中仅使声音静音是如此困难? [英] Why is it SO Hard to Just Mute a Sound in Unity?

查看:73
本文介绍了为什么在Unity中仅使声音静音是如此困难?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我整天都在寻找解决此问题的方法,但我根本找不到.使用Unity 3D中的JavaScript,我有一个脚本,当播放器在X轴上的速度达到特定点时,我想在其中播放声音,如果不在该点,则声音将被静音.而且我相信我拥有正确的结构,只是说要静音的代码行是行不通的.我尝试了各种不同的组合,但每个组合都会出现错误.

I have spent all day looking for a solution to this problem, and I simply can't find one. Using JavaScript in Unity 3D, I have a script where I want to play a sound when the player's velocity on the X axis reaches a certain point, and if it's not at that point, then the sound will be muted. And I believe I have all the structure right, it's just the line of code that says to mute the audio that won't work. I've tried all kinds of different combinations, and I get an error for each one.

脚本如下:

#pragma strict

var playing = false;
var audioSource = GetComponent.<AudioSource>();

function Update () {
    if (transform.GetComponent.<Rigidbody>().velocity.x <= 2.5 && 
transform.GetComponent.<Rigidbody>().velocity.x >= -2.5)
    {
        Mute();
    } else {
        Unmute();
    }
}

function Mute () {
    audioSource.mute = true;
}

function Unmute () {
    audioSource.mute = false;
    Sound();
}

function Sound () {
    if (transform.GetComponent.<Rigidbody>().velocity.x >= 2.5 && playing == 
false)
    {
        playing = true;
        GetComponent.<AudioSource>().Play();
        yield WaitForSeconds(2);
        playing = false;
    }
        if (transform.GetComponent.<Rigidbody>().velocity.x <= -2.5 && 
playing == false)
    {
        playing = true;
        GetComponent.<AudioSource>().Play();
        yield WaitForSeconds(2);
        playing = false;
    }
}

我遇到了各种不同的错误,但我似乎得到最多的错误是:"UnityException:不允许从MonoBehaviour构造函数(或实例字段初始化程序)中调用GetComponentFastPath,请在Awake或相反,从游戏对象球"上的MonoBehaviour的"motioncheck"调用."我不确定这意味着什么,因为我对JavaScript还是有点不了解.

I've gotten all kinds of different errors, but the one I seem to be getting the most says "UnityException: GetComponentFastPath is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour 'motioncheck' on game object 'Ball'." I'm not sure what this means, since I'm still kinda a nub at JavaScript.

我觉得只是静音就不难了.我将假定答案很简单,而且我真的很傻.这就是通常发生的情况,大声笑.

I feel like it shouldn't be this hard to just mute a sound. I'm going to assume that the answer to this is really simple and that I'm just really dumb. That's what usually seems to happen, lol.

与此同时,我将继续在互联网上横冲直撞,以寻找该问题的答案.

In the mean time, I'm going to continue my rampage across the internet in search for answers to this problem.

推荐答案

您的静音代码很好.

"UnityException:不允许从中调用GetComponentFastPath一个MonoBehaviour构造函数(或实例字段初始化程序),将其调用在唤醒"或开始"中.从MonoBehaviour的"motioncheck"调用游戏对象球"."我不确定这是什么意思,因为我仍然有点像JavaScript.

"UnityException: GetComponentFastPath is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour 'motioncheck' on game object 'Ball'." I'm not sure what this means, since I'm still kinda a nub at JavaScript.

看到这个:

var audioSource = GetComponent.<AudioSource>();

那是Unity API,您必须从函数内部调用它们的函数. Awake Start 函数适用于初始化组件变量.

That's a Unity API and you have to call their functions from inside a function. The Awake or Start function is appropriate for initializing component variables.

var audioSource : AudioSource;
function Start() 
{
    audioSource = GetComponent.<AudioSource>();
}


请注意,Unityscript/Javascript现在已终止.他们不再为此更新文档,并且您无法再通过编辑器创建新脚本.它仍然可以像现在一样工作,但是编译器将很快被删除.请学习并开始使用C#,然后再完全删除其支持.


Note that Unityscript/Javascript is now discontinued. They no longer update the doc on this and you cannot create new scripts from the Editor anymore. It still works as for now but the compiler will be removed soon. Please learn and start using C# before its support is totally removed.

这篇关于为什么在Unity中仅使声音静音是如此困难?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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