【问题标题】:Switch Sound Button in Unity Javascript code在 Unity Javascript 代码中切换声音按钮
【发布时间】:2016-08-05 21:23:16
【问题描述】:

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

由于我是 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;
    }
}

【问题讨论】:

  • 我建议您添加更多与统一相关的标签(例如“unity3d、unity5 等”)来提问,因为统一脚本标签非常小,而且人们使用“javascript”和“audio”标签而不是需要特定的知识。
  • 感谢@IdeaMan 做到了。
  • 你能告诉我什么现在不适合你吗?您在程序中看到了什么行为?
  • @Joetjah 谢谢回复。基本上,当我单击声音时,它会将声音静音,这很好,但在声音按钮上放置一个圆圈以描绘关闭状态。我只想使用关闭声音按钮图像 - 请参阅 image
  • 由于您是 Unity 新手,我认为您应该尝试使用 Unity 的 UI 功能。它可以帮助您创建许多 UI 元素,包括按钮。有教程here.

标签: audio unity3d unityscript


【解决方案1】:

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

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;
    }
}

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

【讨论】:

  • 尝试过并收到错误:ERROR1 - SoundButton.js(1,8): BCE0043: Unexpected token: Sprite. ERROR2 - SoundButton.js(2,8): BCE0043: Unexpected token: Sprite. ERROR3 - SoundButton.js(3,9): BCE0043: Unexpected token: SpriteRenderer.
  • @Chelseawillrecover 我编辑了我的帖子。我用 C# 而不是 UnityScript,我的错。
  • 我在关注changes 后遇到了另一个问题。 SoundButton 和 SoundButtonOff 是每个按钮图像的文件名。错误:SoundButton.js(2,15):BCE0018:名称“SoundButtonOff”不表示有效类型(“未找到”)。您的意思是“System.Net.Mail.SmtpDeliveryMethod”吗?
  • @Chelseawillrecover 看来您不了解 UnityScript 的语法以及该语言的编码工作原理。我建议您阅读一些有关编码的基本教程。为什么它不适合你:Sprite 是 UnityScript 知道的标准类。当您键入var sprite1 : Sprite 时,您基本上是在告诉编译器创建一个名为 sprite1 的 Sprite 对象。当您将代码更改为var sprite1 : SoundButton 时,应该有一个名为 SoundButton 的对象类,以便 Unity 在您声明此类对象时知道您在说什么。
  • 是的,我在阅读了一些资料后能够对其进行排序,请参阅here。在测试中,我收到另一个错误:UnassignedReferenceException:SoundButton 的变量 spriteRenderer 尚未分配。您可能需要在检查器中分配 SoundButton 脚本的 spriteRenderer 变量。 UnityEngine.SpriteRenderer.set_sprite(UnityEngine.Sprite值)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-18
  • 2012-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多