Unity3D - How to make a texture changing mute button/toggle?

前端 未结 2 1089
抹茶落季
抹茶落季 2021-01-27 19:42

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\'

相关标签:
2条回答
  • 2021-01-27 20:03

    You can also use UI button in this way:

    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;
    
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-27 20:19

    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)

        public Texture2D Texture1;
        public Texutre2D Texture2;
        public bool textureBool;
    
        void Awake() {
          textureBool=true;
    
        void OnGUI(){
    
        if( GUI.Button( rect , textureBool ? texture1:texture2 ) )
            {
               textureBool = !textureBool;
            }
    
          }
    
    0 讨论(0)
提交回复
热议问题