예제 #1
0
    public IEnumerator CheckForNewControllersCoroutine()
    {
        while (true)
        {
            var devices = Input.GetJoystickNames();

            for (int i = 0; i < devices.Length; i++)
            {
                bool alreadyExists = false;
                //Check that the old mappings aren't made again due to a shift in the devices.
                for (int x = 1; x < this.PlayerMappings.Count; x++)
                {
                    if (this.PlayerMappings[x].OriginalIndex == i)
                    {
                        alreadyExists = true;
                    }
                }
                if (alreadyExists)
                {
                    continue;
                }

                if (!string.IsNullOrEmpty(devices[i]))
                {
                    Type typeofInput;
                    if (this.NameToInputMappingLookupTable.TryGetValue(devices[i], out typeofInput))
                    {
                        InputMapping instance = (InputMapping)Activator.CreateInstance(typeofInput);
                        instance.OriginalIndex = i;
                        instance.MapBindings(i + 1);
                        this.PlayerMappings.Add(instance);
                    }
                    else
                    {
                        Debug.Log("Unknown controller device, pray it works as Xbox input");
                        Xbox360Mapping mapping = new Xbox360Mapping();
                        mapping.OriginalIndex = i;
                        mapping.MapBindings(i + 1);
                        this.PlayerMappings.Add(mapping);
                    }
                    if (OnNewControllerConnected != null)
                    {
                        OnNewControllerConnected.Invoke(this.PlayerMappings.Count - 1);
                    }
                }
            }
            yield return(new WaitForSeconds(this.CheckForNewControllerTimer));
        }
    }
예제 #2
0
    private void Start()
    {
        KeyboardMapping StandardKeyboardMapping = new KeyboardMapping();

        StandardKeyboardMapping.MapBindings(-1);
        StandardKeyboardMapping.OriginalIndex = -1;
        this.PlayerMappings.Add(StandardKeyboardMapping);
        this.FillNameToInputMappingLookupTable();

        var devices = Input.GetJoystickNames();

        for (int i = 0; i < devices.Length; i++)
        {
            if (!string.IsNullOrEmpty(devices[i]))
            {
                Type typeofInput;
                if (this.NameToInputMappingLookupTable.TryGetValue(devices[i], out typeofInput))
                {
                    InputMapping instance = (InputMapping)Activator.CreateInstance(typeofInput);
                    instance.OriginalIndex = i;

                    instance.MapBindings(i + 1);
                    this.PlayerMappings.Add(instance);
                }
                else
                {
                    Debug.Log("Unknown controller device, pray it works as Xbox input");
                    Xbox360Mapping mapping = new Xbox360Mapping();
                    mapping.OriginalIndex = i;
                    mapping.MapBindings(i + 1);
                    this.PlayerMappings.Add(mapping);
                }
            }
        }

        this.StartCoroutine(this.CheckForNewControllersCoroutine());
        this.StartCoroutine(this.CheckForControllerDC());
    }