- Home /
Question by
miszelfox · Nov 25, 2015 at 03:54 PM ·
c#serializationstatic
Save static fields values
I would like to keep some of static fields from static class between sessions.
private static KeyCode[] KeyRight = new KeyCode[4];
private static KeyCode[] KeyLeft = new KeyCode[4];
private static KeyCode[] KeyUp = new KeyCode[4];
private static KeyCode[] KeyDown = new KeyCode[4];
These are variables i want to keep and load their values at start. I'd like to them to keep controls for 4 players (that's why these are arrays). I use functions to set/get their values. This class is static, other classes just use Get functions to load keys from it. Can you help me with that?
Comment
Best Answer
Answer by Jessespike · Nov 25, 2015 at 04:03 PM
One solution is to use PlayerPrefs.
// Saving
PlayerPrefs.SetInt("KeyRight_0", (int)KeyRight[0]);
PlayerPrefs.SetInt("KeyRight_1", (int)KeyRight[1]);
PlayerPrefs.SetInt("KeyRight_2", (int)KeyRight[2]);
PlayerPrefs.SetInt("KeyRight_3", (int)KeyRight[3]);
// Loading
KeyRight[0] = (KeyCode) PlayerPrefs.GetInt("KeyRight_0");
KeyRight[1] = (KeyCode) PlayerPrefs.GetInt("KeyRight_1");
KeyRight[2] = (KeyCode) PlayerPrefs.GetInt("KeyRight_2");
KeyRight[3] = (KeyCode) PlayerPrefs.GetInt("KeyRight_3");
Your answer
Follow this Question
Related Questions
Help implementing code ;) 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
What is the most simple way to serialize a list? 1 Answer
does this key not found error have to do with serialization? 2 Answers