[英]Unity3D - How to make a texture changing mute button/toggle?
I'm trying to make a mute button for in my android game menu, so when I press the button, the texture changes from a playing speaker symbol to a muted speaker symbol (which I've already made in Photoshop). 我试图在我的Android游戏菜单中创建一个静音按钮,因此当我按下该按钮时,纹理会从正在播放的扬声器符号变为静音的扬声器符号(我已经在Photoshop中制作过)。
So when the audio is playing it will have a 'speaker' symbol, but when I press it, it will change to a 'muted speaker' symbol (a speaker with a cross). 因此,当播放音频时,它将带有“扬声器”符号,但是当我按下它时,它将变为“静音扬声器”符号(带有叉号的扬声器)。
Thanks in advance, any help is appreciated! 在此先感谢您的帮助!
first we make a maintexture as the texture that is always used and on awake we put our texture1(speaker) assign to it and if button is pressed we change it to texture2(mute) 首先,我们将maintexture作为始终使用的纹理,在清醒时,将我们的texture1(speaker)分配给它,如果按下按钮,我们将其更改为texture2(mute)
public Texture2D Texture1;
public Texutre2D Texture2;
public bool textureBool;
void Awake() {
textureBool=true;
void OnGUI(){
if( GUI.Button( rect , textureBool ? texture1:texture2 ) )
{
textureBool = !textureBool;
}
}
You can also use UI button in this way: 您还可以通过以下方式使用UI按钮:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ChangeSprite : MonoBehaviour {
public Image image;
public bool isPress = false;
public Button button;
public Sprite Fsprite;
public Sprite Ssprite;
// Use this for initialization
void Start () {
image = button.GetComponent <Image>();
}
// Update is called once per frame
public void ChangeSprites () {
isPress = !isPress;
if ( isPress == true )
{
image.sprite = Ssprite;
}
else
{
image.sprite = Fsprite;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.