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
1
Question by jacksmash2012 · Mar 01, 2011 at 04:32 PM · guifloat

how to use a float in a GUI.TextField?

Using C#, in the OnGUI function I just have something simple like this:

someFloatMemberVariable = float.Parse(GUI.TextField(someRect, someFloatMemberVariable.ToString()));

someFloatMemberVariable is of type float and is initialized to say 1.5, but when I type in the textfield another float, the decimal won't show up, and I always get a FormatException.

Any thoughts?

Comment
Add comment · Show 1
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 yoyo · Mar 01, 2011 at 05:30 PM 1
Share

I think when you get as far as typing "12." then the decimal point would be stripped by the parsing operation. You would have the same problem with trailing zeroes after the decimal. I suggest you let the user type into a string variable, then parse that into your float, rather than continuously converting the float to a string. Not sure about the format exception though.

6 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by stfx · Mar 22, 2012 at 10:07 AM

The trick here is to use a string format for the variable which forces the decimal point. Like so:

 public float Length;

 void OnGUI()
 {
     string lengthText = GUILayout.TextField(Length.ToString("0.00"));
 
     float newLength;
     if (float.TryParse(lengthText, out newLength))
     {
         Length = newLength;
     }
 }

EDIT: It is also possible by adding a bool to store if a . is entered. Downside is that it needs a further variable:

 public float Length;
 bool _isLengthDecimal;

 void OnGUI()
 {
     string lengthText = GUILayout.TextField(Length.ToString() + (_isLengthDecimal ? "." : ""));
     _isLengthDecimal = lengthText.EndsWith(".");
 
     float newLength;
     if (float.TryParse(lengthText, out newLength))
     {
         Length = newLength;
     }
 }
Comment
Add comment · Show 2 · 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 thesfid · Oct 07, 2013 at 03:22 PM 0
Share

Thanks, stfx. This helped me out in controlling animation speed using a textfield. I used...

animSpeed = float.Parse(GUI.TextField(Rect(10, 10, 100, 20), animSpeed.ToString("0.00")));

edit I still get a FormatException error when typing "0.1" with "$$anonymous$$ char: ." as the culprit. The value is still valid as far as functionality is concerned, but I don't like that the error is generated. I have to type "0" then move the cursor behind the "." and then type "1" to keep from getting the error. Not the most intuitive way to input text into a field...

avatar image Seneral · Jul 20, 2015 at 04:47 PM 0
Share

If you really don't want to have a .00 forced at the end but also don't want to have a seperate variable, you can try this. Very costly though...

avatar image
1

Answer by Vijay Kumar R · Apr 16, 2015 at 08:59 PM

EditorGUI.FloatField

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
avatar image
0

Answer by mrde · Mar 01, 2011 at 05:11 PM

when you trying to parse to float and parse value is not valid ("3.5g", "4.k3", ...), it throw exception you can write follow

try {
     someFloatMemberVariable = float.Parse(GUI.TextField(rect, someFloatMemberVariable.ToString()));
} catch {}
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 jacksmash2012 · Mar 01, 2011 at 05:21 PM 0
Share

Yes, I know about try/catch and how it works. That doesn't solve my problem though.

avatar image
-1

Answer by trothmaster · Oct 06, 2014 at 06:08 PM

Here is how I solve this. I "clean" the string using regular expression You will need this at the top of your class:

 using System.Text.RegularExpressions;

And here are two methods. One that will clean a float and another an int:

    public static string CleanStringForFloat(string input)
    {
         if(Regex.Match(input,@"^-?[0-9]*(?:\.[0-9]*)?$").Success)
             return input;
         else {
             Debug.Log("Error, Bad Float: " + input);
             return "0";
         }
     }
 
     public static string CleanStringForInt(string input)
     {
         if(Regex.Match(input,"([-+]?[0-9]+)").Success)
             return input;
         else {
             Debug.Log("Error, Bad Int: " + input);
             return "0";
         }
     }

To use them you would just do this:

 string testFloat = Guilayout.TextField(testFloat);
 testFloat = CleanStringForFloat(testFloat);
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
avatar image
0

Answer by Seneral · Jul 20, 2015 at 04:40 PM

For another solution that mimics the original, look here. It has some downsides, though.

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
  • 1
  • 2
  • ›

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

GUI.DrawTexture Error & Static Variables!! 2 Answers

OnGUI using UnityEditor how to make FloatField work? 1 Answer

"Talent Tree" global yes/false var and int? 3 Answers

GUI Button Position - Can it float to the top-left? 3 Answers

How To Make Floating Combat Text In new UI System of 4.6 1 Answer


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