Unity/C#:打开/关闭音乐GUI按钮
几乎90%的时间里,我的问题已经在这里解决了。但今天是我第一次问 我正在开发一个小型flappy bird克隆,这是一个编程挑战。我以前从未使用过C#或Unity,所以 我有一个开/关按钮,我想“切换”。到目前为止,我有这个。除了再次显示“musicOn”纹理和再次播放音乐外,它还能工作 这里怎么了?先谢谢你Unity/C#:打开/关闭音乐GUI按钮,c#,unity3d,C#,Unity3d,几乎90%的时间里,我的问题已经在这里解决了。但今天是我第一次问 我正在开发一个小型flappy bird克隆,这是一个编程挑战。我以前从未使用过C#或Unity,所以 我有一个开/关按钮,我想“切换”。到目前为止,我有这个。除了再次显示“musicOn”纹理和再次播放音乐外,它还能工作 这里怎么了?先谢谢你 void OnGUI(){ toggleMusicButton(); if(GUI.Button(rect,"", new GUIStyle())){
void OnGUI(){
toggleMusicButton();
if(GUI.Button(rect,"", new GUIStyle())){
music.Pause();
musicBool = false;
}
if(GUI.Button(rect,"", new GUIStyle())){
music.Play();
musicBool = true;
}
}
void toggleMusicButton(){
if(musicBool){
GUI.DrawTexture(rect, musicOn);
} else {
GUI.DrawTexture(rect, musicOff);
}
}
我没有时间进行测试,但我相信这是因为您正在使用OnGUI之外的另一种方法进行GUI调用。我认为ONGUI价格昂贵,所以越少越好。
private Texture musicTexture;
void OnGUI(){
GUI.DrawTexture(rect, musicTexture);
if(GUI.Button(rect,"", new GUIStyle())){
if (musicBool) {
music.Pause();
musicBool = false;
musicTexture = musicOff
}
else {
music.Play();
musicBool = true;
musicTexture = musicOn
}
}
}
如果您使用DrawTexture只是为了更改按钮的外观,请执行以下操作
创建2个GUI样式,并为按钮设置正确的纹理
public GUIStyle musicOff; //assign a GUIStyle with the correct button image for off
private GUIStyle musicOn; //assign a GUIStyle with the correct button image for on
private GUIStyle musicGUIStyle; //holds GUIStyle being displayed. Assign this a default
void OnGUI(){
if(GUI.Button(rect,"", musicGUIStyle)){
if (musicBool) {
music.Pause();
musicBool = false;
musicGUIStyle = musicOff;
}
else {
music.Play();
musicBool = true;
musicGUIStyle = musicOn;
}
}
}
我没有时间进行测试,但我相信这是因为您正在使用OnGUI之外的另一种方法进行GUI调用。我认为ONGUI价格昂贵,所以越少越好。
private Texture musicTexture;
void OnGUI(){
GUI.DrawTexture(rect, musicTexture);
if(GUI.Button(rect,"", new GUIStyle())){
if (musicBool) {
music.Pause();
musicBool = false;
musicTexture = musicOff
}
else {
music.Play();
musicBool = true;
musicTexture = musicOn
}
}
}
如果您使用DrawTexture只是为了更改按钮的外观,请执行以下操作
创建2个GUI样式,并为按钮设置正确的纹理
public GUIStyle musicOff; //assign a GUIStyle with the correct button image for off
private GUIStyle musicOn; //assign a GUIStyle with the correct button image for on
private GUIStyle musicGUIStyle; //holds GUIStyle being displayed. Assign this a default
void OnGUI(){
if(GUI.Button(rect,"", musicGUIStyle)){
if (musicBool) {
music.Pause();
musicBool = false;
musicGUIStyle = musicOff;
}
else {
music.Play();
musicBool = true;
musicGUIStyle = musicOn;
}
}
}
我没有时间进行测试,但我相信这是因为您正在使用OnGUI之外的另一种方法进行GUI调用。我认为ONGUI价格昂贵,所以越少越好。
private Texture musicTexture;
void OnGUI(){
GUI.DrawTexture(rect, musicTexture);
if(GUI.Button(rect,"", new GUIStyle())){
if (musicBool) {
music.Pause();
musicBool = false;
musicTexture = musicOff
}
else {
music.Play();
musicBool = true;
musicTexture = musicOn
}
}
}
如果您使用DrawTexture只是为了更改按钮的外观,请执行以下操作
创建2个GUI样式,并为按钮设置正确的纹理
public GUIStyle musicOff; //assign a GUIStyle with the correct button image for off
private GUIStyle musicOn; //assign a GUIStyle with the correct button image for on
private GUIStyle musicGUIStyle; //holds GUIStyle being displayed. Assign this a default
void OnGUI(){
if(GUI.Button(rect,"", musicGUIStyle)){
if (musicBool) {
music.Pause();
musicBool = false;
musicGUIStyle = musicOff;
}
else {
music.Play();
musicBool = true;
musicGUIStyle = musicOn;
}
}
}
我没有时间进行测试,但我相信这是因为您正在使用OnGUI之外的另一种方法进行GUI调用。我认为ONGUI价格昂贵,所以越少越好。
private Texture musicTexture;
void OnGUI(){
GUI.DrawTexture(rect, musicTexture);
if(GUI.Button(rect,"", new GUIStyle())){
if (musicBool) {
music.Pause();
musicBool = false;
musicTexture = musicOff
}
else {
music.Play();
musicBool = true;
musicTexture = musicOn
}
}
}
如果您使用DrawTexture只是为了更改按钮的外观,请执行以下操作
创建2个GUI样式,并为按钮设置正确的纹理
public GUIStyle musicOff; //assign a GUIStyle with the correct button image for off
private GUIStyle musicOn; //assign a GUIStyle with the correct button image for on
private GUIStyle musicGUIStyle; //holds GUIStyle being displayed. Assign this a default
void OnGUI(){
if(GUI.Button(rect,"", musicGUIStyle)){
if (musicBool) {
music.Pause();
musicBool = false;
musicGUIStyle = musicOff;
}
else {
music.Play();
musicBool = true;
musicGUIStyle = musicOn;
}
}
}
为什么要创建两个GUI按钮?我刚开始使用C#/Unity。我有点了解事情是如何运作的。我的错误。为什么要创建两个GUI按钮?我刚开始使用C#/Unity。我有点了解事情是如何运作的。我的错误。为什么要创建两个GUI按钮?我刚开始使用C#/Unity。我有点了解事情是如何运作的。我的错误。为什么要创建两个GUI按钮?我刚开始使用C#/Unity。我有点了解事情是如何运作的。我错了,这很有效。我喜欢这个网站社区。一旦我有了名声,我会立即投票。如果答案允许的话,也请把它标记为答案。这非常有效。我喜欢这个网站社区。一旦我有了名声,我会立即投票。如果答案允许的话,也请把它标记为答案。这非常有效。我喜欢这个网站社区。一旦我有了名声,我会立即投票。如果答案允许的话,也请把它标记为答案。这非常有效。我喜欢这个网站社区。一旦我有了声誉,我将立即投票。如果允许,请在答案上做标记。