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 Ochreous · Jun 02, 2013 at 03:30 AM · c#numberkeys

C# Number Key For Loop

Hi everyone, I'm trying to make my number keys work with a for loop. But it doesn't appear to be working. Any idea what's wrong with it?

  public int someInt; 
    void OnGUI() {
     //for (int i = 0; i < 9; i++){
     if (Event.current.type == EventType.KeyDown) {
     if (Event.current.keyCode >= KeyCode.Alpha1
     && Event.current.keyCode <= KeyCode.Alpha9) {
     someInt = i;
     Event.current.Use();
       }
     }
   }
 }
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
0

Answer by Bunny83 · Jun 02, 2013 at 03:57 AM

I guess you just want to know which key has been pressed in your someInt variable?

Just do it like this:

 public int someInt;

 void OnGUI()
 {
     Event e = Event.current;
     if (e.type == EventType.KeyDown)
     {
         int key = (int)e.keyCode - (int)KeyCode.Alpha1;
         if (key >= 0 && key < 9)
         {
             someInt = key;
             Event.current.Use();
         }
     }
 }

someInt will be set to a number of 0 - 8 (matching key 1 - 9) when one of the Alpha keys is pressed.

Comment
Add comment · Show 3 · 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 Ochreous · Jun 02, 2013 at 04:05 AM 0
Share

No, I want to get it to work with for loops. I have other things that I need to work with like strings and booleans. I would like to get it to work like the way I have my GUI.Buttons setup.

  public string[] someText;
  public string someString;

 void OnGUI () {
 someText= new string[] 
 {"Text1","Text2","Text3","Text4","Text5","Text6"};

 for(int i = 0; i < someText.Length; i++){

 GUILayout.Button(someText[i], GUILayout.Width(142), GUILayout.Height(25)){

 someString = someText[i];
    }
 }
 

avatar image Chronos-L · Jun 02, 2013 at 04:14 AM 0
Share

If you have different codes for different scenario, just use a switch-case. If you need to work with arrays, just use the someInt as the array index.

 void OnGUI()
 {
     Event e = Event.current;
     if (e.type == EventType.$$anonymous$$eyDown)
     {
         ...
 
         switch( someInt ) {
            case 0:
            //Do stuff
            break;
            case 1:
            //Do stuff
            break;
         }
     }
 }

If you really loves loop, then:

 private $$anonymous$$eyCode[] key;
 
 void Start() {
    key = new $$anonymous$$eyCode[] {
       $$anonymous$$eyCode.Alpha1,
       $$anonymous$$eyCode.Alpha2,
       ...
    };
 }
 
 void OnGUI()
 {
     Event e = Event.current;
     if (e.type == EventType.$$anonymous$$eyDown)
     {
         for( int i = 0; i < key.length; ++i ) {
             if( e.keyCode == key[i] ) {
                 //Do stuff here
             }
         }
     }
 }
avatar image Bunny83 · Jun 02, 2013 at 09:47 AM 1
Share

A for loop makes no sense at all in this situation. Unity will call OnGUI for specific events. In the case of GUI.Buttons it's fine to use a for loop since each Button has to check it's screen area when a $$anonymous$$ouseDown / $$anonymous$$ouseUp event occurs. However key-events aren't context-related. So through what colletion do you want to iterate with your for loop? The only reasonable thing would be to use Chronos-L second example, but it would do exactly the same as my solution. $$anonymous$$y key variable would match your "i" variable.

Here's a combined example:

 public string[] someText = new string[]{"Text1","Text2","Text3","Text4","Text5","Text6"};
 public string someString;
 
 void OnButtonAction(int aIndex)
 {
     someString = someText[aIndex];
 }
 
 void OnGUI ()
 {
     Event e = Event.current;
     for(int i = 0; i < someText.Length; i++)
     {
         if (GUILayout.Button(someText[i], GUILayout.Width(142), GUILayout.Height(25))
         {
             OnButtonAction(i);
         }
     }
     if (e.type == EventType.$$anonymous$$eyDown)
     {
         int key = (int)e.keyCode - (int)$$anonymous$$eyCode.Alpha1;
         if (key >= 0 && key < someText.Length)
         {
             OnButtonAction(key);
             Event.current.Use();
         }
     }
 }

This would display 6 buttons and the user can either click one of the buttons or press the corresponding key to execute the action.

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

17 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

Related Questions

C# Accessing keys 1-9 All at Once 1 Answer

How to Name Individual Buttons Within a List 2 Answers

c# Adjust In-Game audio 1 Answer

C# How to Detect Edges of a Collider 0 Answers

FPS keep a loadout 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