2016年4月5日 星期二

Unity C# 使用者輸入錯誤 (整數)

using UnityEngine;
using System.Collections;
using System;
using System.Windows.Forms;

public class ThreadTest : MonoBehaviour {

    string x = "";  // 數入字串

    // 整數判斷
    bool setNumber() {

        int numX;  // 整數

        bool intSw = Int32.TryParse(x, out numX);

        return intSw;
    }

    void OnGUI() {

        inputNumber();
        if (GUI.Button(new Rect(UnityEngine.Screen.width/UnityEngine.Screen.width, UnityEngine.Screen.height/UnityEngine.Screen.height * 50, 100 , 30),"確定")) {
            if (x.Equals(""))
                MessageBox.Show("不能空白");
            else {
                if (setNumber().Equals(true)) MessageBox.Show("正確");
                else MessageBox.Show("只能輸入整數");
            }
        }
    }

    // GUILayout
    void inputNumber() {
        GUILayout.BeginArea(new Rect(UnityEngine.Screen.width/ UnityEngine.Screen.width, UnityEngine.Screen.height/ UnityEngine.Screen.height, 100, 50));
        GUILayout.BeginHorizontal();
        x = GUILayout.TextField(x, 100);
        GUILayout.EndHorizontal();
        GUILayout.EndArea();
    }
}
--------------------------------
執行結果:

     空白結果


    輸入錯誤


    輸入正確


沒有留言:

張貼留言