在Unity Javascript代码中切换声音按钮

时间:2016-04-14 11:01:38

标签: audio unity3d unityscript

我试图找出如何使用“声音关闭”和“声音开启”图像来控制游戏声音。下面的代码显示了一个声音按钮,但当你点击它时,它会使声音静音,但在按钮上添加一个阴影圆圈。

我想要一些指南或如何让它从声音切换到声音关闭图像按钮,因为我是Unity新手。

图像文件名:SoundButton.psd和WhiteCircle.psd

var whiteCircle : GameObject;
var numberOfTouch : int = 0;
private var a : float = 1;
function Start() {
    if (PlayerPrefs.GetInt("SoundBoolean") == 0) {
        //check whether the sound is included, if turned on, then play sound.
        numberOfTouch = 0;
        whiteCircle.GetComponent(SpriteRenderer).enabled = false;
    }
    if (PlayerPrefs.GetInt("SoundBoolean") == 1) {
        //check whether the sound is included, if turned off, then turn off the sound.
        numberOfTouch = 1;
        whiteCircle.GetComponent(SpriteRenderer).enabled = true;
    }
}
function OnMouseDown () {
    if (a <= 0) {
        if (numberOfTouch == 0) {
            //completely turn off the music.
            a = 1;
            numberOfTouch = 1;
            gameObject.GetComponent.<AudioSource>().Play();
            whiteCircle.GetComponent(SpriteRenderer).enabled = true;
            PlayerPrefs.SetInt("SoundBoolean", 1);
            PlayerPrefs.Save();
        }
    }
    if (a <= 0) {
        if (numberOfTouch == 1) {
            //a fully turn on the music.
            a = 1;
            numberOfTouch = 0;
            whiteCircle.GetComponent(SpriteRenderer).enabled = false;
            PlayerPrefs.SetInt("SoundBoolean", 0);
            PlayerPrefs.Save();
        }
    }
}
function Update() {
    if (a >= 0) {
        a -= 0.1;
    }
}

1 个答案:

答案 0 :(得分:0)

正如我所理解的那样,您正在寻找能够改变您拥有的对象上的精灵的东西。我没有测试我的代码或任何东西,但也许它可以帮助您解决问题。

var sprite1 : Sprite; // Drag your first sprite here
var sprite2 : Sprite; // Drag your second sprite here
var spriteRenderer : SpriteRenderer = whiteCircle.GetComponent(SpriteRenderer); 

var whiteCircle : GameObject;
var numberOfTouch : int = 0;
private var a : float = 1;
function Start() {
    if (PlayerPrefs.GetInt("SoundBoolean") == 0) {
        //check whether the sound is included, if turned on, then play sound.
        numberOfTouch = 0;
        spriteRenderer.sprite = sprite2;
    }
    if (PlayerPrefs.GetInt("SoundBoolean") == 1) {
        //check whether the sound is included, if turned off, then turn off the sound.
        numberOfTouch = 1;
        spriteRenderer.sprite = sprite1;
    }
}
function OnMouseDown () {
    if (a <= 0) {
        if (numberOfTouch == 0) {
            //completely turn off the music.
            a = 1;
            numberOfTouch = 1;
            gameObject.GetComponent.<AudioSource>().Play();
            spriteRenderer.sprite = sprite1;
            PlayerPrefs.SetInt("SoundBoolean", 1);
            PlayerPrefs.Save();
        }
    }
    if (a <= 0) {
        if (numberOfTouch == 1) {
            //a fully turn on the music.
            a = 1;
            numberOfTouch = 0;
            spriteRenderer.sprite = sprite2;
            PlayerPrefs.SetInt("SoundBoolean", 0);
            PlayerPrefs.Save();
        }
    }
}
function Update() {
    if (a >= 0) {
        a -= 0.1;
    }
}

基本上,将两个精灵点添加到可以拖动图像文件的位置,然后替换某个spriterenderer上的精灵(而不是打开和关闭它)。