x


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);
}
more ▼

asked Jan 03 '12 at 02:23 AM

HROP gravatar image

HROP
52 4 5 9

(comments are locked)
10|3000 characters needed characters left

2 answers: sort oldest

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!

more ▼

answered Jan 03 '12 at 02:50 AM

syclamoth gravatar image

syclamoth
14.8k 7 15 80

(comments are locked)
10|3000 characters needed characters left

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="";
    }
},
more ▼

answered Jan 03 '12 at 03:20 AM

HROP gravatar image

HROP
52 4 5 9

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x956
x420
x198
x151

asked: Jan 03 '12 at 02:23 AM

Seen: 867 times

Last Updated: Jan 03 '12 at 03:20 AM