유니티 계산기(c#)

coc·2026년 3월 3일

버튼 없는 방식 (enter)

using UnityEngine;
using System;
using UnityEngine.UI;
using TMPro;
using System.Data;

public class GameManager : MonoBehaviour
{
    public TMP_InputField inputField;// 이거는 유니티에서 input창에서 값을 가지고 오기 위해서 사용 
    public TextMeshProUGUI result;
    DataTable dt;

    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        dt = new DataTable();
    }

    // Update is called once per frame
    void Update()
    {
        // Input.GetKeyDown = 키를 눌렀을때 
        // 엔터를 눌렀을떄마다 안에 있는 코드가 실행
        if (Input.GetKeyDown(KeyCode.Return))
        {
            //  inputField.text = "3+3"
            //result.text = dt.Compute(inputField.text, "").ToString();

            // dt.Compute의 결과는 object 타입이므로, 먼저 숫자로 변환이 필요합니다.
            var calculationResult = dt.Compute(inputField.text, "");

            // "N0"는 천 단위 콤마를 찍고 소수점은 표시하지 않는 형식입니다.
            result.text = double.Parse(calculationResult.ToString()).ToString("N0");

        } 
    }
}

버튼 있는 방식 + enter 형식 두개다 동작

using UnityEngine;
using System;
using UnityEngine.UI;
using TMPro;
using System.Data;

public class GameManager : MonoBehaviour
{
    public TMP_InputField inputField;// 이거는 유니티에서 input창에서 값을 가지고 오기 위해서 사용 
    public TextMeshProUGUI result;
    DataTable dt;

    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        dt = new DataTable();
    }

    // Update is called once per frame
    void Update()
    {
    	// enter & space 두개다 동작
        if (Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.Return))
          {
              BtnClicked();
          }
    }

    public void BtnClicked()
    {
        // dt.Compute의 결과는 object 타입이므로, 먼저 숫자로 변환이 필요합니다.
        var calculationResult = dt.Compute(inputField.text, "");

        // "N0"는 천 단위 콤마를 찍고 소수점은 표시하지 않는 형식입니다.
        result.text = double.Parse(calculationResult.ToString()).ToString("N0");
    }
}
profile
시작

0개의 댓글