Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
2
Question by Jonas 1 · Apr 19, 2011 at 06:08 PM · inputkeymanagerkeycode

Input Manager (From string to keycode)

Hi everyone,

Is there a way to convert stringnames from the input manager to the actual keycodes ?

For example: There is a button called "Attack", at the resolution dialog you have assigned the 'a'-key to the "Attack" button.

Now, I'm searching for a function to return the 'a'-key by giving the strinf name "Attack". Is this possible in Unity ?

Thank you in advance.

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

3 Replies

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

Answer by Jesse Anders · Apr 20, 2011 at 04:30 AM

You can use Enum.Parse to convert a string into the associated enumerant. However, the string will have to match the name of the enumerant exactly. If you want to use different names for display purposes, you'll need an additional layer of indirection (e.g. a dictionary mapping your control names to the key code names).

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 KyleRasmusson · Sep 06, 2016 at 06:19 AM

Because this still shows up in Related Questions, I thought I'd add on to this since I just figured out an answer earlier today.

If you can, I'd recommend saving whatever string you're using to PlayerPrefs so that you can use this KeyCode across scenes, or even allow it to be changeable in the future.

Here's what I've gotten

 KeyCode Up, Down, Left, Right, Jump;

     if (PlayerPrefs.HasKey("Up"))
         Up = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Up"));

     if (PlayerPrefs.HasKey("Down"))
         Down = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Down"));

     if (PlayerPrefs.HasKey("Left"))
         Left = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Left"));

     if (PlayerPrefs.HasKey("Right"))
         Right = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Right"));

     if (PlayerPrefs.HasKey("Jump"))
         Jump = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Jump"));

This takes the KeyCode, gets the string, and converts your string into the KeyCode. It looks like thats what's being used, but if you're not saving to PlayerPrefs, you can just remove that part, and I haven't tested this, but I believe you could remove the PlayerPrefs.GetString and just pass in your string and it should work. Defining the PlayerPrefs is off on another script. Here's the tutorial that I used to write all this.

https://youtu.be/HZVqFIRmdNs

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 VorTer · Apr 12, 2020 at 09:41 AM

Error: Must Specify vaild information for persing in the string.

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 VorTer · Apr 12, 2020 at 09:52 AM 0
Share

Fixed. I was doing this when there was no string Info Yet

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

2 People are following this question.

avatar image avatar image

Related Questions

Problem with Custom Input Manager 1 Answer

Check the latest key pressed? 2 Answers

Anyway to get what key is down? 1 Answer

Key not recognized 2 Answers

Having issues with Input.GetKey... 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