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 BenKurdziel · Sep 30, 2014 at 01:28 AM · c#editoreditorwindoweventtextfield

Detect Enter Key Event with GUILayout.TextField Focused

Hey guys,

Working on an editor script that will allow my designers to modify NPC Waypoints easily. Right now I want to allow them to change the name of each waypoint for the sake of clarity.

Currently, if you press the GUILayout.Button to toggle the edit for each waypoint on and off, it works fine. I also wanted to give the option to simply press Enter. This "works" but only when the sceneview has been selected. So currently, you have to click the "change" button, type the name you want, select the scene view, then press enter.

Is there a way I can get the enter event to be detected from within the focus of the EditorWindow itself (or more specifically the TextField) without having to select the Scene View each time?

Here is a simplified version of my code with only the applicable code included.

Any suggestions are appreciated! Thanks guys!

 void SceneGUI(SceneView sceneView)
 {
     //If we are changing the name of ANY of the waypoints, this should be true
     if (changingName)
     {
         Debug.Log("Changing");
         //Check for Enter Key Press
         if (Event.current.keyCode == KeyCode.Return)
         {
             Debug.Log("hit");
             waypoints[nameChangeID].GetComponent<Waypoint>().changingName = false;
             SceneView.RepaintAll();
             Repaint();
             changingName = false;
         }
     }
 }
 
 
 void OnGUI()
 {
     //For each waypoint, draw a row in our inspector
     for (int i = 0; i < waypoints.Count; i++)
     {
         GameObject go = waypoints[i];
         GUILayout.BeginHorizontal();
         
         //Make sure we don't print anything that might have turned up null
         if (go != null)
         {
             //Print a Label or TextField based on the changingName state of each instance of a Waypoint
             if (go.GetComponent<Waypoint>().changingName)
                 go.GetComponent<Waypoint>().name = GUILayout.TextField(go.GetComponent<Waypoint>().name, 20);
             else
                 GUILayout.Label(go.GetComponent<Waypoint>().name);
 
             //If the "Change Name" button is clicked, toggle changeName bool
             if (GUILayout.Button(Resources.LoadAssetAtPath<Texture>("Assets/Scripts/AI/Patrolling Editor/Images/pencil.png"), GUILayout.Width(30), GUILayout.Height(30)))
             {
                 Waypoint wps = go.GetComponent<Waypoint>();
                 
                 //If true, set false, confirm name
                 if (wps.changingName)
                 {
                     wps.changingName = false;
                     changingName = false;
                     SceneView.RepaintAll();
                 }
                 else    //If false, set true, set the ID of the item being changed so we can confirm it with the enter button
                 {
                     wps.changingName = true;
                     changingName = true;                           
                     nameChangeID = i;
                     SceneView.RepaintAll();
                 }
             }
         }
     }
 }
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

1 Reply

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

Answer by BenKurdziel · Oct 02, 2014 at 12:56 AM

Solved my own problem. The issue seems to be that anything inside SceneGUI is dedicated to the Scene View and cannot be called unless the SceneView is currently focused.

To resolve this, I simply put the code for the enter key event into OnGUI (which is registered by the inspector instead), and it worked like a charm.

  void OnGUI()
     {
 if (changingName)
         {
             if (Event.current.keyCode == KeyCode.Return)
             {
                 waypoints[nameChangeID].GetComponent<Waypoint>().changingName = false;
                 SceneView.RepaintAll();
                 Repaint();
                 changingName = false;
 
                 GUIUtility.hotControl = 0;
                 Event.current.Use();
             }
         }

Hope this helps if anyone has a similar problem!

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

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

Related Questions

Listen to Keyboard Events on EditorWindow 1 Answer

How do I wait until after AssetDatabase creates an assets before carrying out another function ? 0 Answers

Associate my custom asset with a custom EditorWindow 3 Answers

Any way to attach a bit of code to individual UI Windows [Editor] 1 Answer

How to "paint" objects in the editor? 3 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