C# 控制音乐播放的统一按钮(更改代码)

C# 控制音乐播放的统一按钮(更改代码),c#,unity3d,unity3d-2dtools,C#,Unity3d,Unity3d 2dtools,我想用一个按钮创建一个播放音乐的按钮,但我不明白如何才能得到这个按钮并使用它执行功能。除了使音乐停止和开始,我还想更改按钮图像以指示音乐是否已启用。这是我的代码: using UnityEngine; using UnityEngine.UI; using System.Collections; public class SoundControl : MonoBehaviour { // Use this for initialization private Button note; void

我想用一个按钮创建一个播放音乐的按钮,但我不明白如何才能得到这个按钮并使用它执行功能。除了使音乐停止和开始,我还想更改按钮图像以指示音乐是否已启用。这是我的代码:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class SoundControl : MonoBehaviour {
// Use this for initialization
private Button note;
void Start () {
    PlayerPrefs.SetInt ("Music", 1);
    note = GetComponent<Button> ();

}

// Update is called once per frame
public Sprite onnote;
public Sprite offnote;

void Update () {
    if (PlayerPrefs.GetInt ("Music") == 1) {
        note.image.overrideSprite = onnote;
        Debug.Log("Button Music equals 1 UPdated");
    }
    if (PlayerPrefs.GetInt ("Music") == 0) {
        note.image.overrideSprite = offnote;
        Debug.Log("Button Music equals 0 UPdated");
    }
}
public void MakeSound()
{
    Debug.Log("Button Music pressed");

    if (PlayerPrefs.GetInt ("Music") == 1) {
        PlayerPrefs.SetInt ("Music", 0);
        Debug.Log("Button Music equals 0");
    }
    if (PlayerPrefs.GetInt ("Music") == 0) {
        PlayerPrefs.SetInt ("Music", 1);
        Debug.Log("Button Music equals 1");
    }
}
使用UnityEngine;
使用UnityEngine.UI;
使用系统集合;
公共级声音控制:单一行为{
//用于初始化
私人按钮说明;
无效开始(){
PlayerPrefs.SetInt(“音乐”,1);
注意=GetComponent();
}
//每帧调用一次更新
公众雪碧笔记;
公众雪碧副刊;
无效更新(){
如果(PlayerPrefs.GetInt(“音乐”)==1){
note.image.overrideSprite=onnote;
Log(“按钮音乐等于1更新”);
}
如果(PlayerPrefs.GetInt(“音乐”)==0){
note.image.overrideSprite=offnote;
Log(“按钮音乐等于0更新”);
}
}
公共空间
{
Debug.Log(“按下按钮音乐”);
如果(PlayerPrefs.GetInt(“音乐”)==1){
PlayerPrefs.SetInt(“音乐”,0);
Log(“按钮音乐等于0”);
}
如果(PlayerPrefs.GetInt(“音乐”)==0){
PlayerPrefs.SetInt(“音乐”,1);
Log(“按钮音乐等于1”);
}
}
}

所以我把这个脚本放在按钮上。 请注意,我已经更改了代码,但现在控制台显示:

以下是我更正代码的方法:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class SoundControl : MonoBehaviour {

// Use this for initialization
private Button note;
void Start () {
    PlayerPrefs.SetInt ("Music", 1);
    note = GetComponent<Button> ();

}

// Update is called once per frame
public Sprite onnote;
public Sprite offnote;
void Update () {
    if (PlayerPrefs.GetInt ("Music") == 1) {
        note.image.overrideSprite = onnote;
        Debug.Log("Button Music equals 1 UPdated");
    }
    if (PlayerPrefs.GetInt ("Music") == 0) {
        note.image.overrideSprite = offnote;
        Debug.Log("Button Music equals 0 UPdated");
    }
}
public void MakeSound()
{
    switch(PlayerPrefs.GetInt("Music"))
    {
    case 0:
        PlayerPrefs.SetInt ("Music", 1);
        Debug.Log("Button Music equals 1");
        break;
    case 1:
        PlayerPrefs.SetInt ("Music", 0);
        Debug.Log("Button Music equals 0");
        break;
    }
}
}
使用UnityEngine;
使用UnityEngine.UI;
使用系统集合;
公共级声音控制:单一行为{
//用于初始化
私人按钮说明;
无效开始(){
PlayerPrefs.SetInt(“音乐”,1);
注意=GetComponent();
}
//每帧调用一次更新
公众雪碧笔记;
公众雪碧副刊;
无效更新(){
如果(PlayerPrefs.GetInt(“音乐”)==1){
note.image.overrideSprite=onnote;
Log(“按钮音乐等于1更新”);
}
如果(PlayerPrefs.GetInt(“音乐”)==0){
note.image.overrideSprite=offnote;
Log(“按钮音乐等于0更新”);
}
}
公共空间
{
开关(PlayerPrefs.GetInt(“音乐”))
{
案例0:
PlayerPrefs.SetInt(“音乐”,1);
Log(“按钮音乐等于1”);
打破
案例1:
PlayerPrefs.SetInt(“音乐”,0);
Log(“按钮音乐等于0”);
打破
}
}
}
当我使用if时,如果我不能从循环中跳出:)
因此,我使用开关在需要时断开。

制作一个按钮,单击后,让它运行
MakeSound()
…这不是我想要的,我希望当单击按钮时,按钮将图片更改为灰色音符,音乐将停止,当我再次单击时,音符将恢复原始状态,声音将播放,我在更新时的灰线中写下了我需要的。解决了!!!!,通过使用switch,我解决了当我使用if()时,我可以中断并且它不会重复自身