Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Blankzz · Jul 09, 2011 at 05:22 PM · inspectorcustomenterintfield

Detecting when Enter Key is Pressed IntField

Hi, I think this question has been asked before but couldn't find answers again. I'm displaying list in a custom inspector and but would like to be able to copy list values when changing the list size. Unfortunately I can't see that it's going to be possible without changing the size only when the enter/return key is pressed. Is there a way to detect when the enter key is pressed in a IntField? Heres a code snippet.

 _showTags = EditorGUILayout.Foldout(_showTags, "Target Tags");
         if (_showTags)
         {
             _tagsArraySize = _target.tags.Count;
             _tagsArraySize = EditorGUILayout.IntField("Tag Count", _tagsArraySize);
             if (GUI.changed)
             {
                 if (_tagsArraySize != _target.tags.Count)
                 {
                     //implement copying old values here if new arraysize was smaller than the last
                     _tagsTemp = new List<String>(_tagsArraySize);
                     for (int tagIndex = 0; tagIndex < _tagsArraySize; tagIndex++)
                     {
                         _tagsTemp.Add("");
                     }
 
                     _target.tags = new List<String>(_tagsTemp);
                     EditorUtility.SetDirty(_target);
                 }
             }
 
             for(int tagIndex = 0; tagIndex < _target.tags.Count; tagIndex++)
             {
                 _target.tags[tagIndex] = EditorGUILayout.TextField(tagIndex.ToString(), _target.tags[tagIndex]);
                 EditorUtility.SetDirty(_target);
             }
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

2 Replies

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

Answer by Bunny83 · Jul 09, 2011 at 05:41 PM

Take a look at the Event class. It contains all information on the current event that causes OnGUI / OnInspectorGUI or similar event handlers to execute.

Check if Event.current.type is EventType.KeyDown and Event.current.keyCode is KeyCode.Return .

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 Blankzz · Jul 09, 2011 at 07:20 PM 0
Share

Thanks for the links. I've tried a few things but I can't seem to get it to work. I thought I would have been able to just replace if(GUI.changed) with something like if(Event.current.keyCode == $$anonymous$$eycode.Return) but that doesn't seem to work.

avatar image
0

Answer by Blankzz · Jul 09, 2011 at 08:30 PM

Ok so I've got it to work but there's one problem. I seem to be getting an exception error and I cant seem to make it out. Heres the new code and the error. Can anybody make it out?

 if (_showTags)
         {
             _tagsArraySize = EditorGUILayout.IntField("Tag Count", _tagsArraySize);
             Event e = Event.current;
             if (e.type != null && e.isKey== true && e.keyCode==KeyCode.Return)
             {
                 if (_tagsArraySize != _target.tags.Count)
                 {
                     //implement copying old values here if new arraysize was smaller than the last
                     _tagsTemp = new List<String>(_tagsArraySize);
                     for (int tagIndex = 0; tagIndex < _tagsArraySize; tagIndex++)
                     {
                         _tagsTemp.Add("");
                     }
 
                     _target.tags = new List<String>(_tagsTemp);
                     EditorUtility.SetDirty(_target);
                 }
             }
 
             for(int tagIndex = 0; tagIndex < _target.tags.Count; tagIndex++)
             {
                 _target.tags[tagIndex] = EditorGUILayout.TextField(tagIndex.ToString(), _target.tags[tagIndex]);
                 EditorUtility.SetDirty(_target);
             }
         }

Error

ArgumentException: GUILayout: Mismatched LayoutGroup.KeyUp UnityEngine.GUILayoutUtility.BeginLayoutGroup (UnityEngine.GUIStyle style, UnityEngine.GUILayoutOption[] options, System.Type LayoutType) (at C:/BuildAgent/work/6bc5f79e0a4296d6/Runtime/ExportGenerated/Editor/GUILayoutUtility.cs:153) UnityEngine.GUILayout.BeginVertical (UnityEngine.GUIContent content, UnityEngine.GUIStyle style, UnityEngine.GUILayoutOption[] options) (at C:/BuildAgent/work/6bc5f79e0a4296d6/Runtime/ExportGenerated/Editor/GUILayout.cs:192) UnityEngine.GUILayout.BeginVertical (UnityEngine.GUILayoutOption[] options) (at C:/BuildAgent/work/6bc5f79e0a4296d6/Runtime/ExportGenerated/Editor/GUILayout.cs:184) UnityEditor.MaterialEditor.OnInspectorGUI () (at C:/BuildAgent/work/6bc5f79e0a4296d6/Editor/Mono/Inspector/MaterialEditor.cs:107) UnityEditor.InspectorWindow.OnGUI () (at C:/BuildAgent/work/6bc5f79e0a4296d6/Editor/Mono/Inspector/InspectorWindow.cs:364) System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture)

If I comment out the line "_target.tags = new List(_tagsTemp);" I no longer get the error but obviously doesn't work how it should. At least I know the problems lies after assigning the new list.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Inspector button for custom class 1 Answer

Custom Inspector - rearranging control columns. 0 Answers

Custom Editor problems 2 Answers

Custom inspector: uniformly display all instances of a custom class 2 Answers

WebPlayer Custom Params Inspector 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