Esempio n. 1
0
    /// <summary>
    /// Call this before using any of the input functions.
    /// </summary>
    public static void Init()
    {
#if UNITY_ANDROID && !UNITY_EDITOR
        return;
#endif
        if (initDone)
        {
            return;
        }
        initDone = true;

        //Add all keybinds here.
        defaultInput = new InputClass();

        defaultInput.keybinds.Add(new KeyBind(KeyCode.W, "MoveUp", "Walk up", 0));
        defaultInput.keybinds.Add(new KeyBind(KeyCode.S, "MoveDown", "Walk down", 0));
        defaultInput.keybinds.Add(new KeyBind(KeyCode.D, "MoveRight", "Walk right", 0));
        defaultInput.keybinds.Add(new KeyBind(KeyCode.A, "MoveLeft", "Walk left", 0));

        defaultInput.keybinds.Add(new KeyBind("Mouse wheel", 1, "ChangeColorUp", "Change color up", 2));
        defaultInput.keybinds.Add(new KeyBind("Mouse wheel", -1, "ChangeColorDown", "Change color down", 2));

        defaultInput.keybinds.Add(new KeyBind(KeyCode.Escape, "EscapeMenu", "Escape menu Toggle"));

        //Add all allowed input axes here. Other axes cannot be rebinded to.
        allowedAxes.Add(new AxisData("Mouse wheel"));
        allowedAxes.Add(new AxisData("Mouse X", true));
        allowedAxes.Add(new AxisData("Mouse Y", true));
        allowedAxes.Add(new AxisData("JoyAxis 1"));
        allowedAxes.Add(new AxisData("JoyAxis 2"));
        allowedAxes.Add(new AxisData("JoyAxis 3"));
        allowedAxes.Add(new AxisData("JoyAxis 4"));
        allowedAxes.Add(new AxisData("JoyAxis 5"));
        allowedAxes.Add(new AxisData("JoyAxis 6"));
        allowedAxes.Add(new AxisData("JoyAxis 7"));
        allowedAxes.Add(new AxisData("JoyAxis 8"));
        allowedAxes.Add(new AxisData("JoyAxis 9"));
        allowedAxes.Add(new AxisData("JoyAxis 10"));

        //Find and read saved input
        input = new InputClass();

#if UNITY_STANDALONE
        if (File.Exists(fileName))
        {
            var fileText = File.ReadAllText(fileName);
            input = JsonUtility.FromJson <InputClass>(fileText);
        }
#else
        if (PlayerPrefs.HasKey(fileName))
        {
            var fileText = PlayerPrefs.GetString(fileName);
            input = JsonUtility.FromJson <InputClass>(fileText);
        }
#endif

        bool createDefault = true;
        if (input.keybinds.Count == defaultInput.keybinds.Count)
        { //The saved settings are overwritten if default settings have changed
            for (int i = 0; i < input.keybinds.Count; i++)
            {
                if (input.keybinds[i].Same(defaultInput.keybinds[i]))
                {
                    input.keybinds[i].group = defaultInput.keybinds[i].group;
                    createDefault           = false;
                }
                else
                {
                    createDefault = true;
                    break;
                }
            }
        }

        if (createDefault)
        {
            input = defaultInput.Clone();
            SaveSettings();
        }

        //Init runtime data
        keybindsTable = new Dictionary <string, KeyBind>();

        foreach (var item in input.keybinds)
        {
            keybindsTable.Add(item.name, item);
        }
    }