資源簡介
此腳本用unity3D實現了簡單2D計算器,實現計算器的加減乘除等功能,博客地址https://blog.csdn.net/qq874455953
代碼片段和文件信息
using?UnityEngine;
using?System.Text.Regularexpressions;
using?System.Collections.Generic;
using?System;
using?System.Linq;
public?class?NewBehaviourscript?:?MonoBehaviour
{
????public?static?bool?IsNumeric(string?value)
????{
????????return?Regex.IsMatch(value?@“^[+-]?\d*[.]?\d*$“);
????}
????
???
//用來顯示結果
public?string?result?=?““;
????//第一個操作數
????public?static?string?str1?=?““;
????//第二個操作數
????public?static?bool?haveDot?=?false;
????public?static?bool?isCaclutate?=?false;
????void?OnGUI()
????{
????????
????????//對數字進行處理
????????if?(GUI.Button(new?Rect(100?100?100?60)?“CE“))
????????{
????????????result?=?““;
????????????str1?=?““;
????????????haveDot?=?false;
????????}
????????if?(GUI.Button(new?Rect(210?100?100?60)?“ד)?&&?str1.Substring(str1.Length?-?1?1)?!=?“-“?&&?str1.Substring(str1.Length?-?1?1)?!=?“+“?&&?str1.Substring(str1.Length?-?1?1)?!=?“.“)
????????{
????????????if?(IsNumeric(str1.Substring(str1.Length?-?1?1)))
????????????{
????????????????Debug.Log(str1.Substring(str1.Length?-?1?1));
????????????????str1?+=?“*“;
????????????????haveDot?=?false;
????????????????isCaclutate?=?false;
????????????}
????????????result?=?str1;
????????}
????????if?(GUI.Button(new?Rect(320?100?100?60)?“÷“)?&&?str1.Substring(str1.Length?-?1?1)?!=?“-“?&&?str1.Substring(str1.Length?-?1?1)?!=?“+“?&&?str1.Substring(str1.Length?-?1?1)?!=?“.“)
????????{
????????????if?(IsNumeric(str1.Substring(str1.Length?-?1?1)))
????????????{
????????????????str1?+=?“/“;
????????????????haveDot?=?false;
????????????????isCaclutate?=?false;
????????????}
????????????result?=?str1;
????????}
????????if?(GUI.Button(new?Rect(430?100?100?60)?“←“))
????????{
????????????if?(isCaclutate?==?true)
????????????{
????????????????str1?=?““;
????????????????isCaclutate?=?false;
????????????}?else?if?(result.Length?!=?0)
????????????{
????????????????str1?=?str1.Substring(0?str1.Length?-?1);
????????????????
????????????}
????????????result?=?str1;
????????}
????????if?(GUI.Button(new?Rect(100?170?100?60)?“1“))
????????{
????????????if?(isCaclutate?==?true)
????????????{
????????????????str1?=?““;
????????????????isCaclutate?=?false;
????????????????haveDot?=?false;
????????????}
????????????str1?+=?“1“;
????????????result?=?str1;
????????}
????????if?(GUI.Button(new?Rect(210?170?100?60)?“2“))
????????{
????????????if?(isCaclutate?==?true)
????????????{
????????????????str1?=?““;
????????????????isCaclutate?=?false;
????????????????haveDot?=?false;
????????????}
????????????str1?+=?“2“;
????????????result?=?str1;
????????}
????????if?(GUI.Button(new?Rect(320?170?100?60)?“3“))
????????{
????????????if?(isCaclutate?==?true)
????????????{
????????????????str1?=?““;
????????????????isCaclutate?=?false;
????????????????haveDot?=?false;
????????????}
????????????str1?+=?“3“;
????????????result?=?str1;
????????}
????????if?(GUI.Button(new
評論
共有 條評論