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
0
Question by HROP · Jan 03, 2012 at 02:23 AM · inputstringcursortextfield

How to define in which text field the cursors is located

Hi,

I have two different textfields. When I start typing however, no matter where the cursor is located, both text fields receive the same input.

If anyone could point me in the right direction, it would be greatly appreciated. Also, both text fields, have different var names.

Here's the code I am using;

 private var mytarget: GameObject;
 private var variable: float;
 var num: int;
  var c : char;
  var mystring : String = "";
 
 var myfloat : float = 0.0;
 
 function Start(){
 mytarget = GameObject.Find("Cube");
 
 }
 
 function Update () {
 
 for (c in Input.inputString)
     {
     if(c== "\b"[0])
         {
         if (guiText.text.Length !=0)
         guiText.text = guiText.text.Substring(0, guiText.text.Length -1);
         }
     else if(c =="\n"[0]|| c== "\r"[0])
         {
         print (guiText.text);
         num = parseInt (guiText.text);
         }
     else 
         {
         guiText.text +=c;
         }
     }
 }
 
 
 function OnGUI()
 {
 GUI.TextField(Rect(50, 50, 200, 20), guiText.text);
 }


  
   
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 drudiverse · Jul 14, 2014 at 10:20 AM 0
Share

GUI.GetNameOfFocusedControl thanks that sounds great

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by syclamoth · Jan 03, 2012 at 02:50 AM

This isn't the usual way of managing text fields in UnityGUI. Why aren't you doing it the normal way?

guiText.text = GUI.TextField(Rect(50, 50, 200, 20), guiText.text);

In any case, you can use Rect.Contains to check for the screen coordinates of the mouse if you are set on doing things in the strange way you are doing here. Keep a rect

var textRect : Rect = Rect(50, 50, 200, 20);

and then check

var correctedMousePosition : Vector2 = Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y);
if(textRect.Contains(correctedMousePosition))
{
    // Do the rest!
}

EDIT: A more sophisticated way to do this would be to use GUI.GetNameOfFocusedControl. This way, you could use the GUI's keyboard and mouse focusing system to manage which one you are passing input to through your special input parser!

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 HROP · Jan 03, 2012 at 03:20 AM

Thanks for your reply. After a few more trials and errors, I managed to get it to work, using a boolean and the GUI.GetNameOfFocusedControl function. Works great !

Here's the result var num_a: int; var c_a : char; var ici_a: boolean = false; var mystring_a : String = "Enter RPM here";

 function Start(){
 guiText.text = mystring_a;
 
 }
 
 function Update () {
 if (ici_a == true)
 {
     for (c_a in Input.inputString)
         {
         if(c_a== "\b"[0])
             {
             if (guiText.text.Length !=0)
             guiText.text = guiText.text.Substring(0, guiText.text.Length -1);
             }
         else if(c_a =="\n"[0]|| c_a== "\r"[0])
             {
             print (guiText.text);
             num_a = parseInt (guiText.text);
             }
         else 
             {
             guiText.text +=c_a;
             }
         }
     }
 }
 
 function OnGUI()
 {
 GUI.SetNextControlName ("TXT_rpm");
 GUI.TextField(Rect(50, 50, 500, 20), guiText.text);
 
 if (GUI.GetNameOfFocusedControl () == "TXT_rpm")
     {
     ici_a = true;
     }
 
 if (GUI.GetNameOfFocusedControl () != "TXT_rpm")
     {
     ici_a = false;
     guiText.text = mystring_a;
     }
 
 if (GUI.GetNameOfFocusedControl () == "TXT_rpm" & Input.GetMouseButtonDown(0))
     {
     guiText.text="";
     }
 },
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

6 People are following this question.

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

Related Questions

Taking data from text file 2 Answers

Android Textfield locks out input until esc 2 Answers

comparing a number input to a letter input 3 Answers

TextField plus Return Key issue 2 Answers

Can't Input when focus a text field 2 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