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 DanGoldstein91 · May 20, 2015 at 10:52 AM · playerprefssavedictionarykeycode

How to save dictionary keycode values to PlayerPrefs with C#

Hi. I have a dictionary that holds < string, KeyCode > value pairs, but I need to save the keycode values to PlayerPrefs. From what I understand I need to convert the KeyCodes to an int or a string in order to store them. I tried converting the KeyValuePairs to a string using a forEach but I kept receiving "Cannot convert... 'KeyValuePair'... to 'string'". I am still a dev rookie so I may be using improper syntax.

I did the work Googling around and I found some C# solutions but I couldn't get some of their syntax to work in Unity. After a few hours of not getting anywhere with my research, I felt I should ask for advice and be taught how to properly do it. I appreciate any help I get on this. I'll include my failed attempt at converting the values to a string in the 2nd class below.

The dictionary:

 public class ControlKeybinds : MonoBehaviour {
     public static Dictionary < string, KeyCode > keyBinds = new Dictionary < string, KeyCode > ();
     public void SavedKeyBinds () {
         keyBinds.Add( "Player1Right", KeyCode.D );
         keyBinds.Add( "Player1Left", KeyCode.A );
     }


 public class SaveKeybinds : MonoBehaviour {
     public static void SaveControlKeybinds () {
         foreach( string key in ControlKeybinds.keyBinds ) {
             var value = ControlKeybinds.keyBinds[key];
         }
     }
 }


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
2
Best Answer

Answer by Baste · May 20, 2015 at 11:15 AM

foreaching over a dictionary (like your keybinds) gives you KeyValuePairs, not the type of the keys.

You can solve this either by taking that into account, or doing a foreach over the dictionary's .Keys property. Assuming that you do the second one, saving everything to playerprefs should be simple:

 public static void SaveControlKeybinds () {
     foreach(string key in ControlKeybinds.keyBinds.Keys ) {
 
         //cast the enum to an int
         int intRepresentation = (int) keyBinds[key];
         
         //Save the keybind
         PlayerPrefs.SetInt(key, intRepresentation);
     }
     
     //Write the changes to disk
     PlayerPrefs.Save();
 }

Every enum value has a corresponding integer value, and you can cast both ways. So if you need to retrieve say the "Player1Right" key, you can retrieve it and put it back in your dict like this:

 keyBinds["Player1Right"] = (KeyCode) PlayerPrefs.GetInt("Player1Right");

Hope that helps!

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 DanGoldstein91 · May 20, 2015 at 08:19 PM 0
Share

Awesome, that helped me out a lot! $$anonymous$$y $$anonymous$$eybind menu works like a pro's now. Thank you so much, Baste.

avatar image
0

Answer by nathanthesnooper · Jan 04, 2017 at 12:49 AM

I prefer compressing to one big and sloppy piece of code.

public Dictionary<string, string> paths = new Dictionary<string, string>(); public string dict; public void GetDict () { paths = new Dictionary<string, string>(); string[] splitSemicolon = dict.Split (';'); for (int i = 0; i < splitSemicolon.Length - 1; i++) { string[] keysAndValues = splitSemicolon[i].Split(','); paths.Add (keysAndValues[0], keysAndValues[1]); } } public void SetDict () { dict = ""; foreach (string key in paths.Keys) { string val = paths[key]; dict += key + "," + val + ";"; } }

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Can I save a whole Dictionary? 4 Answers

How to save gameobject values with respect to scene? 1 Answer

Playerprefs save player position 3 Answers

How can i get the following script back on track to being a single level high score saver 0 Answers

Highscrore won't work 1 Answer


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