Search Unity

[CodeSource]Multiple configurations of controls game.

Discussion in 'Scripting' started by IsGreen, Aug 23, 2014.

  1. IsGreen

    IsGreen

    Joined:
    Jan 17, 2014
    Posts:
    206
    Modifying the Settings Enumeration in static Controls class, you can create different types of configuration game controls.

    The SettingsControl class manages "Default" configuration and different types of configurations created from Settings Enumeration.

    Control Settings are stored using PlayerPrefs.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SettingsControl : MonoBehaviour {
    5.  
    6.     public bool configureControls = true; // Show Setting GUI
    7.     public GUIStyle guiStyle; // Buttons GUIStyle
    8.     public Controls.Control[] controls; //Controls Defined
    9.  
    10.     Controls.Control keySelected = null; // Button-Control Selected to Change
    11.     string[] ControlsSettings; // Name of all Control Settings
    12.     string selectedControls; // Control Settings selected
    13.     Rect rect = new Rect();
    14.  
    15.     KeyCode keyPressed{
    16.        
    17.         get{
    18.  
    19.             KeyCode key;
    20.             System.Array keys = System.Enum.GetValues(typeof(KeyCode));
    21.  
    22.             for(int i=0;i<keys.Length;i++){
    23.  
    24.                 key = (KeyCode)keys.GetValue(i);
    25.                 if(Input.GetKeyDown(key)) return key;
    26.  
    27.             }
    28.            
    29.             return KeyCode.None;
    30.            
    31.         }
    32.        
    33.     }
    34.  
    35.     void Awake(){
    36.  
    37.         if(this.controls.Length==0){
    38.  
    39.             Debug.Log("Assign Key Controls");
    40.             Destroy(this);
    41.  
    42.         } else {
    43.  
    44.             //Save Default Controls Setting
    45.             Controls.controls = this.controls;
    46.             Controls.Save("Default");
    47.  
    48.             //Controls name array
    49.             string str = "";
    50.             for(int i=0;i<this.controls.Length;i++) str += controls[i].Name;
    51.  
    52.             //Is initialized?
    53.             if(PlayerPrefs.HasKey("ControlSettings")){
    54.  
    55.                 //Compare if not change names array.
    56.                 if(str.Equals(PlayerPrefs.GetString("ControlSettings"))) Controls.Load();
    57.                 else Controls.Initialize();
    58.  
    59.             } else Controls.Initialize();
    60.  
    61.             this.ControlsSettings = new string[System.Enum.GetValues(typeof(Controls.Settings)).Length];
    62.             for(int i=0;i<ControlsSettings.Length;i++){
    63.  
    64.                 Controls.Settings setting = (Controls.Settings)i;
    65.                 this.ControlsSettings[i] = setting.ToString();
    66.  
    67.             }
    68.  
    69.             this.selectedControls = PlayerPrefs.GetString("SelectedControls");
    70.  
    71.         }
    72.    
    73.    
    74.     }
    75.    
    76.     void Update(){
    77.        
    78.         if(this.keySelected != null){
    79.  
    80.             if(Input.anyKeyDown){
    81.                                    
    82.                 KeyCode key = keyPressed;
    83.                    
    84.                 if(key != KeyCode.Escape){
    85.                        
    86.                     Controls.SetKey(this.keySelected.Name , key);
    87.                     Controls.Save();                      
    88.                        
    89.                 }
    90.                    
    91.                 this.keySelected = null;
    92.  
    93.             }
    94.  
    95.         }
    96.        
    97.     }
    98.  
    99.     void OnGUI(){
    100.  
    101.         if(this.configureControls){
    102.  
    103.             float height = 5f;
    104.  
    105.             string str = "### "+this.selectedControls+" ###";
    106.             rect.size = this.guiStyle.CalcSize(new GUIContent(str));
    107.             rect.position = new Vector2(Screen.width*0.5f-rect.size.x*0.5f,height+rect.size.y*0.5f);
    108.             GUI.Label(rect,str);
    109.  
    110.             height += rect.size.y + 10f;
    111.  
    112.             foreach(Controls.Control control in this.controls){
    113.  
    114.                 if(this.keySelected==control){
    115.  
    116.                     rect.size = this.guiStyle.CalcSize(new GUIContent("Press any key"));
    117.                     rect.position = new Vector2(Screen.width*0.5f-rect.size.x*0.5f,height+rect.size.y*0.5f);
    118.                     GUI.Button(rect,"Press any key");
    119.  
    120.                 } else {
    121.  
    122.                     str = control.Name+": "+control.Key.ToString();
    123.                     rect.size = this.guiStyle.CalcSize(new GUIContent(str));
    124.                     rect.position = new Vector2(Screen.width*0.5f-rect.size.x*0.5f,height+rect.size.y*0.5f);
    125.  
    126.                     if(GUI.Button(rect,str)) this.keySelected = control;
    127.  
    128.                 }
    129.  
    130.                 height+=rect.size.y;
    131.  
    132.             }
    133.  
    134.             height += 20f;
    135.  
    136.             for(int i=0;i<this.ControlsSettings.Length;i++){
    137.  
    138.                 rect.size = this.guiStyle.CalcSize(new GUIContent(this.ControlsSettings[i]));
    139.                 rect.position = new Vector2(Screen.width*0.5f-rect.size.x*0.5f,height+rect.size.y*0.5f);
    140.                 if(GUI.Button(rect,this.ControlsSettings[i])){
    141.  
    142.                     this.selectedControls = this.ControlsSettings[i];
    143.                     PlayerPrefs.SetString("SelectedControls",this.ControlsSettings[i]);
    144.                     Controls.Load();
    145.                     this.keySelected = null;
    146.  
    147.                 }
    148.  
    149.                 height += rect.size.y;
    150.  
    151.             }
    152.  
    153.             height += 20f;
    154.  
    155.             rect.size = this.guiStyle.CalcSize(new GUIContent("RESET"));
    156.             rect.position = new Vector2(Screen.width*0.5f-rect.size.x*0.5f,height+rect.size.y*0.5f);
    157.             if(GUI.Button(rect,"RESET")){
    158.  
    159.                 Controls.Reset();
    160.                 Controls.Load();
    161.                 this.keySelected = null;
    162.  
    163.             }
    164.  
    165.         }
    166.  
    167.     }
    168.    
    169. }
    170.  
    171. public static class Controls    //Settings for controls
    172. {
    173.     [System.Serializable]
    174.     public class Control{
    175.        
    176.         public string Name;
    177.         public KeyCode Key;
    178.        
    179.         public Control(){}
    180.         public Control(string name,KeyCode key){
    181.            
    182.             this.Name = name;
    183.             this.Key  = key;
    184.            
    185.         }
    186.        
    187.     }
    188.  
    189.     public enum Settings { PlayerControls, SecondPlayerControls, ThirdPlayerControls };
    190.  
    191.     public static Control[] controls = null;
    192.     public static void Save(){
    193.  
    194.         string str = PlayerPrefs.GetString("SelectedControls");
    195.         if(str.Length != 0) Save(str);
    196.  
    197.     }
    198.     public static void Save(string controlsName) {
    199.  
    200.         if(controls==null) return;
    201.         for(int i=0;i<controls.Length;i++)
    202.             PlayerPrefs.SetString(controlsName+"."+controls[i].Name, controls[i].Key.ToString());
    203.    
    204.     }
    205.     public static void Load(){
    206.  
    207.         string str = PlayerPrefs.GetString("SelectedControls");
    208.         if(str.Length == 0){
    209.  
    210.             str = ((Settings)0).ToString();
    211.             PlayerPrefs.SetString("SelectedControls",str);
    212.  
    213.         }
    214.  
    215.         Load(str);
    216.  
    217.     }
    218.     public static void Load(string controlsName) {
    219.  
    220.         if(controls==null) return;
    221.         for(int i=0;i<controls.Length;i++){
    222.  
    223.             if(PlayerPrefs.HasKey(controlsName+"."+controls[i].Name))
    224.                 controls[i].Key = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString(controlsName+"."+controls[i].Name));
    225.        
    226.         }
    227.  
    228.     }
    229.     public static void SetKey(string name,KeyCode key){
    230.  
    231.         if(controls==null) return;
    232.         int i;
    233.         //Have assigned key?
    234.         for(i=0;i<controls.Length;i++) if(controls[i].Key == key) return;
    235.         for(i=0;i<controls.Length;i++){
    236.  
    237.             if(controls[i].Name.Equals(name)){
    238.  
    239.                 controls[i].Key = key;
    240.                 break;
    241.  
    242.             }
    243.  
    244.         }
    245.  
    246.     }
    247.     public static KeyCode GetKey(string name){
    248.  
    249.         if(controls==null) return KeyCode.None;
    250.         for(int i=0;i<controls.Length;i++) if(controls[i].Name.Equals(name)) return controls[i].Key;
    251.         return KeyCode.None;
    252.  
    253.     }
    254.     public static void Reset(){
    255.  
    256.         if(controls==null) return;
    257.         string str = PlayerPrefs.GetString("SelectedControls");
    258.         if(str.Length != 0){
    259.  
    260.             Load("Default");
    261.             Save(str);
    262.  
    263.         }
    264.  
    265.     }
    266.     public static void Initialize(){
    267.  
    268.         if(controls==null) return;
    269.  
    270.         Load("Default");
    271.  
    272.         for(int i=0;i<System.Enum.GetValues(typeof(Settings)).Length;i++) Save(((Settings)i).ToString());
    273.  
    274.         //Controls name array
    275.         string str = "";
    276.         for(int i=0;i<controls.Length;i++) str += controls[i].Name;
    277.         PlayerPrefs.SetString("ControlSettings",str);
    278.         PlayerPrefs.SetString("SelectedControls",((Settings)0).ToString());
    279.  
    280.     }
    281.  
    282. }


    Download link.