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 Tekner · Sep 01, 2012 at 11:16 PM · guitextfocuschatrelease

How can I release gui focus via code?

I am working on a game which has a GUI Text Field to capture chat text. The chat system works fine and it outputs what you type in after you press enter. The problem is that I can't find a way to release the focus from the Text Field and give keyboard input back to the game. This is incredibly frustrating because every time you are done chatting, you have to click on the game window to manually release focus.

I have already tried the following "fixes":

 GUIUtility.keyboard = 0;
 //This technically does release focus from the Text Field,
 //but the game does not regain focus.
 
 GUI.FocusControl("");
 //This does the same thing as the above method.
 //GUI.FocusControl cannot give the game focus back, merely change it, at least as
 //far as I know.
 
 //I've also tried messing around with enabling/disabling controls,
 //the entire GUI interface, and individual GUI calls to see if it'll reset focus.
 //No dice.

Does anyone know of a way to give focus back to the main game window so that the Input class can receive input without needing to click on the game window again?

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 LastTemplar · Sep 09, 2012 at 12:03 AM 0
Share

Never actually worked with focusing windows, but you could try this thread http://forum.unity3d.com/threads/18109-how-to-get-GUI.Window-focus?p=123442#post123442

6 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Dracorat · Apr 10, 2013 at 12:41 AM

What about:

 GUI.UnfocusWindow();

??

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
1

Answer by _met44 · Apr 07, 2014 at 03:45 PM

Hi, in case someone reads this one day, GUI.FocusControl("") works fine to remove focus of gui elements. It either got fixed or people's logic behind its use was wrong but anyways it does work. (unity 4.3.4)

ps: I use C#, maybe it's a problem with JS

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 D3Duck · Sep 30, 2015 at 09:17 PM 1
Share

Best answer. Just tested it and works perfectly, one line of code...

avatar image
-2

Answer by gooncorp · Apr 10, 2013 at 12:18 AM

whats the confusion? the focus/unfocus question is a simple one, one that everyone who writes code should encounter, and one that everyone will encounter when building a video game. thanks peladovii for the answer. :)

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 peladovii · Mar 16, 2013 at 12:16 AM

I dont know if you still need the answer but you can focus any gui element as long as you have previously defined a name for it:

 GUI.SetNextControlName ("Input");
 submittedName = GUI.TextField (Rect (x, y, a, b), submittedName, 12);
 GUI.FocusControl ("Input");
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 Tekner · Mar 16, 2013 at 12:22 AM 0
Share

As my original post shows, I've already tried that method. $$anonymous$$y issue is giving input focus back to the game itself, not a defined GUI element. If the game itself was somehow registered as a named GUI element, then that method would be perfect, but sadly it doesn't seem to work that way.

avatar image peladovii · Mar 16, 2013 at 04:33 AM 0
Share

Oh, yes, I know what you mean, I had a similar issue but decided to just disable the textfield, because I found no other solution...

 GUI.skin = submissionSkin;
      GUI.SetNextControlName ("Input");
     if(showChat) {
         submittedName = GUI.TextField (Rect (Screen.width / 2 - inputSize.x / 2, Screen.height / 2 - inputSize.y / 2, inputSize.x, inputSize.y), submittedName, 12);
         GUI.FocusControl ("Input");
         }
     
      //IF ENTER 
     if (Event.current.type == EventType.$$anonymous$$eyDown && Event.current.character == '\n' && GUI.GetNameOfFocusedControl () == "Input" && donePlaying) {
           Game$$anonymous$$aster.playerName = submittedName;
         showChat = false;
avatar image
0

Answer by Matthew A · Mar 16, 2013 at 12:41 AM

I had the same issue a while ago. I solved it like this (offscreen control for focussing on):

 static var hackControlName : String = "hackery129578835432342";
 
 // call this function in OnGUI before calling TextField (only needs to be called once)
 static function FocusHack() {
     // fake control used for unfocussing things, since we can't call
     // FocusControl with no arguments and an empty string doesn't work
     GUI.SetNextControlName(hackControlName);
     GUI.Button(Rect(-10000,-10000, 0,0), GUIContent.none);
 }
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

16 People are following this question.

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

Related Questions

speech / chat bubbles above character or game object 0 Answers

Focus on InputField upon key press? 3 Answers

How to prevent ignored input after typing in a GUI? 0 Answers

C# GUI Button 2 Answers

4.6 GUI Button inside a scrolling Text Box 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