This is really a sanity check as I have no other engineers on my job to run this by.
I have an enum called Languages, the members of which I’ve assigned power of two integers:
public enum Languages
{
English = 1,
Arabic = 2,
Klingon = 4,
Jibberish = 8,
};
I am displaying these in a SelectionGrid in OnGUI using the technique:
_selectedLanguage = GUI.SelectionGrid (new Rect (100f, 0f, 100f, Screen.height * .12f), _selectedLanguage, _languageNames, 1);
Where _languageNames is {“English”,“Arabic”,“Klingon”,“Jibberish”}, extracted from the Languages enum (not hard coded, of course!)
The user selects a language in the UI, and I want to store it in playerprefs. I’m taking advantage of the fact that I’m using power of two’s in my enum to use bit-shifting to extract the Language selected using the zero-based selected index stored in _selectedLanguage:
string lang = ((Languages)(1 << _selectedLanguage)).ToString();
PlayerPrefs.SetString("Language", lang);
This works but it seems hacky. Is there a better technique I’m missing?