Snippets

David Fraj Calculadora en Unity

Created by David Fraj

File Calculadora.cs Added

  • Ignore whitespace
  • Hide word diff
+using UnityEngine;
+using System.Collections;
+using UnityEngine.UI;
+
+public class Menu02 : MonoBehaviour {
+
+	//Componentes/Propiedades publicas que apareceran en el Canvas
+	public InputField led;
+	public Button boton0;
+	public Button boton1;
+	public Button boton2;
+	public Button boton3;
+	public Button boton4;
+	public Button boton5;
+	public Button boton6;
+	public Button boton7;
+	public Button boton8;
+	public Button boton9;
+	public Button botonMas;
+	public Button botonMenos;
+	public Button botonPor;
+	public Button botonDiv;
+	public Button botonIgual;
+	public Button botonC;
+
+	public Image fondo;
+
+	//Propiedades privadas a usar solo aqui
+	private bool hayComa;
+	private float enMemoria;
+	private string operacion;
+	private bool vaciarLed;
+	private float elLed;
+	//private bool operacionPulsada;
+
+	// Use this for initialization
+	void Start () {
+		limpiarLed ();
+	}
+	
+	// Update is called once per frame
+	void Update () {
+	
+	}
+
+	//QUE HACER AL PULSAR EL BOTON:
+	public void pulsarBoton(string digito){
+		//El digito es lo que pulsamos con el boton
+		switch (digito) {
+		case "0":
+		case "1":
+		case "2":
+		case "3":
+		case "4":
+		case "5":
+		case "6":
+		case "7":
+		case "8":
+		case "9":
+			//Si vaciarLed esta a true quiere decir, que hemos pulsado una operacion + - * / = C
+			//Asi que lo vaciamos antes de nada.
+			if (vaciarLed == true) {
+				led.text = "0";
+				vaciarLed = false;
+			}
+
+			//Si el led pulsado es el cero, no dejamos acumular ceros
+			if (led.text == "0") {
+				led.text = digito;
+			} else {
+				led.text = led.text + digito;
+			}
+
+			//operacionPulsada = false;
+
+			break;
+
+		case ".":
+			//Si no ha salido una coma, añadimos la coma
+			if (hayComa == false) {
+				led.text = led.text + digito;
+				hayComa = true;
+			}
+
+			//operacionPulsada = false;
+
+			break;
+
+		case "C":
+			//Si le damos a limpiar
+			limpiarLed ();
+			break;
+
+		case "+":
+		case "-":
+		case "*":
+		case "/":
+		case "=":
+			//Si le damos a una operacion
+			//Extraemos en un float el contenido del led
+			float.TryParse (led.text, out elLed);
+
+			//Depende de lo que hay guardado en operacion, hacemos los calculos
+			if (operacion == "+") {
+				enMemoria = enMemoria + elLed;
+				led.text = enMemoria.ToString ();
+			} else if (operacion == "-") {
+				enMemoria = enMemoria - elLed;
+				led.text = enMemoria.ToString ();
+			} else if (operacion == "*") {
+				enMemoria = enMemoria * elLed;
+				led.text = enMemoria.ToString ();
+			} else if (operacion == "/") {
+				enMemoria = enMemoria / elLed;
+				led.text = enMemoria.ToString ();
+			} else {
+				enMemoria = elLed;
+			}
+			vaciarLed = true;
+			operacion = digito;
+			if (operacion == "=") {
+				operacion = "";
+			}
+
+			hayComa = false;	
+
+			break;
+
+		case "borrar":
+			//Recojo el ultimo digito para saber cual estoy quitando
+			string ultimo;
+			ultimo = led.text.Substring (led.text.Length - 1, 1);
+
+			//Borro un solo digito
+			led.text = led.text.Substring (0, led.text.Length - 1);
+			if (led.text.Length == 0) {
+				led.text = "0";
+			}
+
+			//Si el  ultimo digito era una coma, activo el poder añadir coma
+			if (ultimo == ".") {
+				hayComa = false;
+			}
+			break;
+
+		}
+		//Fin del SWITCH
+			
+	}
+	//FIN DE PULSAR BOTON
+
+	//Metodo para limpiar el Led
+	public void limpiarLed(){
+		led.text = "0";
+		hayComa = false;
+		enMemoria = 0;
+		operacion = "";
+		vaciarLed = false;
+		//operacionPulsada = false;
+	}
+
+
+
+
+
+	//METODOS EXTRAS DEL SCRIPT
+	public void cambiarFondo(){
+		//Saco un numero aleatorio para el tema de los numeros de fondo
+		System.Random aleatorio = new System.Random ();
+		int numero = aleatorio.Next (1, 5);
+		//fondo.sprite = Resources.Load<Sprite>("Sprites/fondo03");
+		fondo.sprite = Resources.Load<Sprite>("Sprites/fondo0"+numero);
+
+	}
+		
+	public void cambiarColores(){
+		//Saco un numero aleatorio para el tema de los numeros de fondo
+		//System.Random aleatorio = new System.Random ();
+		//int numero = aleatorio.Next (1, 5);
+
+		//NO PUEDO HACER ESTO:
+		//Color color = new Color (0.2f, 0.5f, 0.4f);
+		//boton0.colors.normalColor = Color.red;
+		//NI HACER ESTO:
+		//boton0.GetComponent<Button> ().colors.normalColor = Color.red;
+		//ASI QUE USO ESTE TRUCO
+		Button b = boton0.GetComponent<Button>(); 
+		ColorBlock cb = b.colors;
+		//cb.normalColor = Color.red;
+		cb.normalColor = new Color (Random.value, Random.value, Random.value);
+		b.colors = cb;
+
+		b = boton1.GetComponent<Button>(); 
+		cb = b.colors;
+		cb.normalColor = new Color (Random.value, Random.value, Random.value);
+		b.colors = cb;
+
+		b = boton2.GetComponent<Button>(); 
+		cb = b.colors;
+		cb.normalColor = new Color (Random.value, Random.value, Random.value);
+		b.colors = cb;
+
+		b = boton3.GetComponent<Button>(); 
+		cb = b.colors;
+		cb.normalColor = new Color (Random.value, Random.value, Random.value);
+		b.colors = cb;
+
+		b = boton4.GetComponent<Button>(); 
+		cb = b.colors;
+		cb.normalColor = new Color (Random.value, Random.value, Random.value);
+		b.colors = cb;
+
+		b = boton5.GetComponent<Button>(); 
+		cb = b.colors;
+		cb.normalColor = new Color (Random.value, Random.value, Random.value);
+		b.colors = cb;
+
+		b = boton6.GetComponent<Button>(); 
+		cb = b.colors;
+		cb.normalColor = new Color (Random.value, Random.value, Random.value);
+		b.colors = cb;
+
+		b = boton7.GetComponent<Button>(); 
+		cb = b.colors;
+		cb.normalColor = new Color (Random.value, Random.value, Random.value);
+		b.colors = cb;
+
+		b = boton8.GetComponent<Button>(); 
+		cb = b.colors;
+		cb.normalColor = new Color (Random.value, Random.value, Random.value);
+		b.colors = cb;
+
+		b = boton9.GetComponent<Button>(); 
+		cb = b.colors;
+		cb.normalColor = new Color (Random.value, Random.value, Random.value);
+		b.colors = cb;
+	}
+
+	public void salir(){
+		Application.Quit ();
+	}
+
+}
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.