Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by Abraham · Sep 06, 2010 at 08:32 AM · guiguilayoutgui-textvalidationgui-style

create a box that player can type in it ( only type numbers )

Hi How i can create a box that player can type in it ( only numbers ) ? thanks

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by jashan · Sep 06, 2010 at 09:10 AM

Creating a text field is pretty simple with Unity GUI, there's two options:

  1. GUI.TextField(...)
  2. GUILayout.TextField(...)

The difference is that with GUI, you have to manage positioning yourself, while GUILayout manages positioning for you, see also the GUI Layout Tutorial.

For allowing only numbers, there are a couple of possible solutions: One is trying to parse the string that was entered, and showing an error message to the user when that fails. One nice way of doing this (that avoids exceptions) is using Int32.TryParse(...). The disadvantage is that the user can enter a alphanumeric value which you probably don't want.

One solution that prevents entering non-digit characters can be found in the Unity forums: Only numbers in TextField. You could make this a little more elegant by using Char.IsDigit(...).

So that would give you (in C#; the example in the forum posting is UnityScript):

using UnityEngine; using System.Collections; using System.Text;

public class NumbersOnly : MonoBehaviour {

 private string textFieldValue = "";
 private StringBuilder textFieldTemp = null;

 public void OnGUI() {
     // NOTE: This creates a new StringBuilder every OnGUI-call which
     // could be a problem in Unity iPhone because of Garbage Collection
     // using stringVariable += c, however, would even be much worse!
     textFieldTemp = new StringBuilder();
     foreach (char c in textFieldValue) {
         if (char.IsDigit(c)) {
             textFieldTemp.Append(c);
         }
     }
     textFieldValue = GUI.TextField(new Rect(10F, 10F, 100F, 20F),
                                    textFieldTemp.ToString());
 }

}

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image konanxiao · Jul 02, 2014 at 09:27 AM 0
Share

Hi,

I just try this method using StringBuilder. It's work fine except I cannot input the decimal separation then... that it the dot between the int and the fraction value.

avatar image
0

Answer by gooncorp · Apr 07, 2013 at 08:42 PM

that looks nice but it looks like it could be heavy?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

create a box that player can type in it 1 Answer

"'UnityEngine.GUI.DoTextField' is inaccessible due to its protection level." 1 Answer

Having a GUILayout Window position immediately at screen center 5 Answers

Why can't I get my tooltip to show only when there is a tooltip set? 2 Answers

GUI.Window error. InvalidOperationException: Hashtable.Enumerator: snapshot out of sync. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges